List of utility methods to do File Read via ByteBuffer
void | readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len) Reads len bytes in a loop using the channel of the stream int toRead = len; ByteBuffer byteBuffer = ByteBuffer.wrap(buf, off, len); while (toRead > 0) { int ret = fileChannel.read(byteBuffer); if (ret < 0) { throw new IOException("Premeture EOF from inputStream"); toRead -= ret; ... |
ByteBuffer | readFileDataIntoBufferBE(FileChannel fc, final int size) read File Data Into Buffer BE final ByteBuffer tagBuffer = ByteBuffer.allocateDirect(size); fc.read(tagBuffer); tagBuffer.position(0); tagBuffer.order(ByteOrder.BIG_ENDIAN); return tagBuffer; |
ByteBuffer | readFileFragment(FileChannel fc, long pos, int size) read File Fragment ByteBuffer fragment = null; try { fc.position(pos); fragment = ByteBuffer.allocate(size); if (fc.read(fragment) != size) return null; fragment.rewind(); } catch (IOException ex) { ... |
int | readFileHeader(FileInputStream fpi) read File Header DataInputStream inStrm = new DataInputStream(fpi); byte[] buf = new byte[4]; try { inStrm.readFully(buf, 0, 4); String fType = new String(buf, 0, 4); if ("RIFF".equals(fType)) { byteOrder = ByteOrder.LITTLE_ENDIAN; if (readWavHeader(inStrm, fpi.getChannel())) ... |
String | readFileIntoString(File localFile) read File Into String FileInputStream stream = new FileInputStream(localFile); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); |
String | readFileIntoString(String path) Read the contents of a file into String. FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fileChannel = stream.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); return Charset.defaultCharset().decode(mappedByteBuffer).toString(); } finally { if (stream != null) { ... |
void | readFileNIO(String path, StringBuilder builder) read File NIO RandomAccessFile aFile = null; FileChannel inChannel = null; try { aFile = new RandomAccessFile(path, "r"); inChannel = aFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (inChannel.read(buffer) > 0) { buffer.flip(); ... |
java.nio.ByteBuffer | readFileToBuffer(java.io.File file) read File To Buffer FileInputStream is = new FileInputStream(file); try { FileChannel fc = is.getChannel(); java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate((int) fc.size()); for (int count = 0; count >= 0 && buffer.hasRemaining();) { count = fc.read(buffer); buffer.flip(); ... |
String | readFileToString(String path) read File To String FileInputStream stream = new FileInputStream(path); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); |
String | readFileToString(String path) Reads a file into a string. FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); |