Example usage for java.nio ByteBuffer rewind

List of usage examples for java.nio ByteBuffer rewind

Introduction

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

Prototype

public final Buffer rewind() 

Source Link

Document

Rewinds this buffer.

Usage

From source file:org.energy_home.jemma.osgi.ah.io.flexgateway.FlexGatewayButtons.java

public void run() {
    long tv_sec;//from   w  ww .j  av a 2 s .  c  o m
    long tv_usec;
    short type;
    short code;
    int value;
    while (true) {
        buttonFile = new File(flexGatewayButtonsDevice);
        ByteBuffer buffer = ByteBuffer.allocate(100);
        buffer.order(ByteOrder.nativeOrder());
        try {
            fis = new FileInputStream(buttonFile);
            channel = fis.getChannel();
            while (true) {
                buffer.clear();
                int size = channel.read(buffer);
                buffer.rewind();

                while (size > 0) {
                    tv_sec = buffer.getInt();
                    tv_usec = buffer.getInt();
                    long tv = tv_sec * 1000000 + tv_usec;
                    type = buffer.getShort();
                    code = buffer.getShort();
                    value = buffer.getInt();
                    size -= 16;

                    if (type == 0 && code == 0 && value == 0)
                        continue;

                    // Code 3 -> front button
                    // Code 2 -> back button
                    // Value > 0 -> button pressed
                    // Value > 0 -> button released

                    // send event
                    log.debug("Button: ms " + tv + " type " + (type & 0xffff) + " code " + (code & 0xffff)
                            + " value " + (value & 0xffffffff));

                    Hashtable props = new Hashtable();
                    props.put("timestamp", new Long(tv));
                    if (value > 0)
                        this.postEvent("button/" + code + "/UP", props);
                    else
                        this.postEvent("button/" + code + "/DOWN", props);
                }
            }
        } catch (ClosedByInterruptException e) {
            break;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("exception", e);
            break;
        } finally {
            try {
                if (channel != null)
                    channel.close();
            } catch (IOException e) {
                log.error("exception", e);
                break;
            }
        }
    }
    log.debug("exiting");
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.amlauncher.AMLauncher.java

@Private
@VisibleForTesting/*from ww w.  j  a v a 2s  . co m*/
protected void setupTokens(ContainerLaunchContext container, ContainerId containerID) throws IOException {
    Map<String, String> environment = container.getEnvironment();
    environment.put(ApplicationConstants.APPLICATION_WEB_PROXY_BASE_ENV, application.getWebProxyBase());
    // Set AppSubmitTime and MaxAppAttempts to be consumable by the AM.
    ApplicationId applicationId = application.getAppAttemptId().getApplicationId();
    environment.put(ApplicationConstants.APP_SUBMIT_TIME_ENV,
            String.valueOf(rmContext.getRMApps().get(applicationId).getSubmitTime()));
    environment.put(ApplicationConstants.MAX_APP_ATTEMPTS_ENV,
            String.valueOf(rmContext.getRMApps().get(applicationId).getMaxAppAttempts()));

    Credentials credentials = new Credentials();
    DataInputByteBuffer dibb = new DataInputByteBuffer();
    ByteBuffer tokens = container.getTokens();
    if (tokens != null) {
        // TODO: Don't do this kind of checks everywhere.
        dibb.reset(tokens);
        credentials.readTokenStorageStream(dibb);
        tokens.rewind();
    }

    // Add AMRMToken
    Token<AMRMTokenIdentifier> amrmToken = createAndSetAMRMToken();
    if (amrmToken != null) {
        credentials.addToken(amrmToken.getService(), amrmToken);
    }
    DataOutputBuffer dob = new DataOutputBuffer();
    credentials.writeTokenStorageToStream(dob);
    container.setTokens(ByteBuffer.wrap(dob.getData(), 0, dob.getLength()));
}

From source file:org.voltdb.TestParameterSet.java

public void testStrings() throws IOException {
    params = ParameterSet.fromArrayNoCopy(new Object[] { "foo" });
    ByteBuffer buf = ByteBuffer.allocate(params.getSerializedSize());
    params.flattenToBuffer(buf);//from   w  w w. j ava 2s . co m
    buf.rewind();

    ParameterSet out = ParameterSet.fromByteBuffer(buf);
    assertEquals(1, out.toArray().length);
    assertEquals("foo", out.toArray()[0]);
}

From source file:org.voltdb.TestParameterSet.java

public void testFloatsInsteadOfDouble() throws IOException {
    params = ParameterSet.fromArrayNoCopy(5.5f);
    ByteBuffer buf = ByteBuffer.allocate(params.getSerializedSize());
    params.flattenToBuffer(buf);//  w  w  w  . j  av  a2  s  .  c om
    buf.rewind();

    ParameterSet out = ParameterSet.fromByteBuffer(buf);
    Object value = out.toArray()[0];
    assertTrue(value instanceof Double);
    assertTrue((5.5f - ((Double) value).doubleValue()) < 0.01);
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

public void write(OutputStream output) throws IOException {

    baseBuffer.rewind();/*w  w w  .  j a v a2  s .  c  o  m*/
    checkBuffer.rewind();
    tailBuffer.rewind();

    int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
    int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());

    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));

    dataOutput.writeBoolean(compact);
    dataOutput.writeInt(baseCheckSize);
    dataOutput.writeInt(tailSize);

    WritableByteChannel channel = Channels.newChannel(dataOutput);

    ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(tailSize * 2);
    CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
    tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    dataOutput.flush();
}

