Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

In this page you can find the example usage for java.nio ByteBuffer flip.

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:com.github.jinahya.verbose.codec.BinaryCodecTest.java

protected final void encodeDecode(final ReadableByteChannel expectedChannel) throws IOException {

    if (expectedChannel == null) {
        throw new NullPointerException("null expectedChannel");
    }/*from  w  w w . j a va 2 s .co  m*/

    final Path encodedPath = Files.createTempFile("test", null);
    getRuntime().addShutdownHook(new Thread(() -> {
        try {
            Files.delete(encodedPath);
        } catch (final IOException ioe) {
            ioe.printStackTrace(System.err);
        }
    }));
    final WritableByteChannel encodedChannel = FileChannel.open(encodedPath, StandardOpenOption.WRITE);

    final ByteBuffer decodedBuffer = ByteBuffer.allocate(128);
    final ByteBuffer encodedBuffer = ByteBuffer.allocate(decodedBuffer.capacity() << 1);

    while (expectedChannel.read(decodedBuffer) != -1) {
        decodedBuffer.flip(); // limit -> position; position -> zero
        encoder.encode(decodedBuffer, encodedBuffer);
        encodedBuffer.flip();
        encodedChannel.write(encodedBuffer);
        encodedBuffer.compact(); // position -> n  + 1; limit -> capacity
        decodedBuffer.compact();
    }

    decodedBuffer.flip();
    while (decodedBuffer.hasRemaining()) {
        encoder.encode(decodedBuffer, encodedBuffer);
        encodedBuffer.flip();
        encodedChannel.write(encodedBuffer);
        encodedBuffer.compact();
    }

    encodedBuffer.flip();
    while (encodedBuffer.hasRemaining()) {
        encodedChannel.write(encodedBuffer);
    }
}

From source file:org.alfresco.patch.PatchServiceImpl.java

@SuppressWarnings("resource")
@Override//  ww w.j av  a2s .co  m
public PatchDocument getPatch(MultiPart resource) throws IOException {
    Integer blockSize = null;
    Integer matchCount = null;

    List<Integer> matchedBlocks = null;
    List<Patch> patches = new LinkedList<>();

    // This will iterate the individual parts of the multipart response
    for (BodyPart bodyPart : resource.getBodyParts()) {
        if (bodyPart instanceof FormDataMultiPart) {
            System.out.printf("Multipart Body Part [Mime Type: %s]\n", bodyPart.getMediaType());

            InputStream is = null;
            Integer size = null;
            Integer lastMatchIndex = null;

            FormDataMultiPart mp = (FormDataMultiPart) bodyPart;
            for (BodyPart bodyPart1 : mp.getBodyParts()) {
                ContentDisposition contentDisposition = bodyPart1.getContentDisposition();
                if (contentDisposition instanceof FormDataContentDisposition) {
                    FormDataContentDisposition cd = (FormDataContentDisposition) contentDisposition;
                    String name = cd.getName();

                    if (name.equals("p_size")) {
                        size = Integer.parseInt((String) bodyPart1.getEntity());
                    } else if (name.equals("p_last_match_idx")) {
                        lastMatchIndex = Integer.parseInt((String) bodyPart1.getEntity());
                    } else if (name.equals("p_stream")) {
                        is = (InputStream) bodyPart1.getEntity();
                    }
                }
            }

            ByteBuffer bb = ByteBuffer.allocate(1024 * 20); // TODO
            ReadableByteChannel channel = Channels.newChannel(is);
            channel.read(bb);
            bb.flip();
            byte[] buffer = new byte[bb.limit()];
            bb.get(buffer);
            Patch patch = new Patch(lastMatchIndex, size, buffer);
            patches.add(patch);
        } else {
            System.out.printf("Embedded Body Part [Mime Type: %s, Length: %s]\n", bodyPart.getMediaType(),
                    bodyPart.getContentDisposition().getSize());

            ContentDisposition contentDisposition = bodyPart.getContentDisposition();
            if (contentDisposition instanceof FormDataContentDisposition) {
                FormDataContentDisposition cd = (FormDataContentDisposition) contentDisposition;
                String name = cd.getName();

                if (name.equals("p_block_size")) {
                    blockSize = Integer.parseInt((String) bodyPart.getEntity());
                } else if (name.equals("p_match_count")) {
                    matchCount = Integer.parseInt((String) bodyPart.getEntity());
                } else if (name.equals("p_matched_blocks")) {
                    String matchedBlocksStr = (String) bodyPart.getEntity();
                    List<String> l = Arrays.asList(matchedBlocksStr.split(","));
                    matchedBlocks = l.stream().filter(s -> s != null && !s.equals(""))
                            .map(s -> Integer.parseInt(s)).collect(Collectors.toList());
                }
            }
        }
    }

    PatchDocument patchDocument = new PatchDocument(blockSize, matchedBlocks, patches);
    return patchDocument;
}

