List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip(); DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close();//w ww . j av a 2s. c om DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.BINARY); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8()); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:org.apache.axis2.transport.nhttp.ServerHandler.java
/** * Process ready input by writing it into the Pipe * @param conn the connection being processed * @param decoder the content decoder in use *//*from ww w .j a v a2s. c o m*/ public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) { HttpContext context = conn.getContext(); Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(REQUEST_SINK_CHANNEL); ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER); try { while (decoder.read(inbuf) > 0) { inbuf.flip(); sink.write(inbuf); inbuf.compact(); } if (decoder.isCompleted()) { sink.close(); } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEIntReader.java
@Override public byte[] ensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip(); DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close();//from w w w .j av a 2 s.c o m DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.INT32); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = cr.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:com.facebook.infrastructure.net.UdpConnection.java
public void read(SelectionKey key) { key.interestOps(key.interestOps() & (~SelectionKey.OP_READ)); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); try {/* w ww . j a v a 2 s . co m*/ SocketAddress sa = socketChannel_.receive(buffer); if (sa == null) { logger_.debug("*** No datagram packet was available to be read ***"); return; } buffer.flip(); byte[] bytes = gobbleHeaderAndExtractBody(buffer); if (bytes.length > 0) { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); Message message = Message.serializer().deserialize(dis); if (message != null) { MessagingService.receive(message); } } } catch (IOException ioe) { logger_.warn(LogUtil.throwableToString(ioe)); } finally { key.interestOps(key_.interestOps() | SelectionKey.OP_READ); } }
From source file:Main.java
private static ByteBuffer convertImageData(BufferedImage bufferedImage) { ByteBuffer imageBuffer; WritableRaster raster;//from w ww . j av a 2 s . c o m BufferedImage texImage; // for a texture if (bufferedImage.getColorModel().hasAlpha()) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 4, null); texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>()); } else { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 3, null); texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>()); } // copy the source image into the produced image Graphics g = texImage.getGraphics(); g.setColor(new Color(0f, 0f, 0f, 0f)); g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()); g.drawImage(bufferedImage, 0, 0, null); // build a byte buffer from the temporary image // that be used by OpenGL to produce a texture. byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java
@Override public byte[] ensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip(); DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close();//ww w .java2 s . co m DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.BINARY); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8()); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEIntReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip(); DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close();/*from www. j a va 2 s.co m*/ DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.INT32); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = cr.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:net.ymate.platform.serv.nio.support.NioSession.java
public void read() throws IOException { if (__buffer == null) { __buffer = ByteBufferBuilder.allocate(__eventGroup.bufferSize()); }/*from w w w .ja va 2 s . c o m*/ ByteBuffer _data = ByteBuffer.allocate(__eventGroup.bufferSize()); int _len = 0; while ((_len = __doChannelRead(_data)) > 0) { _data.flip(); __buffer.append(_data.array(), _data.position(), _data.remaining()); _data.clear(); } if (_len < 0) { close(); return; } ByteBufferBuilder _copiedBuffer = __buffer.duplicate().flip(); while (true) { _copiedBuffer.mark(); Object _message = null; if (_copiedBuffer.remaining() > 0) { _message = __eventGroup.codec().decode(_copiedBuffer); } else { _message = null; } if (_message == null) { _copiedBuffer.reset(); __doBufferReset(_copiedBuffer); break; } else { final Object _copiedObj = _message; __eventGroup.executorService().submit(new Runnable() { public void run() { try { __eventGroup.listener().onMessageReceived(_copiedObj, NioSession.this); } catch (IOException e) { try { __eventGroup.listener().onExceptionCaught(e, NioSession.this); } catch (IOException ex) { try { close(); } catch (Throwable exx) { _LOG.error(exx.getMessage(), RuntimeUtils.unwrapThrow(exx)); } } } } }); } } }
From source file:org.apache.nifi.io.nio.AbstractChannelReader.java
@Override public final void run() { if (!key.isValid() || consumer.isConsumerFinished()) { closeStream();// w w w. j a va 2 s . c o m return; } if (!key.isReadable()) { return;//there is nothing available to read...or we aren't allow to read due to throttling } ByteBuffer buffer = null; try { buffer = bufferPool.poll(); if (buffer == null) { return; // no buffers available - come back later } final int bytesRead = fillBuffer(key, buffer); buffer.flip(); if (buffer.remaining() > 0) { consumer.addFilledBuffer(buffer); buffer = null; //clear the reference - is now the consumer's responsibility } else { buffer.clear(); bufferPool.returnBuffer(buffer, 0); buffer = null; //clear the reference - is now back to the queue } if (bytesRead < 0) { //we've reached the end closeStream(); } } catch (final Exception ioe) { closeStream(); LOGGER.error("Closed channel reader " + this + " due to " + ioe); } finally { if (buffer != null) { buffer.clear(); bufferPool.returnBuffer(buffer, 0); } } }
From source file:org.lispmob.noroot.IPC.java
public void run() { int len = 0;/* www .ja va2 s .c om*/ ByteBuffer buf = ByteBuffer.allocate(9000); while (!ipc_thread.isInterrupted()) { buf.clear(); try { len = ipc_channel.read(buf); if (len == 0) { continue; } buf.flip(); String json_str = EncodingUtils.getString(buf.array(), "utf8"); JSONObject jObj = new JSONObject(json_str); int ipc_type = jObj.getInt("type"); System.out.println("LISPmob: Received IPC message: " + ipc_type); switch (ipc_type) { case IPC_LOG_MSG: LISPmobVPNService.err_msg_code = jObj.getInt("err_msg_code"); Thread.sleep(1000); if (LISPmobVPNService.err_msg_code != 0) { /* If LISPmob is not the active windows, the error msg code is not clean * and we send a notification of the error */ Resources res = vpn_service.getResources(); String[] err_msg = res.getStringArray(R.array.ErrMsgArray); String msg = err_msg[LISPmobVPNService.err_msg_code]; //notifications.notify_msg( msg); } break; case IPC_PROTECT_SOCKS: int socket = jObj.getInt("socket"); if (socket != -1) { boolean sock_protect = false; int retry = 0; while (!sock_protect && retry < 30) { if (!vpn_service.protect(socket)) { retry++; Thread.sleep(200); } else { sock_protect = true; System.out.println( "LISPmob: The socket " + socket + " has been protected (VPN Service)"); } } } break; default: System.out.println("***** Unknown IPC message: " + ipc_type); break; } } catch (Exception e) { e.printStackTrace(); } } }