List of usage examples for io.netty.buffer ByteBuf hasArray
public abstract boolean hasArray();
From source file:com.difference.historybook.proxy.littleproxy.LittleProxyResponse.java
License:Apache License
@Override public byte[] getContent() { ByteBuf buf = response.content(); byte[] bytes; int length = buf.readableBytes(); if (buf.hasArray()) { bytes = buf.array();//w w w . j a v a 2 s .co m } else { bytes = new byte[length]; buf.getBytes(buf.readerIndex(), bytes); } buf.release(); return bytes; }
From source file:com.github.milenkovicm.kafka.protocol.Convert.java
License:Apache License
public static String decodeString(ByteBuf buf) { int readable = decodeShort(buf); // N => int16 ByteBuf bytes = buf.readBytes(readable); // content if (bytes.hasArray()) { return new String(bytes.array(), DEFAULT_CHARSET); } else {//from ww w. j a v a2s .c om byte[] array = new byte[readable]; bytes.readBytes(array); return new String(array, DEFAULT_CHARSET); } }
From source file:com.linecorp.armeria.server.thrift.TByteBufTransport.java
License:Apache License
@Nullable @Override//from w w w. j a va 2s. c o m public byte[] getBuffer() { final ByteBuf buf = this.buf; if (!buf.hasArray()) { return null; } else { return buf.array(); } }
From source file:com.linecorp.armeria.server.thrift.TByteBufTransport.java
License:Apache License
@Override public int getBufferPosition() { final ByteBuf buf = this.buf; if (!buf.hasArray()) { return 0; } else {/* w ww . j a va2 s . co m*/ return buf.arrayOffset() + buf.readerIndex(); } }
From source file:com.linecorp.armeria.server.thrift.TByteBufTransport.java
License:Apache License
@Override public int getBytesRemainingInBuffer() { final ByteBuf buf = this.buf; if (buf.hasArray()) { return buf.readableBytes(); } else {//www.j a v a2s .c om return -1; } }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameDecoder.java
License:Apache License
private ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf frame) throws Exception { int readableBytes = frame.readableBytes(); if (frame.hasArray()) { inflater.setInput(frame.array(), 0, readableBytes); } else {//from w w w .j a v a 2 s .c o m byte[] array = new byte[frame.readableBytes()]; frame.getBytes(0, array); inflater.setInput(array); } int totalLength = 0; List<ByteBuf> all = new LinkedList<ByteBuf>(); int multiplier = 2; alldone: while (true) { int maxOutputLength = inflater.getRemaining() * multiplier; // multiplier keeps increasing, so we will keep picking // larger and larger buffers the more times we have to loop // around, i.e., the more we realize that the data was very // heavily compressed, the larger our buffers are going to be. multiplier += 1; ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); while (!inflater.needsInput()) { byte[] outArray = decompressed.array(); int outIndex = decompressed.arrayOffset() + decompressed.writerIndex(); int length = outArray.length - outIndex; if (length == 0) break; try { //LOG.debug("here1"); int outputLength = inflater.inflate(outArray, outIndex, length); totalLength += outputLength; //LOG.debug("here2"); if (outputLength > 0) decompressed.writerIndex(decompressed.writerIndex() + outputLength); } catch (DataFormatException e) { throw new Exception("Could not inflate" + e.getMessage()); } if (inflater.finished()) { all.add(decompressed); break alldone; } } all.add(decompressed); } inflater.reset(); if (all.size() == 1) return all.get(0); else { ByteBuf allData = ctx.alloc().heapBuffer(totalLength); for (ByteBuf b : all) { //LOG.debug("capacity=" + allData.capacity()); allData.writeBytes(b); b.release(); } return allData; } }
From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java
License:Apache License
/** * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library * which is faster as it computes using hardware machine instruction else it computes using crc32c algo. * * @param payload//from w w w.j a va 2s .c om * @return */ public static int computeChecksum(ByteBuf payload) { if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { return CRC32C_HASH.calculate(payload.nioBuffer()); } }
From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java
License:Apache License
/** * Computes incremental checksum with input previousChecksum and input payload * * @param previousChecksum : previously computed checksum * @param payload/*from w w w .java 2 s.c o m*/ * @return */ public static int resumeChecksum(int previousChecksum, ByteBuf payload) { if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { return CRC32C_HASH.resume(previousChecksum, payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer()); } }
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library * which is faster as it computes using hardware machine instruction else it computes using crc32c algo. * * @param payload//from w w w. ja v a 2 s . c o m * @return */ public static long computeChecksum(ByteBuf payload) { int crc; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.calculate(payload.nioBuffer()); } return crc & 0xffffffffL; }
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes incremental checksum with input previousChecksum and input payload * * @param previousChecksum : previously computed checksum * @param payload//from ww w .java 2s . c o m * @return */ public static long resumeChecksum(long previousChecksum, ByteBuf payload) { int crc = (int) previousChecksum; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.resume(crc, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.resume(crc, payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.resume(crc, payload.nioBuffer()); } return crc & 0xffffffffL; }