From source file:org.apache.hadoop.hive.serde2.compression.TestSnappyCompDe.java

@Before
public void init() {
    ByteBuffer firstRow = ByteBuffer.wrap(new byte[] { 2, 33, 7, 75, 5 });
    ByteBuffer secondRow = ByteBuffer.wrap(new byte[] { 3, 21, 6 });
    ByteBuffer thirdRow = ByteBuffer.wrap(new byte[] { 52, 25, 74, 74, 64 });
    firstRow.flip();
    secondRow.flip();//from  w  ww .  j  ava  2s .c  om
    thirdRow.flip();
    ArrayList<ByteBuffer> someBinaries = new ArrayList<ByteBuffer>();
    someBinaries.add(firstRow);
    someBinaries.add(secondRow);
    someBinaries.add(thirdRow);
    columnBinary = new ColumnBuffer(
            TColumn.binaryVal(new TBinaryColumn(someBinaries, ByteBuffer.wrap(new byte[] {}))));

    // Test leading and trailing `false` in column
    ArrayList<Boolean> bools = new ArrayList<Boolean>();
    bools.add(false);
    bools.add(true);
    bools.add(false);
    bools.add(true);
    bools.add(false);
    columnBool = new ColumnBuffer(TColumn.boolVal(new TBoolColumn(bools, ByteBuffer.wrap(noNullMask))));

    ArrayList<Byte> bytes = new ArrayList<Byte>();
    bytes.add((byte) 0);
    bytes.add((byte) 1);
    bytes.add((byte) 2);
    bytes.add((byte) 3);
    columnByte = new ColumnBuffer(TColumn.byteVal(new TByteColumn(bytes, ByteBuffer.wrap(noNullMask))));

    ArrayList<Short> shorts = new ArrayList<Short>();
    shorts.add((short) 0);
    shorts.add((short) 1);
    shorts.add((short) -127);
    shorts.add((short) 127);
    columnShort = new ColumnBuffer(TColumn.i16Val(new TI16Column(shorts, ByteBuffer.wrap(noNullMask))));

    ArrayList<Integer> ints = new ArrayList<Integer>();
    ints.add(0);
    ints.add(1);
    ints.add(-32767);
    ints.add(32767);
    columnInt = new ColumnBuffer(TColumn.i32Val(new TI32Column(ints, ByteBuffer.wrap(noNullMask))));

    ArrayList<Long> longs = new ArrayList<Long>();
    longs.add((long) 0);
    longs.add((long) 1);
    longs.add((long) -2147483647);
    longs.add((long) 2147483647);
    columnLong = new ColumnBuffer(TColumn.i64Val(new TI64Column(longs, ByteBuffer.wrap(noNullMask))));

    ArrayList<Double> doubles = new ArrayList<Double>();
    doubles.add((double) 0);
    doubles.add((double) 1.0);
    doubles.add((double) -2147483647.5);
    doubles.add((double) 2147483647.5);
    columnDouble = new ColumnBuffer(TColumn.doubleVal(new TDoubleColumn(doubles, ByteBuffer.wrap(noNullMask))));

    ArrayList<String> strings = new ArrayList<String>();
    strings.add("ABC");
    strings.add("DEFG");
    strings.add("HI");
    strings.add(StringUtils.rightPad("", 65535, 'j'));
    strings.add("");
    columnStr = new ColumnBuffer(TColumn.stringVal(new TStringColumn(strings, ByteBuffer.wrap(noNullMask))));

    compDe.init(new HashMap<String, String>());
}

From source file:com.spotify.heroic.metric.datastax.schema.legacy.MapSerializer.java

@Override
public ByteBuffer serialize(final Map<A, B> value) throws IOException {
    final List<Pair<ByteBuffer, ByteBuffer>> buffers = new ArrayList<>();

    short size = 0;

    for (final Map.Entry<A, B> e : value.entrySet()) {
        final ByteBuffer key = a.serialize(e.getKey());
        final ByteBuffer val = b.serialize(e.getValue());

        size += key.limit() + val.limit();

        buffers.add(Pair.of(key, val));
    }/*  www.j  a v a 2  s .  c  o  m*/

    final ByteBuffer buffer = ByteBuffer.allocate(4 + 8 * value.size() + size);
    buffer.putShort((short) buffers.size());

    for (final Pair<ByteBuffer, ByteBuffer> p : buffers) {
        buffer.putShort((short) p.getLeft().remaining());
        buffer.put(p.getLeft());
        buffer.putShort((short) p.getRight().remaining());
        buffer.put(p.getRight());
    }

    buffer.flip();
    return buffer;
}

From source file:org.apache.hadoop.hbase.client.TestResult.java

public void testMultiVersionLoadValue() throws Exception {
    KeyValue[] kvs1 = genKVs(row, family, value, 1, 100);
    KeyValue[] kvs2 = genKVs(row, family, value, 200, 100);

    KeyValue[] kvs = new KeyValue[kvs1.length + kvs2.length];
    System.arraycopy(kvs1, 0, kvs, 0, kvs1.length);
    System.arraycopy(kvs2, 0, kvs, kvs1.length, kvs2.length);

    Arrays.sort(kvs, KeyValue.COMPARATOR);

    ByteBuffer loadValueBuffer = ByteBuffer.allocate(1024);

    Result r = Result.create(kvs);
    for (int i = 0; i < 100; ++i) {
        final byte[] qf = Bytes.toBytes(i);

        loadValueBuffer.clear();/*from   ww w .  j  ava 2 s  .  co  m*/
        r.loadValue(family, qf, loadValueBuffer);
        loadValueBuffer.flip();
        assertEquals(ByteBuffer.wrap(Bytes.add(value, Bytes.toBytes(i))), loadValueBuffer);
        assertEquals(ByteBuffer.wrap(Bytes.add(value, Bytes.toBytes(i))), r.getValueAsByteBuffer(family, qf));
    }
}

From source file:gridool.memcached.gateway.MemcachedProxyHandler.java

@Override
public byte[] handleGet(byte[] key) {
    final ByteBuffer reqPacket = ByteBuffer.allocate(HEADER_LENGTH + key.length);
    // request header
    Header header = new Header(MAGIC_BYTE_REQUEST, OPCODE_GET);
    header.setBodyLength(GET_EXTRA_LENGTH, key.length, 0);
    header.encode(reqPacket);/*from   w  w w  . j  ava  2 s .c  om*/
    // request body (key)
    reqPacket.put(key);
    reqPacket.flip();

    final byte[] value;
    final SocketAddress sockAddr = getSocket(key);
    final ByteChannel channel = sockPool.borrowObject(sockAddr);
    try {
        // handle request
        NIOUtils.writeFully(channel, reqPacket);

        // handle response header
        ByteBuffer responseHeaderPacket = ByteBuffer.allocate(HEADER_LENGTH);
        NIOUtils.readFully(channel, responseHeaderPacket);
        responseHeaderPacket.flip();
        // handle response body 
        int totalBody = responseHeaderPacket.getInt(8);
        int keyLen = responseHeaderPacket.getShort(2);
        int extraLen = responseHeaderPacket.get(4);
        int bodyPos = extraLen + keyLen;
        int bodyLen = totalBody - bodyPos;
        if (bodyLen <= 0) {
            return null;
        }
        ByteBuffer responseBodyPacket = ByteBuffer.allocate(totalBody);
        NIOUtils.readFully(channel, responseBodyPacket);
        responseBodyPacket.flip();
        value = new byte[bodyLen];
        responseBodyPacket.get(value, 0, bodyLen);
    } catch (IOException e) {
        LOG.error(e);
        return null;
    } finally {
        sockPool.returnObject(sockAddr, channel);
    }
    return value;
}

