Skip to content

Instantly share code, notes, and snippets.

@Oleur
Created November 24, 2020 13:10
Show Gist options
  • Save Oleur/25db21548163e5fa00d98690eda124d3 to your computer and use it in GitHub Desktop.
Save Oleur/25db21548163e5fa00d98690eda124d3 to your computer and use it in GitHub Desktop.
class SmbDataSource(private val dataSpec: DataSpec) : BaseDataSource(/* isNetwork= */ true) {
private var smbClient: SMBClient? = null
private var inputStream: InputStream? = null
private var bytesRemaining: Long
private var opened: Boolean
@Throws(IOException::class)
override fun open(): Long {
try {
smbClient = SMBClient()
val connection = smbClient.connect(hostName)
val ac = AuthenticationContext(username, password.toCharArray(), "WORKGROUP")
val session = connection.authenticate(ac)
// Connect to Share
val share = session.connectShare(shareName) as DiskShare
val shareAccess = hashSetOf<SMB2ShareAccess>()
s.add(SMB2ShareAccess.ALL.iterator().next()) // READ only
val remoteSmbjFile = share.openFile(mPath, EnumSet.of(AccessMask.GENERIC_READ),
null, s, SMB2CreateDisposition.FILE_OPEN, null)
// Open the input stream
inputStream = remoteSmbjFile.getInputStream()
// Get the number of bytes that can be read from the opened source
val skipped = inputStream.skip(dataSpec.position)
if (skipped < dataSpec.position) {
throw EOFException()
}
if (dataSpec.length != C.LENGTH_UNSET) {
bytesRemaining = dataSpec.length;
} else {
bytesRemaining = dataSpec.length;
if (bytesRemaining == Integer.MAX_VALUE) {
bytesRemaining = C.LENGTH_UNSET;
}
}
} catch (e: Exception) {
throw IOException(e)
}
return bytesRemaining
}
override getUri() = dataSpec.uri
}
@FongMi
Copy link

FongMi commented May 31, 2023

Is there full version ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment