List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:se.sics.ktoolbox.cc.heartbeat.util.CCValueFactory.java
License:Open Source License
public static byte[] getHeartbeatValue(KAddress src) { ByteBuf buf = Unpooled.buffer(); Serializers.toBinary(src, buf);// ww w. j a va 2 s . c o m return Arrays.copyOf(buf.array(), buf.readableBytes()); }
From source file:se.sics.ktoolbox.videostream.VideoStreamMngrImpl.java
License:Open Source License
@Override public synchronized byte[] getContent(long readPos, long endPos) { LOG.debug("getting content from readPos:{} to endPos:{}", readPos, endPos); ByteBuf buf = Unpooled.buffer(); int pieceIdx = (int) (readPos / pieceSize); int startOffset = (int) (readPos % pieceSize); int endPieceIdx = (int) (endPos / pieceSize); int endOffset = (int) (endPos % pieceSize); if (endOffset == 0) { endPieceIdx--;/*from www . j a v a 2 s . c om*/ } int restToSend = piecesToSend; if (!fm.hasPiece(pieceIdx)) { playPos.set(pieceIdx); } int currWaitTime = 0; while (!fm.hasPiece(pieceIdx)) { try { Thread.sleep(1000); } catch (InterruptedException ex) { LOG.error("error while waiting for pieces"); System.exit(1); } currWaitTime++; if (currWaitTime > waitTime) { LOG.warn("problem retrieving pieces"); return new byte[] {}; } } while (fm.hasPiece(pieceIdx) && pieceIdx < endPieceIdx && restToSend > 0) { ByteBuffer piece = fm.readPiece(null, pieceIdx, (Set) new HashSet<Integer>()); buf.writeBytes(piece.array(), startOffset, piece.array().length - startOffset); LOG.debug("sending piece:{} total:{}", new Object[] { pieceIdx, buf.writerIndex() }); pieceIdx++; restToSend--; startOffset = 0; } if (pieceIdx == endPieceIdx && readPos / pieceSize == pieceIdx) { //if start piece = end piece if (fm.hasPiece(pieceIdx)) { if (endOffset == 0) { ByteBuffer piece = fm.readPiece(null, pieceIdx, null); int pieceStartOffset = startOffset; int pieceLength = piece.array().length - pieceStartOffset; buf.writeBytes(piece.array(), pieceStartOffset, piece.array().length); LOG.debug("sending part of piece:{} startOff:{} endOff:{} total: {}", new Object[] { pieceIdx, pieceStartOffset, pieceLength, buf.writerIndex() }); } else { ByteBuffer piece = fm.readPiece(null, pieceIdx, null); int pieceStartOffset = startOffset; int pieceLength = (endOffset < piece.array().length ? endOffset + 1 : piece.array().length) - startOffset; buf.writeBytes(piece.array(), pieceStartOffset, pieceLength); LOG.debug("sending part of piece:{} startOff:{} endOff:{} total: {}", new Object[] { pieceIdx, pieceStartOffset, pieceLength, buf.writerIndex() }); } } } else if (pieceIdx == endPieceIdx) { //if need to write part of end piece if (fm.hasPiece(pieceIdx)) { if (endOffset == 0) { ByteBuffer piece = fm.readPiece(null, pieceIdx, null); int pieceStartOffset = 0; int pieceLength = piece.array().length; buf.writeBytes(piece); LOG.debug("sending part of piece:{} startOff:{} endOff:{} total: {}", new Object[] { pieceIdx, pieceStartOffset, pieceLength, buf.writerIndex() }); } else { ByteBuffer piece = fm.readPiece(null, pieceIdx, null); int pieceStartOffset = 0; int pieceLength = (endOffset < piece.array().length ? endOffset + 1 : piece.array().length); buf.writeBytes(piece.array(), pieceStartOffset, pieceLength); LOG.debug("sending part of piece:{} startOff:{} endOff:{} total: {}", new Object[] { pieceIdx, pieceStartOffset, pieceLength, buf.writerIndex() }); } } } byte[] ret = new byte[buf.writerIndex()]; System.arraycopy(buf.array(), 0, ret, 0, buf.writerIndex()); return ret; }
From source file:storm.net.codec.impl.BerLongLongEncoder.java
License:Open Source License
/** * Decodes the data from within the byte buffer. * * @param ctx Encoding context for recursive decoding. * @param buf The buffer which houses the source data. */// w ww . j ava 2 s .c om @Override protected Object decodeData(BerEncodingContext ctx, ByteBuf buf) { return new BigInteger(buf.array()); }
From source file:storm.net.codec.impl.BerStringEncoder.java
License:Open Source License
/** * Decodes the data from within the byte buffer. * * @param ctx Encoding context for recursive decoding. * @param buf The buffer which houses the source data. *///from w ww.j ava2 s . c om @Override protected Object decodeData(BerEncodingContext ctx, ByteBuf buf) { final byte[] data = buf.array(); return new String(data); }
From source file:test.net.hasor.rsf.functions.ProtocolTest.java
License:Apache License
@Test public void requestPack() throws IOException { DefaultRsfEnvironment rsfEnv = new DefaultRsfEnvironment(Hasor.createAppContext().getEnvironment()); CodecAdapterForV1 codecAdapter = new CodecAdapterForV1(rsfEnv); ///*from ww w .j av a 2 s .c om*/ // RequestInfo outRequest = new RequestInfo(); outRequest.setMessage(true); outRequest.setClientTimeout(1000); outRequest.setReceiveTime(System.nanoTime()); outRequest.setRequestID(System.currentTimeMillis()); outRequest.setSerializeType("json"); outRequest.setServiceGroup("Test"); outRequest.setServiceName("java.util.List"); outRequest.setServiceVersion("1.0.0"); outRequest.setTargetMethod("add"); outRequest.addParameter("java.lang.Object", "aaaa".getBytes(), null); // ByteBuf outBuf = ByteBufAllocator.DEFAULT.heapBuffer(); codecAdapter.wirteRequestBlock(codecAdapter.buildRequestBlock(outRequest), outBuf); byte[] datas = outBuf.array(); // // ByteBuf inBuf = ByteBufAllocator.DEFAULT.heapBuffer(); inBuf.writeBytes(datas); RequestInfo inRequest = codecAdapter.readRequestInfo(inBuf); // System.out.println(inRequest); }
From source file:test.net.hasor.rsf.functions.ProtocolTest.java
License:Apache License
@Test public void responsePack() throws IOException { DefaultRsfEnvironment rsfEnv = new DefaultRsfEnvironment(Hasor.createAppContext().getEnvironment()); CodecAdapterForV1 codecAdapter = new CodecAdapterForV1(rsfEnv); ////from www . jav a 2s. c om ResponseInfo outResponse = new ResponseInfo(); outResponse.setSerializeType("json"); outResponse.setRequestID(System.currentTimeMillis()); outResponse.setReceiveTime(System.currentTimeMillis()); outResponse.setReturnData("ok".getBytes()); outResponse.setStatus((short) 200); // ByteBuf outBuf = ByteBufAllocator.DEFAULT.heapBuffer(); codecAdapter.wirteResponseBlock(codecAdapter.buildResponseBlock(outResponse), outBuf); byte[] datas = outBuf.array(); // // ByteBuf inBuf = ByteBufAllocator.DEFAULT.heapBuffer(); inBuf.writeBytes(datas); ResponseInfo inResponse = codecAdapter.readResponseInfo(inBuf); // System.out.println(inResponse); }
From source file:tp.MyJZLibDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable()) { return;/*w w w . ja v a2 s . c o m*/ } try { // Configure input. int inputLength = in.readableBytes(); //totalBytes += inputLength; z.avail_in = inputLength; if (in.hasArray()) { z.next_in = in.array(); z.next_in_index = in.arrayOffset() + in.readerIndex(); } else { byte[] array = new byte[inputLength]; in.getBytes(in.readerIndex(), array); z.next_in = array; z.next_in_index = 0; } int oldNextInIndex = z.next_in_index; // Configure output. int maxOutputLength = inputLength << 1; ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); try { loop: for (;;) { z.avail_out = maxOutputLength; decompressed.ensureWritable(maxOutputLength); z.next_out = decompressed.array(); z.next_out_index = decompressed.arrayOffset() + decompressed.writerIndex(); int oldNextOutIndex = z.next_out_index; // Decompress 'in' into 'out' int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH); int outputLength = z.next_out_index - oldNextOutIndex; if (outputLength > 0) { decompressed.writerIndex(decompressed.writerIndex() + outputLength); } switch (resultCode) { case JZlib.Z_NEED_DICT: if (dictionary == null) { ZlibUtil.fail(z, "decompression failure", resultCode); } else { resultCode = z.inflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode); } } break; case JZlib.Z_STREAM_END: finished = true; // Do not decode anymore. z.inflateEnd(); break loop; case JZlib.Z_OK: break; case JZlib.Z_BUF_ERROR: if (z.avail_in <= 0) { //ZlibUtil.fail(z, "decompression failure [Z_BUF_ERROR] ", resultCode); break loop; } break; default: ZlibUtil.fail(z, "decompression failure", resultCode); } } } finally { in.skipBytes(z.next_in_index - oldNextInIndex); //System.err.println("[recived][numBytes] = "+inputLength); if (decompressed.isReadable()) { out.add(decompressed); } else { decompressed.release(); } } } finally { // Deference the external references explicitly to tell the VM that // the allocated byte arrays are temporary so that the call stack // can be utilized. // I'm not sure if the modern VMs do this optimization though. z.next_in = null; z.next_out = null; } }
From source file:uk.co.thinkofdeath.prismarine.network.CipherCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { byte[] data;/* ww w . j av a 2 s . c om*/ int offset = 0; int dataSize; if (!msg.isDirect()) { data = msg.array(); offset = msg.arrayOffset(); dataSize = msg.readableBytes(); msg.skipBytes(msg.readableBytes()); } else { dataSize = msg.readableBytes(); if (dataBuffer.length < dataSize) { dataBuffer = new byte[dataSize]; } msg.readBytes(dataBuffer, 0, dataSize); data = dataBuffer; } int size = cipherEncrypt.getOutputSize(msg.readableBytes()); if (encryptBuffer.length < size) { encryptBuffer = new byte[size]; } int count = cipherEncrypt.update(data, offset, dataSize, encryptBuffer); out.writeBytes(encryptBuffer, 0, count); }
From source file:uk.co.thinkofdeath.prismarine.network.CipherCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { byte[] data;/*w ww . j a v a2 s . c o m*/ int offset = 0; int dataSize = in.readableBytes(); if (!in.isDirect()) { data = in.array(); offset = in.arrayOffset(); in.skipBytes(in.readableBytes()); } else { if (deDataBuffer.length < dataSize) { deDataBuffer = new byte[dataSize]; } in.readBytes(deDataBuffer, 0, dataSize); data = deDataBuffer; } int size = cipherDecrypt.getOutputSize(dataSize); ByteBuf buf = ctx.alloc().heapBuffer(size); buf.writerIndex(cipherDecrypt.update(data, offset, dataSize, buf.array(), buf.arrayOffset())); out.add(buf); }
From source file:uk.co.thinkofdeath.prismarine.network.CompressionCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { MCByteBuf buf = new MCByteBuf(out); if (msg.readableBytes() < threshold) { buf.writeVarInt(0);/* w w w .j a v a2 s. c om*/ buf.writeBytes(msg); } else { CompressionInfo ci = info.get(); byte[] data; int offset = 0; int dataSize; // Handle both direct and heap buffers // heap buffers are faster in this case as they // do not require a copy if (!msg.isDirect()) { data = msg.array(); offset = msg.arrayOffset(); dataSize = msg.readableBytes(); msg.skipBytes(msg.readableBytes()); } else { dataSize = msg.readableBytes(); if (ci.dataBuffer.length < dataSize) { ci.dataBuffer = new byte[dataSize]; } msg.readBytes(ci.dataBuffer, 0, dataSize); data = ci.dataBuffer; } buf.writeVarInt(dataSize); ci.deflater.setInput(data, offset, dataSize); ci.deflater.finish(); while (!ci.deflater.finished()) { int count = ci.deflater.deflate(ci.compBuffer); out.writeBytes(ci.compBuffer, 0, count); } ci.deflater.reset(); } }