Skip to content

Instantly share code, notes, and snippets.

@enisinanaj
Created April 23, 2019 14:58
Show Gist options
  • Save enisinanaj/6ceb13eaf25ba3cbaa96c9026291803d to your computer and use it in GitHub Desktop.
Save enisinanaj/6ceb13eaf25ba3cbaa96c9026291803d to your computer and use it in GitHub Desktop.
Read a chunk of data from the file. Java
public byte[] getNextFileChunk() throws IOException {
FileInputStream fileReader = new FileInputStream(file);
byte[] b;
int realLength = CHUNK_SIZE;
// assuming chunk index starts from 1
if (currentChunk - 1 == chunksLength) {
b = new byte[lastChunkSize];
realLength = lastChunkSize;
} else {
b = new byte[CHUNK_SIZE];
}
fileReader.read(b, (currentChunk - 1) * CHUNK_SIZE, realLength);
return b;
}
public int countChunksLength() {
int fileSize = 0;
if (file.exists()) {
fileSize = Integer.parseInt(String.valueOf(file.length()));
}
this.chunksLength = fileSize / CHUNK_SIZE;
this.lastChunkSize = fileSize % CHUNK_SIZE;
if (lastChunkSize > 0) {
this.chunksLength++;
}
return this.chunksLength;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment