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:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that does not include any groups.
 *//*from w  w  w .ja  v  a  2 s. co  m*/
@Test
public void testRowKeyWithNoGroup() throws Exception {
    // setup
    measurement.withGroups(Collections.emptyList());

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:edu.stanford.slac.archiverappliance.PB.data.BoundaryConditionsSimulationValueGenerator.java

/**
 * Get a value based on the DBR type. /*  www .j a va  2 s .  c o m*/
 * We should check for boundary conditions here and make sure PB does not throw exceptions when we come close to MIN_ and MAX_ values 
 * @param type
 * @param secondsIntoYear
 * @return
 */
public SampleValue getSampleValue(ArchDBRTypes type, int secondsIntoYear) {
    switch (type) {
    case DBR_SCALAR_STRING:
        return new ScalarStringSampleValue(Integer.toString(secondsIntoYear));
    case DBR_SCALAR_SHORT:
        if (0 <= secondsIntoYear && secondsIntoYear < 1000) {
            // Check for some numbers around the minimum value
            return new ScalarValue<Short>((short) (Short.MIN_VALUE + secondsIntoYear));
        } else if (1000 <= secondsIntoYear && secondsIntoYear < 2000) {
            // Check for some numbers around the maximum value
            return new ScalarValue<Short>((short) (Short.MAX_VALUE - (secondsIntoYear - 1000)));
        } else {
            // Check for some numbers around 0
            return new ScalarValue<Short>((short) (secondsIntoYear - 2000));
        }
    case DBR_SCALAR_FLOAT:
        if (0 <= secondsIntoYear && secondsIntoYear < 1000) {
            // Check for some numbers around the minimum value
            return new ScalarValue<Float>(Float.MIN_VALUE + secondsIntoYear);
        } else if (1000 <= secondsIntoYear && secondsIntoYear < 2000) {
            // Check for some numbers around the maximum value
            return new ScalarValue<Float>(Float.MAX_VALUE - (secondsIntoYear - 1000));
        } else {
            // Check for some numbers around 0. Divide by a large number to make sure we cater to the number of precision digits
            return new ScalarValue<Float>((secondsIntoYear - 2000.0f) / secondsIntoYear);
        }
    case DBR_SCALAR_ENUM:
        return new ScalarValue<Short>((short) secondsIntoYear);
    case DBR_SCALAR_BYTE:
        return new ScalarValue<Byte>(((byte) (secondsIntoYear % 255)));
    case DBR_SCALAR_INT:
        if (0 <= secondsIntoYear && secondsIntoYear < 1000) {
            // Check for some numbers around the minimum value
            return new ScalarValue<Integer>(Integer.MIN_VALUE + secondsIntoYear);
        } else if (1000 <= secondsIntoYear && secondsIntoYear < 2000) {
            // Check for some numbers around the maximum value
            return new ScalarValue<Integer>(Integer.MAX_VALUE - (secondsIntoYear - 1000));
        } else {
            // Check for some numbers around 0
            return new ScalarValue<Integer>(secondsIntoYear - 2000);
        }
    case DBR_SCALAR_DOUBLE:
        if (0 <= secondsIntoYear && secondsIntoYear < 1000) {
            // Check for some numbers around the minimum value
            return new ScalarValue<Double>(Double.MIN_VALUE + secondsIntoYear);
        } else if (1000 <= secondsIntoYear && secondsIntoYear < 2000) {
            // Check for some numbers around the maximum value
            return new ScalarValue<Double>(Double.MAX_VALUE - (secondsIntoYear - 1000));
        } else {
            // Check for some numbers around 0. Divide by a large number to make sure we cater to the number of precision digits
            return new ScalarValue<Double>((secondsIntoYear - 2000.0) / (secondsIntoYear * 1000000));
        }
    case DBR_WAVEFORM_STRING:
        // Varying number of copies of a typical value
        return new VectorStringSampleValue(
                Collections.nCopies(secondsIntoYear, Integer.toString(secondsIntoYear)));
    case DBR_WAVEFORM_SHORT:
        return new VectorValue<Short>(Collections.nCopies(1, (short) secondsIntoYear));
    case DBR_WAVEFORM_FLOAT:
        // Varying number of copies of a typical value
        return new VectorValue<Float>(
                Collections.nCopies(secondsIntoYear, (float) Math.cos(secondsIntoYear * Math.PI / 3600)));
    case DBR_WAVEFORM_ENUM:
        return new VectorValue<Short>(Collections.nCopies(1024, (short) secondsIntoYear));
    case DBR_WAVEFORM_BYTE:
        // Large number of elements in the array
        return new VectorValue<Byte>(
                Collections.nCopies(65536 * secondsIntoYear, ((byte) (secondsIntoYear % 255))));
    case DBR_WAVEFORM_INT:
        // Varying number of copies of a typical value
        return new VectorValue<Integer>(
                Collections.nCopies(secondsIntoYear, secondsIntoYear * secondsIntoYear));
    case DBR_WAVEFORM_DOUBLE:
        // Varying number of copies of a typical value
        return new VectorValue<Double>(
                Collections.nCopies(secondsIntoYear, Math.sin(secondsIntoYear * Math.PI / 3600)));
    case DBR_V4_GENERIC_BYTES:
        // Varying number of copies of a typical value
        ByteBuffer buf = ByteBuffer.allocate(1024 * 10);
        buf.put(Integer.toString(secondsIntoYear).getBytes());
        buf.flip();
        return new ByteBufSampleValue(buf);
    default:
        throw new RuntimeException("We seemed to have missed a DBR type when generating sample data");
    }
}

From source file:org.apache.hadoop.mapred.FadvisedFileRegion.java

/**
 * This method transfers data using local buffer. It transfers data from 
 * a disk to a local buffer in memory, and then it transfers data from the 
 * buffer to the target. This is used only if transferTo is disallowed in
 * the configuration file. super.TransferTo does not perform well on Windows 
 * due to a small IO request generated. customShuffleTransfer can control 
 * the size of the IO requests by changing the size of the intermediate 
 * buffer.//from  w  w w . jav  a2 s  .  c om
 */
@VisibleForTesting
long customShuffleTransfer(WritableByteChannel target, long position) throws IOException {
    long actualCount = this.count - position;
    if (actualCount < 0 || position < 0) {
        throw new IllegalArgumentException(
                "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')');
    }
    if (actualCount == 0) {
        return 0L;
    }

    long trans = actualCount;
    int readSize;
    ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize);

    while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) {
        //adjust counters and buffer limit
        if (readSize < trans) {
            trans -= readSize;
            position += readSize;
            byteBuffer.flip();
        } else {
            //We can read more than we need if the actualCount is not multiple 
            //of the byteBuffer size and file is big enough. In that case we cannot
            //use flip method but we need to set buffer limit manually to trans.
            byteBuffer.limit((int) trans);
            byteBuffer.position(0);
            position += trans;
            trans = 0;
        }

        //write data to the target
        while (byteBuffer.hasRemaining()) {
            target.write(byteBuffer);
        }

        byteBuffer.clear();
    }

    return actualCount - trans;
}