From source file:org.voltdb.TestParameterSet.java

public void testStringsAsByteArray() throws IOException {
    params = ParameterSet.fromArrayNoCopy(new Object[] { new byte[] { 'f', 'o', 'o' } });
    ByteBuffer buf = ByteBuffer.allocate(params.getSerializedSize());
    params.flattenToBuffer(buf);//from w w w. j a va  2 s.com
    buf.rewind();

    ParameterSet out = ParameterSet.fromByteBuffer(buf);
    assertEquals(1, out.toArray().length);

    byte[] bin = (byte[]) out.toArray()[0];
    assertEquals(bin[0], 'f');
    assertEquals(bin[1], 'o');
    assertEquals(bin[2], 'o');
}

From source file:org.voltdb.TestParameterSet.java

public void testNull() throws IOException {
    params = ParameterSet.fromArrayNoCopy(new Object[] { null, null, null });
    ByteBuffer buf = ByteBuffer.allocate(params.getSerializedSize());
    params.flattenToBuffer(buf);//from   w  w  w.ja  va 2s.c  om
    buf.rewind();

    ParameterSet out = ParameterSet.fromByteBuffer(buf);

    buf = ByteBuffer.allocate(out.getSerializedSize());
    out.flattenToBuffer(buf);
    buf.rewind();

    ParameterSet out2 = ParameterSet.fromByteBuffer(buf);

    assertEquals(3, out2.toArray().length);
    assertNull(out.toArray()[0]);
}

From source file:org.voltdb.TestParameterSet.java

public void testNullSigils() throws IOException {
    params = ParameterSet.fromArrayNoCopy(VoltType.NULL_STRING_OR_VARBINARY, VoltType.NULL_DECIMAL,
            VoltType.NULL_INTEGER);/*ww  w. ja va2s  . c o  m*/
    ByteBuffer buf = ByteBuffer.allocate(params.getSerializedSize());
    params.flattenToBuffer(buf);
    buf.rewind();

    ParameterSet out = ParameterSet.fromByteBuffer(buf);
    assertEquals(3, out.toArray().length);

    buf = ByteBuffer.allocate(out.getSerializedSize());
    out.flattenToBuffer(buf);
    buf.rewind();

    ParameterSet out2 = ParameterSet.fromByteBuffer(buf);
    assertEquals(3, out2.toArray().length);

    System.out.println(out2.toJSONString());
}

From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java

/**
 * Creates a file with random content, repeatedly writing a random string of
 * 4k length to the file until the file has at least the specified length.
 *
 * @param f/*from   w w w  .jav  a2  s .c  om*/
 *            file to fill
 * @param size
 *            size of the file to generate
 * @return length of the generated file in bytes
 * @throws IOException
 */
protected long createPseudoRandomContentFile(Path f, long size) throws IOException {
    SecureRandom rnd = new SecureRandom();
    byte[] buf = new byte[4096];
    rnd.nextBytes(buf);
    ByteBuffer bytebuf = ByteBuffer.wrap(buf);
    try (FileChannel outChannel = FileChannel.open(f, StandardOpenOption.CREATE_NEW,
            StandardOpenOption.WRITE)) {
        long len = 0;
        do {
            len += outChannel.write(bytebuf);
            if (bytebuf.position() == 4096) {
                bytebuf.rewind();
            }
        } while (len < size);
    }
    return Files.size(f);
}

From source file:org.apache.cassandra.hadoop.ColumnFamilyRecordReader.java

@Override
public boolean next(ByteBuffer key, SortedMap<ByteBuffer, IColumn> value) throws IOException {
    if (this.nextKeyValue()) {
        key.clear();//from w  ww  . ja  v  a2  s  . co m
        key.put(this.getCurrentKey());
        key.rewind();

        value.clear();
        value.putAll(this.getCurrentValue());

        return true;
    }
    return false;
}