private String readBinaryFile(String filename, IDecoder decoder) { String contents = ""; try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(new File(filename)))) { byte[] header = new byte[8]; byte[] bytes = new byte[2]; int headerLength = stream.read(header); if (headerLength != 8) { throw new IOException("File header corrupted."); } if (stream.read(bytes) > 0) { int size = Integer.parseInt(new String(bytes)); byte[] body = new byte[size]; if (stream.read(body) != size) { throw new IOException("File body corrupted."); } contents = new String(body); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return decoder.decode(contents); }
private String readFile(String filename, IReader reader, IDecoder decoder) { return decoder.decode(reader.read(filename)); }