From source file:com.linkedin.pinot.core.segment.index.creator.SegmentGenerationWithBytesTypeTest.java

/**
 * Build Avro file containing serialized TDigest bytes.
 *
 * @param schema Schema of data (one fixed and one variable column)
 * @param _fixedExpected Serialized bytes of fixed length column are populated here
 * @param _varExpected Serialized bytes of variable length column are populated here
 * @throws IOException/* w  ww. j a  v a 2  s.co m*/
 */
private void buildAvro(Schema schema, List<byte[]> _fixedExpected, List<byte[]> _varExpected)
        throws IOException {
    org.apache.avro.Schema avroSchema = AvroUtils.getAvroSchemaFromPinotSchema(schema);

    try (DataFileWriter<GenericData.Record> recordWriter = new DataFileWriter<>(
            new GenericDatumWriter<>(avroSchema))) {

        if (!new File(AVRO_DIR_NAME).mkdir()) {
            throw new RuntimeException("Unable to create test directory: " + AVRO_DIR_NAME);
        }

        recordWriter.create(avroSchema, new File(AVRO_DIR_NAME, AVRO_NAME));
        for (int i = 0; i < NUM_ROWS; i++) {
            GenericData.Record record = new GenericData.Record(avroSchema);

            TDigest tDigest = new TDigest(PercentileTDigestAggregationFunction.DEFAULT_TDIGEST_COMPRESSION);
            tDigest.add(_random.nextDouble());

            ByteBuffer buffer = ByteBuffer.allocate(tDigest.byteSize());
            tDigest.asBytes(buffer);
            _fixedExpected.add(buffer.array());

            buffer.flip();
            record.put(FIXED_BYTES_UNSORTED_COLUMN, buffer);

            if (i % 2 == 0) {
                tDigest.add(_random.nextDouble());
            }

            buffer = ByteBuffer.allocate(tDigest.byteSize());
            tDigest.asBytes(buffer);
            _varExpected.add(buffer.array());

            buffer.flip();
            record.put(VARIABLE_BYTES_COLUMN, buffer);

            recordWriter.append(record);
        }
    }
}

From source file:com.alexkli.jhb.Worker.java

@Override
public void run() {
    try {/*from w  w  w .  jav  a 2s . c o  m*/
        while (true) {
            long start = System.nanoTime();

            QueueItem<HttpRequestBase> item = queue.take();

            idleAvg.add(System.nanoTime() - start);

            if (item.isPoisonPill()) {
                return;
            }

            HttpRequestBase request = item.getRequest();

            if ("java".equals(config.client)) {
                System.setProperty("http.keepAlive", "false");

                item.sent();

                try {
                    HttpURLConnection http = (HttpURLConnection) new URL(request.getURI().toString())
                            .openConnection();
                    http.setConnectTimeout(5000);
                    http.setReadTimeout(5000);
                    int statusCode = http.getResponseCode();

                    consumeAndCloseStream(http.getInputStream());

                    if (statusCode == 200) {
                        item.done();
                    } else {
                        item.failed();
                    }
                } catch (IOException e) {
                    System.err.println("Failed request: " + e.getMessage());
                    e.printStackTrace();
                    //                        System.exit(2);
                    item.failed();
                }
            } else if ("ahc".equals(config.client)) {
                try {
                    item.sent();

                    try (CloseableHttpResponse response = httpClient.execute(request, context)) {
                        int statusCode = response.getStatusLine().getStatusCode();
                        if (statusCode == 200) {
                            item.done();
                        } else {
                            item.failed();
                        }
                    }
                } catch (IOException e) {
                    System.err.println("Failed request: " + e.getMessage());
                    item.failed();
                }
            } else if ("fast".equals(config.client)) {
                try {
                    URI uri = request.getURI();

                    item.sent();

                    InetAddress addr = InetAddress.getByName(uri.getHost());
                    Socket socket = new Socket(addr, uri.getPort());
                    PrintWriter out = new PrintWriter(socket.getOutputStream());
                    //                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    // send an HTTP request to the web server
                    out.println("GET / HTTP/1.1");
                    out.append("Host: ").append(uri.getHost()).append(":").println(uri.getPort());
                    out.println("Connection: Close");
                    out.println();
                    out.flush();

                    // read the response
                    consumeAndCloseStream(socket.getInputStream());
                    //                        boolean loop = true;
                    //                        StringBuilder sb = new StringBuilder(8096);
                    //                        while (loop) {
                    //                            if (in.ready()) {
                    //                                int i = 0;
                    //                                while (i != -1) {
                    //                                    i = in.read();
                    //                                    sb.append((char) i);
                    //                                }
                    //                                loop = false;
                    //                            }
                    //                        }
                    item.done();
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                    item.failed();
                }
            } else if ("nio".equals(config.client)) {
                URI uri = request.getURI();

                item.sent();

                String requestBody = "GET / HTTP/1.1\n" + "Host: " + uri.getHost() + ":" + uri.getPort() + "\n"
                        + "Connection: Close\n\n";

                try {
                    InetSocketAddress addr = new InetSocketAddress(uri.getHost(), uri.getPort());
                    SocketChannel channel = SocketChannel.open();
                    channel.socket().setSoTimeout(5000);
                    channel.connect(addr);

                    ByteBuffer msg = ByteBuffer.wrap(requestBody.getBytes());
                    channel.write(msg);
                    msg.clear();

                    ByteBuffer buf = ByteBuffer.allocate(1024);

                    int count;
                    while ((count = channel.read(buf)) != -1) {
                        buf.flip();

                        byte[] bytes = new byte[count];
                        buf.get(bytes);

                        buf.clear();
                    }
                    channel.close();

                    item.done();

                } catch (IOException e) {
                    e.printStackTrace();
                    item.failed();
                }
            }
        }
    } catch (InterruptedException e) {
        System.err.println("Worker thread [" + this.toString() + "] was interrupted: " + e.getMessage());
    }
}

