Created
April 23, 2019 14:58
-
-
Save enisinanaj/6ceb13eaf25ba3cbaa96c9026291803d to your computer and use it in GitHub Desktop.
Read a chunk of data from the file. Java
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
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