Skip to content

Instantly share code, notes, and snippets.

@hacksoldier
Created September 29, 2015 10:21
Show Gist options
  • Select an option

  • Save hacksoldier/b4690e6778b1014e513b to your computer and use it in GitHub Desktop.

Select an option

Save hacksoldier/b4690e6778b1014e513b to your computer and use it in GitHub Desktop.
public byte[] getByteFromFile(File file) throws IOException, FileTooBigException {
if (file.length() > MAX_FILE_SIZE) {
throw new FileTooBigException(file);
}
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1) {
ous.write(buffer, 0, read);
}
}finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
}
}
return ous.toByteArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment