/** * Writes data to the block from an input {@link ByteBuffer}. * * @param offset starting offset of the block file to write * @param inputBuf {@link ByteBuffer} that input data is stored in * @return the size of data that was written */ private long write(long offset, ByteBuffer inputBuf) throws IOException { int inputBufLength = inputBuf.limit() - inputBuf.position(); MappedByteBuffer outputBuf = mLocalFileChannel.map(FileChannel.MapMode.READ_WRITE, offset, inputBufLength); outputBuf.put(inputBuf); int bytesWritten = outputBuf.limit(); BufferUtils.cleanDirectBuffer(outputBuf); return bytesWritten; } }
public ByteBuffer[] sliceTerms() { final ByteBuffer[] terms = new ByteBuffer[PARTITION_COUNT]; if (termLength < ONE_GIG) { final MappedByteBuffer buffer = mappedBuffers[0]; for (int i = 0; i < PARTITION_COUNT; i++) { buffer.limit((termLength * i) + termLength).position(termLength * i); terms[i] = buffer.slice(); } } else { for (int i = 0; i < PARTITION_COUNT; i++) { terms[i] = mappedBuffers[i].duplicate(); } } return terms; }
/** * Re-map byte buffer into memory, called when file size has changed or file * was created. */ private void reMap() throws IOException { int oldPos = 0; if (mapped != null) { oldPos = pos; unMap(); } fileLength = file.length(); checkFileSizeLimit(fileLength); // maps new MappedByteBuffer; the old one is disposed during GC mapped = file.getChannel().map(mode, 0, fileLength); int limit = mapped.limit(); int capacity = mapped.capacity(); if (limit < fileLength || capacity < fileLength) { throw new IOException("Unable to map: length=" + limit + " capacity=" + capacity + " length=" + fileLength); } if (SysProperties.NIO_LOAD_MAPPED) { mapped.load(); } this.pos = Math.min(oldPos, (int) fileLength); }
/** * Re-map byte buffer into memory, called when file size has changed or file * was created. */ private void reMap() throws IOException { int oldPos = 0; if (mapped != null) { oldPos = pos; unMap(); } fileLength = file.length(); checkFileSizeLimit(fileLength); // maps new MappedByteBuffer; the old one is disposed during GC mapped = file.getChannel().map(mode, 0, fileLength); int limit = mapped.limit(); int capacity = mapped.capacity(); if (limit < fileLength || capacity < fileLength) { throw new IOException("Unable to map: length=" + limit + " capacity=" + capacity + " length=" + fileLength); } if (SysProperties.NIO_LOAD_MAPPED) { mapped.load(); } this.pos = Math.min(oldPos, (int) fileLength); }
mappedBuffer.limit(offset + termLength).position(offset);
/** * @see java.io.RandomAccessFile#length() * @return long length */ public long length() { return mappedByteBuffer.limit(); }
/** * @see java.io.RandomAccessFile#length() * @return long length */ public long length() { return mappedByteBuffer.limit(); }
/** * @see java.io.RandomAccessFile#length() * @return long length */ public long length() { return mappedByteBuffer.limit(); }
private ByteBuffer wrap(MappedByteBuffer map) { byte[] encryptedHashTableBytes = new byte[map.limit()]; map.get(encryptedHashTableBytes); return ByteBuffer.wrap(encryptedHashTableBytes); }
@Override public void setPosition(int position) throws IOException { if (position > buf.limit()) { throw new EOFException(); } buf.position(position); }
@Override public void setPosition(int position) throws IOException { if (position > buf.limit()) { throw new EOFException(); } buf.position(position); }
public void seekToEndOfFile() { mappedByteBuffer.position(mappedByteBuffer.limit()); } }
@Override public String toString() { return "CycleLogOutput{" + "mbb=" + mbb + ", file=" + file + ", mbb=" + (mbb == null ? "null" : "(pos=" + mbb.position() + ", limit=" + mbb.limit() + ", capacity=" + mbb.capacity() + ")") + '}'; }
protected void copyToTunnelStreamBuffer(TunnelStreamPersistenceBuffer persistBuffer, TunnelStreamBuffer tunnelStreamBuffer) { int tmpLimit = _fileByteBuf.limit(); int startPos = persistBuffer.filePosition() + persistBufferMsgOffset() + persistBuffer.tunnelStreamHeaderLen(); int length = persistBuffer.length() - persistBuffer.tunnelStreamHeaderLen(); _fileByteBuf.limit(startPos + length); _fileByteBuf.position(startPos); tunnelStreamBuffer.data().put(_fileByteBuf); _fileByteBuf.limit(tmpLimit); tunnelStreamBuffer.setCurrentPositionAsEndOfEncoding(); tunnelStreamBuffer.persistenceBuffer(_tunnelSubstream, persistBuffer); }
private long allocateAddress(long offset, int size) { cachedBuffer = getBufferInternal(offset, size); int p = cachedBuffer.position(); cachedBufferLo = offset - p - 1; cachedBufferHi = cachedBufferLo + cachedBuffer.limit() + 2; cachedAddress = ByteBuffers.getAddress(cachedBuffer); cachedPointer = cachedAddress - cachedBufferLo - 1; return cachedAddress + p; }
@Override public final long writeToFileChannel(final FileChannel chanDst, final long maxCount) throws IOException { final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput.getLayer(layerNum).asReadOnlyBuffer(); int n = (int) ((offset + position) % dataInputSize); ringBuff.position(n); n = (int) Math.min(maxCount, ringBuff.remaining()); ringBuff.limit(ringBuff.position() + n); n = chanDst.write(ringBuff); position += n; return n; }
@Override public final long writeToFileChannel(final FileChannel chanDst, final long maxCount) throws IOException { final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput .getLayer(layerNum) .asReadOnlyBuffer(); int n = (int) ((offset + position) % dataInputSize); ringBuff.position(n); n = (int) Math.min(maxCount, ringBuff.remaining()); ringBuff.limit(ringBuff.position() + n); n = chanDst.write(ringBuff); position += n; return n; }
public MappedByteBuffer getBuffer(long offset) { if (offset > cachedBufferLo && offset + 1 < cachedBufferHi) { cachedBuffer.position((int) (offset - cachedBufferLo - 1)); } else { cachedBuffer = getBufferInternal(offset, 1); cachedBufferLo = offset - cachedBuffer.position() - 1; cachedBufferHi = cachedBufferLo + cachedBuffer.limit() + 2; cachedAddress = ByteBuffers.getAddress(cachedBuffer); cachedPointer = cachedAddress - cachedBufferLo - 1; } return cachedBuffer; }
private long allocateAddress(long offset) { MappedByteBuffer buf = getBufferInternal(offset); cachedBufferLo = offset - buf.position() - 1; cachedBufferHi = cachedBufferLo + buf.limit() + 2; cachedAddress = ByteBuffers.getAddress(buf); return cachedAddress + buf.position(); }
public static ByteBuffer getSampleData(int sample, File file) throws IOException { MovieBox moov = MP4Util.parseMovie(file); MediaInfoBox minf = moov.getAudioTracks().get(0).getMdia().getMinf(); ChunkOffsetsBox stco = NodeBox.findFirstPath(minf, ChunkOffsetsBox.class, Box.path("stbl.stco")); SampleToChunkBox stsc = NodeBox.findFirstPath(minf, SampleToChunkBox.class, Box.path("stbl.stsc")); SampleSizesBox stsz = NodeBox.findFirstPath(minf, SampleSizesBox.class, Box.path("stbl.stsz")); long sampleOffset = getSampleOffset(sample, stsc, stco, stsz); MappedByteBuffer map = NIOUtils.mapFile(file); map.position((int) sampleOffset); map.limit(map.position() + stsz.getSizes()[sample]); return map; }