List of usage examples for java.nio ByteBuffer limit
public final Buffer limit(int newLimit)
From source file:com.koda.integ.hbase.storage.FileExtStorage.java
@Override public StorageHandle storeData(ByteBuffer buf) { writeLock.writeLock().lock();/* w ww . j a va2 s .co m*/ int pos = 0; try { if (activeBuffer.get() == null) { // unlock writeLock.writeLock().unlock(); // Get next buffer from empty queue - blocking call ByteBuffer bbuf = emptyBuffersQueue.take(); // lock again writeLock.writeLock().lock(); if (activeBuffer.get() == null) { activeBuffer.set(bbuf); bufferOffset.set(0); } else { // somebody already set the activeBuffer // repeat call recursively emptyBuffersQueue.offer(bbuf); writeLock.writeLock().unlock(); return storeData(buf); } } pos = buf.position(); long currentFileLength = currentFileOffsetForWrites.get(); if (bufferOffset.get() == 0 && currentFileLength + bufferSize > fileSizeLimit) { // previous buffer was flushed currentFileOffsetForWrites.set(0); maxIdForWrites.incrementAndGet(); } int size = buf.getInt(); long off = bufferOffset.getAndAdd(size + 4); if (off + size + 4 > bufferSize) { // send current buffer to write queue ByteBuffer buff = activeBuffer.get(); //verifyBuffer(buff); writeQueue.offer(buff); activeBuffer.set(null); if (currentFileLength + bufferSize > fileSizeLimit) { currentFileOffsetForWrites.set(0); maxIdForWrites.incrementAndGet(); } // release lock writeLock.writeLock().unlock(); // Get next buffer from empty queue ByteBuffer bbuf = emptyBuffersQueue.take(); // lock again writeLock.writeLock().lock(); if (activeBuffer.get() == null) { activeBuffer.set(bbuf); } else { // some other thread set already the activeBuffer // repeat call recursively emptyBuffersQueue.offer(bbuf); writeLock.writeLock().unlock(); buf.position(pos); return storeData(buf); } bufferOffset.set(size + 4); // Check if need advance file } // We need to keep overall object (key+block) size in a file buf.position(pos); buf.limit(pos + size + 4); activeBuffer.get().put(buf); FileStorageHandle fsh = new FileStorageHandle(maxIdForWrites.get(), (int) (currentFileOffsetForWrites.get()), size); // Increase offset in current file for writes; currentFileOffsetForWrites.addAndGet(size + 4); return fsh; } catch (InterruptedException e) { e.printStackTrace(); writeLock.writeLock().unlock(); buf.position(pos); return storeData(buf); } finally { WriteLock lock = writeLock.writeLock(); if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
From source file:com.linkedin.databus.core.DbusEventBuffer.java
/** * Batch interface to write events within a range out into a WritableByteChannel * @param range// ww w .java 2 s. co m * @param writeChannel * @param encoding * @return number of bytes written */ public int batchWrite(Range range, WritableByteChannel writeChannel, Encoding encoding) { long startOffset = range.start; long endOffset = range.end; assert (_bufferPositionParser.bufferIndex(startOffset) == _bufferPositionParser.bufferIndex(endOffset)); ByteBuffer buf = _buffers[_bufferPositionParser.bufferIndex(startOffset)]; int endBufferOffset = _bufferPositionParser.bufferOffset(endOffset); int startBufferOffset = _bufferPositionParser.bufferOffset(startOffset); int bytesWritten = 0; switch (encoding) { case BINARY: { ByteBuffer writeBuf = buf.duplicate().order(_eventFactory.getByteOrder()); writeBuf.position(startBufferOffset); writeBuf.limit(endBufferOffset); try { bytesWritten = writeChannel.write(writeBuf); } catch (IOException e1) { LOG.error("batchWrite error: " + e1.getMessage(), e1); throw new RuntimeException(e1); } break; } case JSON: case JSON_PLAIN_VALUE: { DbusEventInternalReadable e = _eventFactory.createReadOnlyDbusEventFromBuffer(buf, startBufferOffset); int currentBufferOffset = startBufferOffset; while (currentBufferOffset != endBufferOffset) { e = e.reset(buf, currentBufferOffset); e.writeTo(writeChannel, encoding); currentBufferOffset += e.size(); } } } return (bytesWritten); }
From source file:com.linkedin.databus.core.DbusEventBuffer.java
/** * go over all the ByteBuffers and validate them * @throws DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException */// w w w .ja va 2 s . c om private void setAndValidateMMappedBuffers(DbusEventBufferMetaInfo mi) throws DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException { // set buffer info - pos and limit DbusEventBufferMetaInfo.BufferInfo[] bufsInfo = null; bufsInfo = mi.getBuffersInfo(); int i = 0; for (ByteBuffer buffer : _buffers) { DbusEventBufferMetaInfo.BufferInfo bi = bufsInfo[i]; buffer.position(bi.getPos()); buffer.limit(bi.getLimit()); // validate if (buffer.position() > buffer.limit() || buffer.limit() > buffer.capacity() || buffer.capacity() != bi.getCapacity()) { String msg = "ByteBuffers don't match: i=" + i + "; pos=" + buffer.position() + "; limit=" + buffer.limit() + "; capacity=" + buffer.capacity() + "; miCapacity=" + bi.getCapacity(); throw new DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException(mi, msg); } i++; } _log.info("successfully validated all " + i + " mmapped buffers"); }