From source file:com.offbynull.portmapper.natpmp.NatPmpReceiver.java

/**
 * Start listening for NAT-PMP events. This method blocks until {@link #stop() } is called.
 * @param listener listener to notify of events
 * @throws IOException if socket error occurs
 * @throws NullPointerException if any argument is {@code null}
 *//*w w w. j  a  v a  2 s.  c o m*/
public void start(NatPmpEventListener listener) throws IOException {
    Validate.notNull(listener);

    MulticastSocket socket = null;
    try {
        final InetAddress group = InetAddress.getByName("224.0.0.1"); // NOPMD
        final int port = 5350;
        final InetSocketAddress groupAddress = new InetSocketAddress(group, port);

        socket = new MulticastSocket(port);

        if (!currentSocket.compareAndSet(null, socket)) {
            IOUtils.closeQuietly(socket);
            return;
        }

        socket.setReuseAddress(true);

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
            while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
                InetAddress addr = addrs.nextElement();

                try {
                    if (addr instanceof Inet4Address) {
                        socket.joinGroup(groupAddress, networkInterface);
                    }
                } catch (IOException ioe) { // NOPMD
                    // occurs with certain interfaces
                    // do nothing
                }
            }
        }

        ByteBuffer buffer = ByteBuffer.allocate(12);
        DatagramPacket data = new DatagramPacket(buffer.array(), buffer.capacity());

        while (true) {
            buffer.clear();
            socket.receive(data);
            buffer.position(data.getLength());
            buffer.flip();

            if (!data.getAddress().equals(gatewayAddress)) { // data isn't from our gateway, ignore
                continue;
            }

            if (buffer.remaining() != 12) { // data isn't the expected size, ignore
                continue;
            }

            int version = buffer.get(0);
            if (version != 0) { // data doesn't have the correct version, ignore
                continue;
            }

            int opcode = buffer.get(1) & 0xFF;
            if (opcode != 128) { // data doesn't have the correct op, ignore
                continue;
            }

            int resultCode = buffer.getShort(2) & 0xFFFF;
            switch (resultCode) {
            case 0:
                break;
            default:
                continue; // data doesn't have a successful result, ignore
            }

            listener.publicAddressUpdated(new ExternalAddressNatPmpResponse(buffer));
        }

    } catch (IOException ioe) {
        if (currentSocket.get() == null) {
            return; // ioexception caused by interruption/stop, so just return without propogating error up
        }

        throw ioe;
    } finally {
        IOUtils.closeQuietly(socket);
        currentSocket.set(null);
    }
}