List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:Main.java
/** * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted * as the length of the buffer./*from w w w . j ava2 s . c o m*/ * <p> * When the method returns, {@code data.position()} will contain the new length of the buffer. If * the buffer is not empty it is guaranteed to start with an SPS. * * @param data Buffer containing start code delimited NAL units. */ public static void discardToSps(ByteBuffer data) { int length = data.position(); int consecutiveZeros = 0; int offset = 0; while (offset + 1 < length) { int value = data.get(offset) & 0xFF; if (consecutiveZeros == 3) { if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) { // Copy from this NAL unit onwards to the start of the buffer. ByteBuffer offsetData = data.duplicate(); offsetData.position(offset - 3); offsetData.limit(length); data.position(0); data.put(offsetData); return; } } else if (value == 0) { consecutiveZeros++; } if (value != 0) { consecutiveZeros = 0; } offset++; } // Empty the buffer if the SPS NAL unit was not found. data.clear(); }
From source file:hivemall.mf.BPRMatrixFactorizationUDTF.java
private static void writeBuffer(@Nonnull final ByteBuffer srcBuf, @Nonnull final NioFixedSegment dst, final long lastWritePos) throws HiveException { // TODO asynchronous write in the background srcBuf.flip();/*from w w w . j a v a2 s . c om*/ try { dst.writeRecords(lastWritePos, srcBuf); } catch (IOException e) { throw new HiveException("Exception causes while writing records to : " + lastWritePos, e); } srcBuf.clear(); }
From source file:org.geoserver.rest.util.IOUtils.java
/** * Copies the content of the source channel onto the destination channel. * //www . j a v a 2s . c o m * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination) throws IOException { inputNotNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize); while (source.read(buffer) != -1) { //prepare the buffer for draining buffer.flip(); //write to destination while (buffer.hasRemaining()) destination.write(buffer); //clear buffer.clear(); } }
From source file:org.quickserver.util.pool.ByteBufferObjectFactory.java
public void passivateObject(Object obj) { ByteBuffer ch = (ByteBuffer) obj; ch.clear(); }
From source file:org.siddhiesb.transport.passthru.util.BufferFactory.java
public ByteBuffer getBuffer() { if (marker == -1) { // System.out.println("allocating marker -1"); return allocator.allocate(bufferSize); } else {/*from ww w . ja v a2s . c o m*/ try { lock.lock(); if (marker >= 0) { // System.out.println("Returning buffer"); ByteBuffer b = buffers[marker]; b.clear(); buffers[marker] = null; marker--; return b; } } finally { lock.unlock(); } } return allocator.allocate(bufferSize); }
From source file:org.apache.hadoop.hbase.io.hfile.slab.Slab.java
ByteBuffer alloc(int bufferSize) throws InterruptedException { int newCapacity = Preconditions.checkPositionIndex(bufferSize, blockSize); ByteBuffer returnedBuffer = buffers.take(); returnedBuffer.clear().limit(newCapacity); return returnedBuffer; }
From source file:it.unimi.di.big.mg4j.index.DiskBasedIndex.java
/** Commodity method for loading from a channel a big list of binary longs with specified endianness into a {@linkplain LongBigArrays long big array}. * /*from w w w.j a v a 2 s. c om*/ * @param channel the channel. * @param byteOrder the endianness of the longs. * @return a big list of longs containing the longs returned by <code>channel</code>. */ public static LongBigArrayBigList loadLongBigList(final ReadableByteChannel channel, final long length, final ByteOrder byteOrder) throws IOException { final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(byteOrder); LongBigArrayBigList list = new LongBigArrayBigList(length); while (channel.read(byteBuffer) > 0) { byteBuffer.flip(); while (byteBuffer.hasRemaining()) list.add(byteBuffer.getLong()); byteBuffer.clear(); } return list; }
From source file:org.jtrfp.trcl.core.Texture.java
public static ByteBuffer RGBA8FromPNG(BufferedImage image, int startX, int startY, int sizeX, int sizeY) { //int color;//from w ww . j a v a2s . c o m ByteBuffer buf = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 4); final int[] row = new int[image.getWidth()]; for (int y = startY; y < startY + sizeY; y++) { image.getRGB(0, y, image.getWidth(), 1, row, 0, image.getWidth()); for (int color : row) { buf.put((byte) ((color & 0x00FF0000) >> 16)); buf.put((byte) ((color & 0x0000FF00) >> 8)); buf.put((byte) (color & 0x000000FF)); buf.put((byte) ((color & 0xFF000000) >> 24)); } // end for(x) } // end for(y) buf.clear();// Rewind return buf; }
From source file:org.apache.hama.monitor.fd.UDPSensor.java
/** * The heartbeat function, signifying its existence. *//* ww w .j ava2s . c o m*/ @Override public void heartbeat() throws IOException { ByteBuffer heartbeat = ByteBuffer.allocate(8); heartbeat.clear(); heartbeat.putLong(sequence.incrementAndGet()); heartbeat.flip(); channel.send(heartbeat, new InetSocketAddress(this.host, this.port)); if (LOG.isDebugEnabled()) { LOG.debug("Heartbeat sequence " + sequence.get() + " is sent to " + this.host + ":" + this.port); } }
From source file:org.gephi.io.importer.api.ImportUtils.java
/** * Uncompress a GZIP file.//from ww w . j a v a 2 s .c o m */ public 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; }