If you think the Android project drive-mount listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
* Copyright 2014 Jan Seeger//www.java2s.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package net.alphadev.fat32wrapper;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import de.waldheinz.fs.FsFile;
publicclass ReadingFileHandle extends InputStream {
privatefinalint totalFileSize;
private FsFile file;
privateint position;
public ReadingFileHandle(FsFile file) {
this.file = file;
totalFileSize = (file != null) ? (int) file.getLength() : 0;
}
@Override
publicint read(@NotNull byte[] buffer) throws IOException {
finalint bytesRemaining = available();
if (bytesRemaining <= 0) {
return -1;
}
finalint bytesRead = Math.min(buffer.length, bytesRemaining);
final ByteBuffer bb = ByteBuffer.wrap(buffer);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.limit(bytesRead);
if (file != null) {
file.read(position, bb);
position += bytesRead;
}
return bytesRead;
}
@Override
publicint available() {
return totalFileSize - position;
}
@Override
publicint read() throws IOException {
if (available() <= 0) {
return -1;
}
finalbyte[] buffer = newbyte[4];
final ByteBuffer bb = ByteBuffer.wrap(buffer);
bb.order(ByteOrder.LITTLE_ENDIAN);
if (file != null) {
file.read(position, bb);
position += 4;
}
return bb.getInt(position);
}
@Override
publicboolean markSupported() {
return false;
}
@Override
publicvoid close() {
this.file = null;
}
}