Example usage for io.netty.buffer ByteBuf clear

List of usage examples for io.netty.buffer ByteBuf clear

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf clear.

Prototype

public abstract ByteBuf clear();

Source Link

Document

Sets the readerIndex and writerIndex of this buffer to 0 .

Usage

From source file:com.addthis.hydra.store.skiplist.Page.java

License:Apache License

public byte[] encode(ByteBufOutputStream out, boolean record) {
    SkipListCacheMetrics metrics = parent.metrics;
    parent.numPagesEncoded.getAndIncrement();
    try {/*from w  ww .  j  ava  2 s.  co  m*/
        OutputStream os = out;
        out.write(gztype | FLAGS_HAS_ESTIMATES | FLAGS_IS_SPARSE);
        switch (gztype) {
        case 0:
            break;
        case 1:
            os = new DeflaterOutputStream(out, new Deflater(gzlevel));
            break;
        case 2:
            os = new GZOut(out, gzbuf, gzlevel);
            break;
        case 3:
            os = new LZFOutputStream(out);
            break;
        case 4:
            os = new SnappyOutputStream(out);
            break;
        default:
            throw new RuntimeException("invalid gztype: " + gztype);
        }

        DataOutputStream dos = new DataOutputStream(os);
        byte[] firstKeyEncoded = keyCoder.keyEncode(firstKey);
        byte[] nextFirstKeyEncoded = keyCoder.keyEncode(nextFirstKey);

        updateHistogram(metrics.encodeNextFirstKeySize, nextFirstKeyEncoded.length, record);

        Varint.writeUnsignedVarInt(size, dos);
        Varint.writeUnsignedVarInt(firstKeyEncoded.length, dos);
        dos.write(firstKeyEncoded);
        Varint.writeUnsignedVarInt(nextFirstKeyEncoded.length, dos);
        if (nextFirstKeyEncoded.length > 0) {
            dos.write(nextFirstKeyEncoded);
        }
        for (int i = 0; i < size; i++) {
            byte[] keyEncoded = keyCoder.keyEncode(keys.get(i));
            byte[] rawVal = rawValues.get(i);

            if (rawVal == null || encodeType != KeyCoder.EncodeType.SPARSE) {
                fetchValue(i);
                rawVal = keyCoder.valueEncode(values.get(i), KeyCoder.EncodeType.SPARSE);
            }

            updateHistogram(metrics.encodeKeySize, keyEncoded.length, record);
            updateHistogram(metrics.encodeValueSize, rawVal.length, record);

            Varint.writeUnsignedVarInt(keyEncoded.length, dos);
            dos.write(keyEncoded);
            Varint.writeUnsignedVarInt(rawVal.length, dos);
            dos.write(rawVal);
        }

        Varint.writeUnsignedVarInt((estimateTotal > 0 ? estimateTotal : 1), dos);
        Varint.writeUnsignedVarInt((estimates > 0 ? estimates : 1), dos);
        switch (gztype) {
        case 1:
            ((DeflaterOutputStream) os).finish();
            break;
        case 2:
            ((GZOut) os).finish();
            break;
        case 4:
            os.flush();
            break;
        }
        os.flush();
        os.close();
        dos.close();

        ByteBuf buffer = out.buffer();

        byte[] returnValue = new byte[out.writtenBytes()];

        buffer.readBytes(returnValue);
        buffer.clear();
        updateHistogram(metrics.numberKeysPerPage, size, record);
        updateHistogram(metrics.encodePageSize, returnValue.length, record);
        return returnValue;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.addthis.hydra.store.skiplist.SparsePage.java

License:Apache License

public byte[] encode(ByteBufOutputStream out, boolean record) {
    SkipListCacheMetrics metrics = parent.metrics;
    parent.numPagesEncoded.getAndIncrement();
    try {/*from   w  w  w.j  av  a2 s . co m*/
        OutputStream os = out;
        out.write(gztype | FLAGS_HAS_ESTIMATES | FLAGS_IS_SPARSE);
        switch (gztype) {
        case 0:
            break;
        case 1:
            os = new DeflaterOutputStream(out, new Deflater(gzlevel));
            break;
        case 2:
            os = new GZOut(out, gzbuf, gzlevel);
            break;
        case 3:
            os = new LZFOutputStream(out);
            break;
        case 4:
            os = new SnappyOutputStream(out);
            break;
        default:
            throw new RuntimeException("invalid gztype: " + gztype);
        }

        DataOutputStream dos = new DataOutputStream(os);
        byte[] firstKeyEncoded = keyCoder.keyEncode(firstKey);
        byte[] nextFirstKeyEncoded = keyCoder.keyEncode(nextFirstKey);

        updateHistogram(metrics.encodeNextFirstKeySize, nextFirstKeyEncoded.length, record);

        Varint.writeUnsignedVarInt(size, dos);
        Varint.writeUnsignedVarInt(firstKeyEncoded.length, dos);
        dos.write(firstKeyEncoded);
        Varint.writeUnsignedVarInt(nextFirstKeyEncoded.length, dos);
        if (nextFirstKeyEncoded.length > 0) {
            dos.write(nextFirstKeyEncoded);
        }
        for (int i = 0; i < size; i++) {
            byte[] keyEncoded = keyCoder.keyEncode(keys.get(i));
            byte[] rawVal = rawValues.get(i);

            if (rawVal == null || encodeType != PageEncodeType.SPARSE) {
                fetchValue(i);
                rawVal = keyCoder.valueEncode(values.get(i), PageEncodeType.SPARSE);
            }

            updateHistogram(metrics.encodeKeySize, keyEncoded.length, record);
            updateHistogram(metrics.encodeValueSize, rawVal.length, record);

            Varint.writeUnsignedVarInt(keyEncoded.length, dos);
            dos.write(keyEncoded);
            Varint.writeUnsignedVarInt(rawVal.length, dos);
            dos.write(rawVal);
        }

        Varint.writeUnsignedVarInt((estimateTotal > 0 ? estimateTotal : 1), dos);
        Varint.writeUnsignedVarInt((estimates > 0 ? estimates : 1), dos);
        switch (gztype) {
        case 1:
            ((DeflaterOutputStream) os).finish();
            break;
        case 2:
            ((GZOut) os).finish();
            break;
        }
        os.flush(); // flush should be called by dos.close(), but better safe than sorry
        dos.close();

        ByteBuf buffer = out.buffer();

        byte[] returnValue = new byte[out.writtenBytes()];

        buffer.readBytes(returnValue);
        buffer.clear();
        updateHistogram(metrics.numberKeysPerPage, size, record);
        updateHistogram(metrics.encodePageSize, returnValue.length, record);
        return returnValue;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.cloudhopper.smpp.util.ChannelBufferUtilTest.java

License:Apache License

@Test
public void writeNullTerminatedString() throws Exception {
    ByteBuf buffer0 = Unpooled.buffer().capacity(10);

    // handle null case
    buffer0.clear();
    ChannelBufferUtil.writeNullTerminatedString(buffer0, null);
    Assert.assertArrayEquals(HexUtil.toByteArray("00"), BufferHelper.createByteArray(buffer0));

    buffer0.clear();//from www  .j  av  a2 s .c  om
    ChannelBufferUtil.writeNullTerminatedString(buffer0, "");
    Assert.assertArrayEquals(HexUtil.toByteArray("00"), BufferHelper.createByteArray(buffer0));

    buffer0.clear();
    ChannelBufferUtil.writeNullTerminatedString(buffer0, "A");
    Assert.assertArrayEquals(HexUtil.toByteArray("4100"), BufferHelper.createByteArray(buffer0));
}

From source file:com.cloudhopper.smpp.util.ChannelBufferUtilTest.java

License:Apache License

@Test
public void writeTlv() throws Exception {
    Tlv tlv0 = null;//from  w  w  w  .j  a  v  a2  s  . c om
    ByteBuf buffer0 = null;

    buffer0 = Unpooled.buffer().capacity(10);

    // handle null case
    buffer0.clear();
    ChannelBufferUtil.writeTlv(buffer0, tlv0);
    Assert.assertArrayEquals(HexUtil.toByteArray(""), BufferHelper.createByteArray(buffer0));

    buffer0.clear();
    tlv0 = new Tlv((short) 0xFFFF, new byte[] { 0x41, 0x42 });
    ChannelBufferUtil.writeTlv(buffer0, tlv0);
    Assert.assertArrayEquals(HexUtil.toByteArray("FFFF00024142"), BufferHelper.createByteArray(buffer0));
}

From source file:com.cloudhopper.smpp.util.ChannelBufferUtilTest.java

License:Apache License

@Test
public void writeAddress() throws Exception {
    Address addr0 = null;/*from  w  w  w.  jav a 2  s.co m*/
    ByteBuf buffer0 = null;

    buffer0 = Unpooled.buffer().capacity(10);

    // handle null case
    buffer0.clear();
    ChannelBufferUtil.writeAddress(buffer0, addr0);
    Assert.assertArrayEquals(HexUtil.toByteArray("000000"), BufferHelper.createByteArray(buffer0));

    // handle default address
    buffer0.clear();
    ChannelBufferUtil.writeAddress(buffer0, new Address());
    Assert.assertArrayEquals(HexUtil.toByteArray("000000"), BufferHelper.createByteArray(buffer0));

    // handle some stuff in address
    buffer0.clear();
    ChannelBufferUtil.writeAddress(buffer0, new Address((byte) 0x01, (byte) 0x02, ""));
    Assert.assertArrayEquals(HexUtil.toByteArray("010200"), BufferHelper.createByteArray(buffer0));

    buffer0.clear();
    ChannelBufferUtil.writeAddress(buffer0, new Address((byte) 0x01, (byte) 0x02, "ABC"));
    Assert.assertArrayEquals(HexUtil.toByteArray("010241424300"), BufferHelper.createByteArray(buffer0));
}

From source file:com.flysoloing.learning.network.netty.portunification.PortUnificationServerHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // Will use the first five bytes to detect a protocol.
    if (in.readableBytes() < 5) {
        return;/*from  www.  ja  v  a 2 s.co  m*/
    }

    if (isSsl(in)) {
        enableSsl(ctx);
    } else {
        final int magic1 = in.getUnsignedByte(in.readerIndex());
        final int magic2 = in.getUnsignedByte(in.readerIndex() + 1);
        if (isGzip(magic1, magic2)) {
            enableGzip(ctx);
        } else if (isHttp(magic1, magic2)) {
            switchToHttp(ctx);
        } else if (isFactorial(magic1)) {
            switchToFactorial(ctx);
        } else {
            // Unknown protocol; discard everything and close the connection.
            in.clear();
            ctx.close();
        }
    }
}

From source file:com.heliosapm.streams.metrichub.serialization.JSONChannelBufferSerializer.java

License:Apache License

/**
 * {@inheritDoc}/*  w w  w  .j a  v a2s  . c  om*/
 * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
 * FIXME: Need a way to stream the data in the ChannelBuffer as converting to a byte[] or String will scorch heap usage.
 */
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    if (value instanceof ByteBuf) {
        ByteBuf buff = (ByteBuf) value;
        System.err.println(buff.toString(UTF8_CHARSET));
        jgen.writeString(buff.toString(UTF8_CHARSET));
        buff.clear();
    } else {
        provider.defaultSerializeValue(value, jgen);
    }
}

From source file:com.heliosapm.streams.metrichub.tsdbplugin.MetricsAPIHttpPlugin.java

License:Apache License

/**
 * Unloads the UI content from a jar// w w w.j av  a 2s . co m
 * @param file The jar file
 */
protected void unloadFromJar(final File file) {
    log.info("Loading MetricsAPI UI Content from JAR: [{}]", file);
    final long startTime = System.currentTimeMillis();
    int filesLoaded = 0;
    int fileFailures = 0;
    int fileNewer = 0;
    long bytesLoaded = 0;

    JarFile jar = null;
    final ByteBuf contentBuffer = BufferManager.getInstance().directBuffer(30000);
    try {
        jar = new JarFile(file);
        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (name.startsWith(CONTENT_BASE + "/")) {
                final int contentSize = (int) entry.getSize();
                final long contentTime = entry.getTime();
                if (entry.isDirectory()) {
                    new File(metricUiContentDir, name).mkdirs();
                    continue;
                }
                File contentFile = new File(metricUiContentDir, name.replace(CONTENT_BASE + "/", ""));
                if (!contentFile.getParentFile().exists()) {
                    contentFile.getParentFile().mkdirs();
                }
                if (contentFile.exists()) {
                    if (contentFile.lastModified() >= contentTime) {
                        log.debug("File in directory was newer [{}]", name);
                        fileNewer++;
                        continue;
                    }
                    contentFile.delete();
                }
                log.debug("Writing content file [{}]", contentFile);
                contentFile.createNewFile();
                if (!contentFile.canWrite()) {
                    log.warn("Content file [{}] not writable", contentFile);
                    fileFailures++;
                    continue;
                }
                FileOutputStream fos = null;
                InputStream jis = null;
                try {
                    fos = new FileOutputStream(contentFile);
                    jis = jar.getInputStream(entry);
                    contentBuffer.writeBytes(jis, contentSize);
                    contentBuffer.readBytes(fos, contentSize);
                    fos.flush();
                    jis.close();
                    jis = null;
                    fos.close();
                    fos = null;
                    filesLoaded++;
                    bytesLoaded += contentSize;
                    log.debug("Wrote content file [{}] + with size [{}]", contentFile, contentSize);
                } finally {
                    if (jis != null)
                        try {
                            jis.close();
                        } catch (Exception ex) {
                        }
                    if (fos != null)
                        try {
                            fos.close();
                        } catch (Exception ex) {
                        }
                    contentBuffer.clear();
                }
            } // not content
        } // end of while loop
        final long elapsed = System.currentTimeMillis() - startTime;
        StringBuilder b = new StringBuilder(
                "\n\n\t===================================================\n\tMetricsAPI Content Directory:[")
                        .append(metricUiContentDir).append("]");
        b.append("\n\tTotal Files Written:").append(filesLoaded);
        b.append("\n\tTotal Bytes Written:").append(bytesLoaded);
        b.append("\n\tFile Write Failures:").append(fileFailures);
        b.append("\n\tExisting File Newer Than Content:").append(fileNewer);
        b.append("\n\tElapsed (ms):").append(elapsed);
        b.append("\n\t===================================================\n");
        log.info(b.toString());
    } catch (Exception ex) {
        log.error("Failed to export MetricsAPI content", ex);
    } finally {
        if (jar != null)
            try {
                jar.close();
            } catch (Exception x) {
                /* No Op */}
        try {
            contentBuffer.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.opentsdb.ringbuffer.RingBufferService.java

License:Apache License

/**
 * {@inheritDoc}//from   www .j  a  v a2 s  .co  m
 * @see com.lmax.disruptor.EventHandler#onEvent(java.lang.Object, long, boolean)
 */
@Override
public void onEvent(final ByteBuf event, final long sequence, final boolean endOfBatch) throws Exception {
    try {
        producer.send(new ProducerRecord<String, ByteBuf>(targetTopic, null,
                StreamedMetricValue.timestamp(event), StreamedMetricValue.metricName(event), event));
    } catch (Exception x) {
        handleEventExceptions.inc();
        log.error("Failed to handle event", x);
    } finally {
        if (endOfBatch)
            producer.flush();
        event.clear();
    }
}

From source file:com.heliosapm.tsdblite.handlers.ProtocolSwitch.java

License:Apache License

/**
 * {@inheritDoc}// w  w  w. j  a  va2 s.c  o  m
 * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    // Will use the first five bytes to detect a protocol.
    if (in.readableBytes() < 5) {
        log.info("No ProtocolSwitch. Bytes: {}", in.readableBytes());
        return;
    }
    final int magic1 = in.getUnsignedByte(in.readerIndex());
    final int magic2 = in.getUnsignedByte(in.readerIndex() + 1);
    if (detectGzip && isGzip(magic1, magic2)) {
        enableGzip(ctx);
        log.info("Enabled GZip on channel [{}]", ctx.channel().id().asShortText());
    } else if (isHttp(magic1, magic2)) {
        switchToHttp(ctx);
        log.info("Switched to HTTP on channel [{}]", ctx.channel().id().asShortText());
    } else if (isText(magic1, magic2)) {
        switchToPlainText(ctx);
        log.info("Switched to PlainText on channel [{}]", ctx.channel().id().asShortText());
    } else {
        log.error("No protocol recognized on [{}]", ctx.channel().id().asLongText());
        in.clear();
        ctx.close();
    }
}