Created
November 24, 2020 13:10
-
-
Save Oleur/25db21548163e5fa00d98690eda124d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there full version ?