List of usage examples for java.nio ByteBuffer limit
public final Buffer limit(int newLimit)
From source file:ome.services.blitz.repo.RepoRawFileStoreI.java
public void write_async(AMD_RawFileStore_write __cb, byte[] buf, long position, int length, Current __current) throws ServerError { if (true) {/*from w ww. j av a2 s. c om*/ // See ticket:2562 __cb.ice_exception(new omero.ApiUsageException(null, null, "Currently disabled.")); return; } ByteBuffer buffer = MappedByteBuffer.wrap(buf); buffer.limit(length); try { rafile.getChannel().write(buffer, position); __cb.ice_response(); } catch (Throwable t) { __cb.ice_exception(convert(t)); } }
From source file:org.gephi.desktop.importer.DesktopImportControllerUI.java
/** * Uncompress a GZIP file.//from w w w . j a v a2 s. c o m */ private static File getGzFile(FileObject in, File out, boolean isTar) throws IOException { // Stream buffer final int BUFF_SIZE = 8192; final byte[] buffer = new byte[BUFF_SIZE]; GZIPInputStream inputStream = null; FileOutputStream outStream = null; try { inputStream = new GZIPInputStream(new FileInputStream(in.getPath())); outStream = new FileOutputStream(out); if (isTar) { // Read Tar header int remainingBytes = readTarHeader(inputStream); // Read content ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE); byte[] tmpCache = new byte[BUFF_SIZE]; int nRead, nGet; while ((nRead = inputStream.read(tmpCache)) != -1) { if (nRead == 0) { continue; } bb.put(tmpCache); bb.position(0); bb.limit(nRead); while (bb.hasRemaining() && remainingBytes > 0) { nGet = Math.min(bb.remaining(), BUFF_SIZE); nGet = Math.min(nGet, remainingBytes); bb.get(buffer, 0, nGet); outStream.write(buffer, 0, nGet); remainingBytes -= nGet; } bb.clear(); } } else { int len; while ((len = inputStream.read(buffer)) > 0) { outStream.write(buffer, 0, len); } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } finally { if (inputStream != null) { inputStream.close(); } if (outStream != null) { outStream.close(); } } return out; }
From source file:org.gephi.desktop.importer.DesktopImportControllerUI.java
/** * Uncompress a Bzip2 file./*from w w w . ja v a 2 s . c o m*/ */ private static File getBzipFile(FileObject in, File out, boolean isTar) throws IOException { // Stream buffer final int BUFF_SIZE = 8192; final byte[] buffer = new byte[BUFF_SIZE]; BZip2CompressorInputStream inputStream = null; FileOutputStream outStream = null; try { FileInputStream is = new FileInputStream(in.getPath()); inputStream = new BZip2CompressorInputStream(is); outStream = new FileOutputStream(out.getAbsolutePath()); if (isTar) { // Read Tar header int remainingBytes = readTarHeader(inputStream); // Read content ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE); byte[] tmpCache = new byte[BUFF_SIZE]; int nRead, nGet; while ((nRead = inputStream.read(tmpCache)) != -1) { if (nRead == 0) { continue; } bb.put(tmpCache); bb.position(0); bb.limit(nRead); while (bb.hasRemaining() && remainingBytes > 0) { nGet = Math.min(bb.remaining(), BUFF_SIZE); nGet = Math.min(nGet, remainingBytes); bb.get(buffer, 0, nGet); outStream.write(buffer, 0, nGet); remainingBytes -= nGet; } bb.clear(); } } else { int len; while ((len = inputStream.read(buffer)) > 0) { outStream.write(buffer, 0, len); } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } finally { if (inputStream != null) { inputStream.close(); } if (outStream != null) { outStream.close(); } } return out; }
From source file:com.turn.ttorrent.client.TorrentByteStorage.java
public ByteBuffer read(int offset, int length) throws IOException { ByteBuffer data = ByteBuffer.allocate(length); int bytes = this.channel.read(data, offset); data.clear();/* w w w . j a va2 s. co m*/ data.limit(bytes >= 0 ? bytes : 0); return data; }
From source file:com.hazelcast.simulator.probes.probes.impl.HdrLatencyDistributionResult.java
@Override public void writeTo(XMLStreamWriter writer) { Histogram tmp = histogram.copy();/*from w ww . j a va2 s. c o m*/ int size = tmp.getNeededByteBufferCapacity(); ByteBuffer byteBuffer = ByteBuffer.allocate(size); int bytesWritten = tmp.encodeIntoCompressedByteBuffer(byteBuffer); byteBuffer.rewind(); byteBuffer.limit(bytesWritten); String encodedData = Base64.encodeBase64String(byteBuffer.array()); try { writer.writeStartElement(ProbesResultXmlElements.HDR_LATENCY_DATA.getName()); writer.writeCData(encodedData); writer.writeEndElement(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
From source file:org.apache.hadoop.hbase.io.hfile.slab.Slab.java
private void allocateAndSlice(int size, int sliceSize) { ByteBuffer newSlab = ByteBuffer.allocateDirect(size); slabs.add(newSlab);//from w ww . j a v a 2 s . c o m for (int j = 0; j < newSlab.capacity(); j += sliceSize) { newSlab.limit(j + sliceSize).position(j); ByteBuffer aSlice = newSlab.slice(); buffers.add(aSlice); heapSize += ClassSize.estimateBase(aSlice.getClass(), false); } }
From source file:com.mellanox.jxio.Msg.java
private ByteBuffer createSubBuffer(int position, int limit, ByteBuffer buf) { ByteBuffer sub;//w w w . j av a 2s. c o m buf.position(position); buf.limit(limit); sub = buf.slice(); return sub; }
From source file:com.mellanox.jxio.MsgPool.java
/** * Constructor of MsgPool. Creates MsgPool (including allocating and RDMA registering the memory in C). * /*from w w w .j a v a 2s. c o m*/ * @param capacity * - number of msg that this pool will contain. Max is {@value MAX_CAPACITY} * @param inSize * - size (in bytes) of the receive buffer. For client this will be the response from the server * and for the server this will be the request from the client * @param outSize * - size (in bytes) of the send buffer. For client this will be the request to the server * and for the server this will be the response to the client. * */ public MsgPool(int capacity, int inSize, int outSize) { if (LOG.isDebugEnabled()) { LOG.debug("MP CTOR entry"); } if (capacity > MsgPool.MAX_CAPACITY) { LOG.warn("Can't create pool with capacity bigger than maximum. Creating pool with capacity " + MsgPool.MAX_CAPACITY); this.capacity = MsgPool.MAX_CAPACITY; } else { this.capacity = capacity; } this.inSize = inSize; this.outSize = outSize; long refToCObjects[] = new long[this.capacity + 1]; // the first element represents the id of MsgPool buffer = Bridge.createMsgPool(this.capacity, inSize, outSize, refToCObjects); if (buffer == null) { LOG.fatal("there was an error creating the MsgPool"); refToCObject = 0; return; // TODO: throw exception } refToCObject = refToCObjects[0]; int msgBufferSize = inSize + outSize; for (int i = 0; i < this.capacity; i++) { buffer.position(msgBufferSize * i); ByteBuffer partialBuffer = buffer.slice(); partialBuffer.limit(msgBufferSize); Msg m = new Msg(partialBuffer, inSize, outSize, refToCObjects[i + 1], this); listMsg.add(m); } if (LOG.isDebugEnabled()) { LOG.debug(this.toLogString() + "MP CTOR done"); } }
From source file:org.apache.hadoop.io.crypto.aes.NativeOpensslAESCipher.java
/** * input and output are direct buffer for speed improvement. * //from w w w . j av a 2 s. c om * @param input * @param inputLength * @param output * @return the output length * @throws Exception when error happens */ @Override public int doFinal(ByteBuffer input, int inputLength, ByteBuffer output) throws Exception { if (!inited) { throw new Exception("Cipher not inited, please call init()"); } output.clear(); output.limit(0); int length = doFinal(context, input, inputLength, output); if (0 == length) { throw new Exception("Decompressed length is 0!!!"); } output.limit(length); return length; }
From source file:org.accelio.jxio.MsgPool.java
/** * Constructor of MsgPool. Creates MsgPool (including allocating and RDMA registering the memory in C). * /*from w w w . java2 s .co m*/ * @param capacity * - number of msg that this pool will contain. Max is {@value MAX_CAPACITY} * @param inSize * - size (in bytes) of the receive buffer. For client this will be the response from the server * and for the server this will be the request from the client * @param outSize * - size (in bytes) of the send buffer. For client this will be the request to the server * and for the server this will be the response to the client. * */ public MsgPool(int capacity, int inSize, int outSize) { if (LOG.isDebugEnabled()) { LOG.debug("MP CTOR entry"); } if (capacity > MsgPool.MAX_CAPACITY) { LOG.warn("Can't create pool with capacity bigger than maximum. Creating pool with capacity " + MsgPool.MAX_CAPACITY); this.capacity = MsgPool.MAX_CAPACITY; } else { this.capacity = capacity; } this.inSize = inSize; this.outSize = outSize; long refToCObjects[] = new long[this.capacity + 1]; // the first element represents the id of MsgPool buffer = Bridge.createMsgPool(this.capacity, inSize, outSize, refToCObjects); if (buffer == null) { LOG.fatal("there was an error creating the MsgPool. Capacity=" + this.capacity + ", inSize=" + inSize + ", outSize=" + outSize); refToCObject = 0; return; // TODO: throw exception } refToCObject = refToCObjects[0]; int msgBufferSize = inSize + outSize; for (int i = 0; i < this.capacity; i++) { buffer.position(msgBufferSize * i); ByteBuffer partialBuffer = buffer.slice(); partialBuffer.limit(msgBufferSize); Msg m = new Msg(partialBuffer, inSize, outSize, refToCObjects[i + 1], this); listMsg.add(m); } if (LOG.isDebugEnabled()) { LOG.debug(this.toLogString() + "MP CTOR done"); } }