From source file:de.spqrinfo.cups4j.operations.IppOperation.java

/**
 * Gets the IPP header/*w  w w.j  a v a 2  s  .c  o  m*/
 *
 * @param url
 * @param map
 * @return IPP header
 * @throws UnsupportedEncodingException
 */
public ByteBuffer getIppHeader(URL url, Map<String, String> map) throws UnsupportedEncodingException {
    if (url == null) {
        logger.error("IppOperation.getIppHeader(): uri is null");
        return null;
    }

    ByteBuffer ippBuf = ByteBuffer.allocateDirect(bufferSize);
    ippBuf = IppTag.getOperation(ippBuf, operationID);
    ippBuf = IppTag.getUri(ippBuf, "printer-uri", stripPortNumber(url));

    if (map == null) {
        ippBuf = IppTag.getEnd(ippBuf);
        ippBuf.flip();
        return ippBuf;
    }

    ippBuf = IppTag.getNameWithoutLanguage(ippBuf, "requesting-user-name", map.get("requesting-user-name"));

    if (map.get("limit") != null) {
        int value = Integer.parseInt(map.get("limit"));
        ippBuf = IppTag.getInteger(ippBuf, "limit", value);
    }

    if (map.get("requested-attributes") != null) {
        String[] sta = map.get("requested-attributes").split(" ");
        if (sta != null) {
            ippBuf = IppTag.getKeyword(ippBuf, "requested-attributes", sta[0]);
            int l = sta.length;
            for (int i = 1; i < l; i++) {
                ippBuf = IppTag.getKeyword(ippBuf, null, sta[i]);
            }
        }
    }

    ippBuf = IppTag.getEnd(ippBuf);
    ippBuf.flip();
    return ippBuf;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

private ByteBuffer fuse(List<byte[]> buffers, final int length) {
    //fuses the buffers into a single array of the target length
    final ByteBuffer bb = ByteBuffer.allocate(length);

    for (byte[] buffer : buffers) {
        if (buffer.length > length - bb.position()) {
            bb.put(buffer, 0, length - bb.position());
        } else {/*from   w w w . j  a v  a 2 s  .  co  m*/
            bb.put(buffer);
        }
    }

    //important
    bb.flip();

    return bb;
}

From source file:org.apache.htrace.impl.PackedBufferManager.java

private void readAndValidateResponseFrame(SelectionKey sockKey, ByteBuffer buf, long expectedSeq,
        int expectedMethodId) throws IOException {
    buf.clear();//from   w ww . jav  a2s .c  o m
    buf.limit(PackedBuffer.HRPC_RESP_FRAME_LENGTH);
    doRecv(sockKey, buf);
    buf.flip();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    long seq = buf.getLong();
    if (seq != expectedSeq) {
        throw new IOException("Expected sequence number " + expectedSeq + ", but got sequence number " + seq);
    }
    int methodId = buf.getInt();
    if (expectedMethodId != methodId) {
        throw new IOException("Expected method id " + expectedMethodId + ", but got " + methodId);
    }
    int errorLength = buf.getInt();
    buf.getInt();
    if ((errorLength < 0) || (errorLength > PackedBuffer.MAX_HRPC_ERROR_LENGTH)) {
        throw new IOException("Got server error with invalid length " + errorLength);
    } else if (errorLength > 0) {
        buf.clear();
        buf.limit(errorLength);
        doRecv(sockKey, buf);
        buf.flip();
        CharBuffer charBuf = StandardCharsets.UTF_8.decode(buf);
        String serverErrorStr = charBuf.toString();
        throw new IOException("Got server error " + serverErrorStr);
    }
}

From source file:org.apache.tajo.pullserver.FadvisedFileRegion.java

/**
 * This method transfers data using local buffer. It transfers data from
 * a disk to a local buffer in memory, and then it transfers data from the
 * buffer to the target. This is used only if transferTo is disallowed in
 * the configuration file. super.TransferTo does not perform well on Windows
 * due to a small IO request generated. customShuffleTransfer can control
 * the size of the IO requests by changing the size of the intermediate
 * buffer./*ww  w .  j a  v a 2s . c  o  m*/
 */
@VisibleForTesting
long customShuffleTransfer(WritableByteChannel target, long position) throws IOException {
    long actualCount = this.count - position;
    if (actualCount < 0 || position < 0) {
        throw new IllegalArgumentException(
                "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')');
    }
    if (actualCount == 0) {
        return 0L;
    }

    long trans = actualCount;
    int readSize;
    ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize);

    while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) {
        //adjust counters and buffer limit
        if (readSize < trans) {
            trans -= readSize;
            position += readSize;
            byteBuffer.flip();
        } else {
            //We can read more than we need if the actualCount is not multiple
            //of the byteBuffer size and file is big enough. In that case we cannot
            //use flip method but we need to set buffer limit manually to trans.
            byteBuffer.limit((int) trans);
            byteBuffer.position(0);
            position += trans;
            trans = 0;
        }

        //write data to the target
        while (byteBuffer.hasRemaining()) {
            target.write(byteBuffer);
        }

        byteBuffer.clear();
    }

    return actualCount - trans;
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.TestAuxServices.java

@Test
public void testAuxEventDispatch() {
    Configuration conf = new Configuration();
    conf.setStrings(YarnConfiguration.NM_AUX_SERVICES, new String[] { "Asrv", "Bsrv" });
    conf.setClass(String.format(YarnConfiguration.NM_AUX_SERVICE_FMT, "Asrv"), ServiceA.class, Service.class);
    conf.setClass(String.format(YarnConfiguration.NM_AUX_SERVICE_FMT, "Bsrv"), ServiceB.class, Service.class);
    conf.setInt("A.expected.init", 1);
    conf.setInt("B.expected.stop", 1);
    final AuxServices aux = new AuxServices();
    aux.init(conf);/*ww w . j a v  a2  s  .  com*/
    aux.start();

    ApplicationId appId1 = ApplicationId.newInstance(0, 65);
    ByteBuffer buf = ByteBuffer.allocate(6);
    buf.putChar('A');
    buf.putInt(65);
    buf.flip();
    AuxServicesEvent event = new AuxServicesEvent(AuxServicesEventType.APPLICATION_INIT, "user0", appId1,
            "Asrv", buf, "user0Folder");
    aux.handle(event);
    ApplicationId appId2 = ApplicationId.newInstance(0, 66);
    event = new AuxServicesEvent(AuxServicesEventType.APPLICATION_STOP, "user0", appId2, "Bsrv", null,
            "user0Folder");
    // verify all services got the stop event 
    aux.handle(event);
    Collection<AuxiliaryService> servs = aux.getServices();
    for (AuxiliaryService serv : servs) {
        ArrayList<Integer> appIds = ((LightService) serv).getAppIdsStopped();
        assertEquals("app not properly stopped", 1, appIds.size());
        assertTrue("wrong app stopped", appIds.contains((Integer) 66));
    }

    for (AuxiliaryService serv : servs) {
        assertNull(((LightService) serv).containerId);
        assertNull(((LightService) serv).resource);
    }

    ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance(appId1, 1);
    ContainerTokenIdentifier cti = new ContainerTokenIdentifier(ContainerId.newContainerId(attemptId, 1), "",
            "", Resource.newInstance(1, 1), 0, 0, 0, Priority.newInstance(0), 0, "");
    Context context = mock(Context.class);
    Container container = new ContainerImpl(null, null, null, null, null, cti, context);
    ContainerId containerId = container.getContainerId();
    Resource resource = container.getResource();
    event = new AuxServicesEvent(AuxServicesEventType.CONTAINER_INIT, container);
    aux.handle(event);
    for (AuxiliaryService serv : servs) {
        assertEquals(containerId, ((LightService) serv).containerId);
        assertEquals(resource, ((LightService) serv).resource);
        ((LightService) serv).containerId = null;
        ((LightService) serv).resource = null;
    }

    event = new AuxServicesEvent(AuxServicesEventType.CONTAINER_STOP, container);
    aux.handle(event);
    for (AuxiliaryService serv : servs) {
        assertEquals(containerId, ((LightService) serv).containerId);
        assertEquals(resource, ((LightService) serv).resource);
    }
}

From source file:eu.europa.esig.dss.DSSUtils.java

public static long toLong(final byte[] bytes) {
    // Long.valueOf(new String(bytes)).longValue();
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes, 0, Long.SIZE / 8);
    // TODO: (Bob: 2014 Jan 22) To be checked if it is not platform dependent?
    buffer.flip();//need flip
    return buffer.getLong();
}

From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java

protected boolean writeAndProcessChannelData(NIOClientSocketHandler clientHandler) {
    try {//from  w w  w . j  a  va  2s. co  m
        if (!clientHandler.appendOutgoingData())
            return false;

        ByteBuffer outputBuffer = clientHandler.getOutputBuffer();
        outputBuffer.flip(); // Prepare for reading
        int writeAmount;
        try {
            writeAmount = clientHandler.getSocketChannel().write(outputBuffer);
            if (writeAmount <= 0)
                log.debug("[" + clientHandler.getId() + "] Cannot write, channel socket was closed");
        } catch (IOException e) {
            log.error("[" + clientHandler.getId() + "] Write failed : " + e.getMessage());
            writeAmount = -1;
        }
        outputBuffer.compact(); // Restore pointers

        return (writeAmount > 0);
    } catch (Exception e) {
        log.error("[" + clientHandler.getId() + "] Could not process data", e);
        return false;
    }
}