List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { if (b.length == 0) { return;/*ww w . j a va2 s . co m*/ } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { minEpoch = Varint.readUnsignedVarLong(byteBuf); int length = Varint.readUnsignedVarInt(byteBuf); reservoir = new int[length]; for (int i = 0; i < reservoir.length; i++) { reservoir[i] = Varint.readUnsignedVarInt(byteBuf); } } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.data.tree.prop.DataTime.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try {// w ww .jav a2 s. c o m first = Varint.readUnsignedVarLong(byteBuf); last = first + Varint.readUnsignedVarLong(byteBuf); } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.data.util.ConcurrentKeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { if (b.length == 0) { map = new ConcurrentHashMapV8<>(16, 0.75f, 4); return;// w w w . j a va2 s.co m } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { int mapSize = Varint.readUnsignedVarInt(byteBuf); int i; try { if (mapSize > 0) { map = new ConcurrentHashMapV8<>(mapSize + 16, 0.75f, 4); for (i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); } } else { map = new ConcurrentHashMapV8<>(16, 0.75f, 4); } } catch (Exception e) { throw new RuntimeException(e); } } finally { byteBuf.release(); } postDecode(); }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { map = new HashMap<>(); errors = null;/*from www . ja va 2 s .co m*/ if (b.length == 0) { return; } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { byte marker = byteBuf.getByte(byteBuf.readerIndex()); if (marker == 0) { errors = new HashMap<>(); // Consume the sentinel byte value byteBuf.readByte(); } int mapSize = Varint.readUnsignedVarInt(byteBuf); try { if (mapSize > 0) { for (int i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); if (hasErrors()) { long error = Varint.readUnsignedVarLong(byteBuf); if (error != 0) { errors.put(k, error); } } } } } catch (Exception e) { throw Throwables.propagate(e); } } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.store.skiplist.Page.java
License:Apache License
public void decode(byte[] page) { parent.numPagesDecoded.getAndIncrement(); ByteBuf buffer = Unpooled.wrappedBuffer(page); try {//from w w w. j ava 2 s . co m InputStream in = new ByteBufInputStream(buffer); int flags = in.read() & 0xff; int gztype = flags & 0x0f; boolean isSparse = (flags & FLAGS_IS_SPARSE) != 0; boolean hasEstimates = (flags & FLAGS_HAS_ESTIMATES) != 0; int readEstimateTotal, readEstimates; switch (gztype) { case 1: in = new InflaterInputStream(in); break; case 2: in = new GZIPInputStream(in); break; case 3: in = new LZFInputStream(in); break; case 4: in = new SnappyInputStream(in); break; } K firstKey; byte[] nextFirstKey; if (isSparse) { encodeType = KeyCoder.EncodeType.SPARSE; DataInputStream dis = new DataInputStream(in); int entries = Varint.readUnsignedVarInt(dis); firstKey = keyCoder.keyDecode(Bytes.readBytes(in, Varint.readUnsignedVarInt(dis))); int nextFirstKeyLength = Varint.readUnsignedVarInt(dis); if (nextFirstKeyLength > 0) { nextFirstKey = Bytes.readBytes(in, nextFirstKeyLength); } else { nextFirstKey = null; } int bytes = 0; size = entries; keys = new ArrayList<>(size); values = new ArrayList<>(size); rawValues = new ArrayList<>(size); for (int i = 0; i < entries; i++) { byte kb[] = Bytes.readBytes(in, Varint.readUnsignedVarInt(dis)); byte vb[] = Bytes.readBytes(in, Varint.readUnsignedVarInt(dis)); bytes += kb.length + vb.length; keys.add(keyCoder.keyDecode(kb)); values.add(null); rawValues.add(vb); } if (hasEstimates) { readEstimateTotal = Varint.readUnsignedVarInt(dis); readEstimates = Varint.readUnsignedVarInt(dis); setAverage(readEstimateTotal, readEstimates); } else { /** use a pessimistic/conservative byte/entry estimate */ setAverage(bytes * estimateMissingFactor, entries); } } else { encodeType = KeyCoder.EncodeType.LEGACY; int entries = (int) Bytes.readLength(in); firstKey = keyCoder.keyDecode(Bytes.readBytes(in)); nextFirstKey = Bytes.readBytes(in); int bytes = 0; size = entries; keys = new ArrayList<>(size); values = new ArrayList<>(size); rawValues = new ArrayList<>(size); for (int i = 0; i < entries; i++) { byte kb[] = Bytes.readBytes(in); byte vb[] = Bytes.readBytes(in); bytes += kb.length + vb.length; keys.add(keyCoder.keyDecode(kb)); values.add(null); rawValues.add(vb); } if (hasEstimates) { readEstimateTotal = (int) Bytes.readLength(in); readEstimates = (int) Bytes.readLength(in); setAverage(readEstimateTotal, readEstimates); } else { /** use a pessimistic/conservative byte/entry estimate */ setAverage(bytes * estimateMissingFactor, entries); } } updateMemoryEstimate(); assert (this.firstKey.equals(firstKey)); this.nextFirstKey = keyCoder.keyDecode(nextFirstKey); in.close(); } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } finally { buffer.release(); } }
From source file:com.addthis.hydra.task.source.Mark.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf buffer = Unpooled.wrappedBuffer(b); try {//w w w .j ava2 s. c o m int valLength = Varint.readUnsignedVarInt(buffer); byte[] valBytes = new byte[valLength]; buffer.readBytes(valBytes); setValue(new String(valBytes)); setIndex(Varint.readUnsignedVarLong(buffer)); setEnd(buffer.readByte() == 1); setError(Varint.readUnsignedVarInt(buffer)); } finally { buffer.release(); } }
From source file:com.addthis.hydra.task.source.SimpleMark.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf buffer = Unpooled.wrappedBuffer(b); try {//from w w w .ja v a 2 s . co m int valLength = Varint.readUnsignedVarInt(buffer); byte[] valBytes = new byte[valLength]; buffer.readBytes(valBytes); val = new String(valBytes); index = Varint.readUnsignedVarLong(buffer); end = buffer.readByte() == 1; } finally { buffer.release(); } }
From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { // Handle a bad request. if (!req.getDecoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;// w ww . j a v a 2s. co m } // Allow only GET methods. if (req.getMethod() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } if ("/".equals(req.getUri()) || "/favicon.ico".equals(req.getUri())) { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); sendHttpResponse(ctx, req, res); return; } if (req.getUri().startsWith("/livereload.js")) { InputStream is = getClass().getResourceAsStream("/livereload.js"); byte[] data = IOUtils.toByteArray(is); ByteBuf content = Unpooled.wrappedBuffer(data); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(CONTENT_TYPE, "application/javascript"); setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } }
From source file:com.alibaba.dubbo.qos.server.handler.HttpProcessHandler.java
License:Apache License
private static final FullHttpResponse http_200(String result) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
From source file:com.alibaba.dubbo.qos.server.handler.HttpProcessHandler.java
License:Apache License
private static final FullHttpResponse http_500(String errorMessage) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(errorMessage.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }