public DataReader(@Nonnull File file) throws IOException { mFile = file; mRaf = new RandomAccessFile(mFile, "r"); mMappedBuffer = mRaf.getChannel().map( FileChannel.MapMode.READ_ONLY, 0, file.length()); mMappedBuffer.rewind(); setLittleEndian(true); }
/** * Return the current value without grabbing any locks. * <p> * ONLY USE THIS METHOD INTERNALLY!!! * </p> * * @return the current value */ private int getUnsafe() { int value = storage.getInt(); storage.rewind(); return value; }
/** * Set {@code value} without grabbing any locks. * <p> * ONLY USE THIS METHOD INTERNALLY!!! * </p> * * @param value the value to set * @return {@code value} for chaining */ private int setUnsafe(int value) { storage.putInt(value); storage.rewind(); return value; } }
public void printHexDump(MappedByteBuffer buff, String hdr) { int counter = 0; if (hdr != null) { System.out.println(hdr); } for (int i = 0; i < buff.capacity(); i++) { counter = i + 1; System.out.println(String.format("%02X ", buff.get())); if (counter % 16 == 0) { System.out.println(); } else { if (counter % 8 == 0) { System.out.print(" "); } } } System.out.println(); buff.rewind(); }
/** * dump the data buffer in HEX * */ public void printHexDump(MappedByteBuffer buff, String hdr) { int counter = 0; if (hdr != null) { out.println(hdr); } for (int i = 0; i < buff.capacity(); i++) { counter = i + 1; out.print(String.format("%02X ", buff.get())); if (counter % 16 == 0) { out.println(); } else { if (counter % 8 == 0) { out.print(" "); } } } out.println(); buff.rewind(); }
/** * Writes a <code>MappedByteBuffer</code> object in hexadecimal. * * @param buff a MappedByteBuffer object. * @param hdr the title string. */ public void printHexDump(MappedByteBuffer buff, String hdr) { int counter = 0; if (hdr != null) { out.println(hdr); } for (int i = 0; i < buff.capacity(); i++) { counter = i + 1; out.println(String.format("%02X ", buff.get())); if (counter % 16 == 0) { out.println(); } else { if (counter % 8 == 0) { out.print(" "); } } } out.println(); buff.rewind(); } /**
public void update( Path file ) throws IOException { long fileSize; try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) { fileSize = channel.size(); long pos = 0; while (pos<fileSize) { long bufferSize = Math.min(BUFFER_SIZE, fileSize-pos); MappedByteBuffer buffer = channel.map( FileChannel.MapMode.READ_ONLY, pos, bufferSize); update( buffer ); buffer.rewind(); fileSize = channel.size(); pos += BUFFER_SIZE; } finish(); } }
new String(Hex.encodeHex(buff.array()))); buff.rewind();
buff.rewind(); boolean DEBUG = false;
buff.rewind();
buff.rewind(); boolean DEBUG = false;
private int getGzipBufferSize(MappedByteBuffer buff) { int GZIP_BUFFER_SIZE = 120; /* note: gzip buffer size <= 118 causes "java.io.EOFException: Unexpected end of ZLIB input stream" with a byte buffer of 500 bytes */ // adjust gzip buffer size if necessary // file.size might be less than the default gzip buffer size if (buff.capacity() < GZIP_BUFFER_SIZE) { GZIP_BUFFER_SIZE = buff.capacity(); } buff.rewind(); return GZIP_BUFFER_SIZE; }
public void close() throws IOException { _bytebuffer.rewind(); _bytebuffer.putInt(_firstDim); _bytebuffer.putInt(_secondDim); _bytebuffer.putInt(_thirdDim); _bytebuffer.force(); _filechannel.close(); _raf.close(); _name = "no file"; } }
public static void update( List<Checksum> checksums, Path file ) throws ChecksumValidationException { long fileSize; try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) { fileSize = channel.size(); long pos = 0; while (pos<fileSize) { long bufferSize = Math.min(BUFFER_SIZE, fileSize-pos); MappedByteBuffer buffer = channel.map( FileChannel.MapMode.READ_ONLY, pos, bufferSize); for ( Checksum checksum : checksums ) { checksum.update( buffer ); buffer.rewind(); } fileSize = channel.size(); pos += BUFFER_SIZE; } for (Checksum checksum : checksums) { checksum.finish(); } } catch(FileNotFoundException e) { throw new ChecksumValidationException( ChecksumValidationException.ValidationError.FILE_NOT_FOUND, "File that should be parsed, not found: "+e.getMessage(), e ); } catch(IOException e) { throw new ChecksumValidationException( ChecksumValidationException.ValidationError.READ_ERROR, "Parsing of file failed: "+e.getMessage(), e ); } }
mappedByteBuffer.rewind();
public static boolean setEventBufferFile(String fileName) { if ((status != null) && status.remoteProfiling) { return true; } if (bufFile != null) { removeEventBufferFile(); } try { bufFile = new File(fileName); raFile = new RandomAccessFile(bufFile, "rw"); // NOI18N bufFileChannel = raFile.getChannel(); mapByteBuf = bufFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, EVENT_BUFFER_SIZE_IN_BYTES); mapByteBuf.rewind(); mapByteBuf.mark(); bufFileExists = true; } catch (FileNotFoundException ex1) { return false; } catch (IOException ex2) { ProfilerLogger.severe("internal error when opening memory-mapped temporary file"); // NOI18N ProfilerLogger.log(ex2); ProfilerLogger.severe(PLEASE_REPORT_PROBLEM); return false; } return true; }
public static void forceDelete(File file) throws IOException { if (file.exists()) { SecureRandom random = new SecureRandom(); @SuppressWarnings("resource") RandomAccessFile raf = new RandomAccessFile(file, "rw"); FileChannel channel = raf.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); // Overwrite with zeros while (buffer.hasRemaining()) { buffer.put((byte) 0); } buffer.force(); buffer.rewind(); // Overwrite with ones while (buffer.hasRemaining()) { buffer.put((byte) 0xFF); } buffer.force(); buffer.rewind(); // Overwrite with random data; one byte at a time byte[] data = new byte[1]; while (buffer.hasRemaining()) { random.nextBytes(data); buffer.put(data[0]); } buffer.force(); file.delete(); } }
private MappedByteBuffer createStitchBuffer(int size, int index, long stitchOffset) { ByteBufferWrapper bufferWrapper = stitches.getQuiet(index); if (bufferWrapper != null) { // if we already have a stitch for this buffer // it could be too small for the size // if that's the case - discard the existing stitch and create a larger one. if (bufferWrapper.getOffset() != stitchOffset || bufferWrapper.getByteBuffer().limit() < size) { bufferWrapper.close(); bufferWrapper = null; } else { bufferWrapper.getByteBuffer().rewind(); } } if (bufferWrapper == null) { bufferWrapper = new ByteBufferWrapper(stitchOffset, mapBufferInternal(stitchOffset, size)); stitches.extendAndSet(index, bufferWrapper); } return bufferWrapper.getByteBuffer(); }