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.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsReply.java

@Override
public void readFrom(ByteBuffer data) {
    short i;//from  w  w w.ja  va  2 s.c om
    this.length = data.getShort();
    if (length < MINIMUM_LENGTH)
        return; //TBD - Spurious Packet?
    this.tableId = data.get();
    data.get(); // pad
    this.durationSeconds = data.getInt();
    this.durationNanoseconds = data.getInt();
    this.priority = data.getShort();
    this.idleTimeout = data.getShort();
    this.hardTimeout = data.getShort();
    this.match_len = data.getShort();
    this.idleAge = data.getShort();
    this.hardAge = data.getShort();
    this.cookie = data.getLong();
    this.packetCount = data.getLong();
    this.byteCount = data.getLong();
    if (this.length == MINIMUM_LENGTH) {
        return; //TBD - can this happen??
    }
    if (this.match == null)
        this.match = new V6Match();
    ByteBuffer mbuf = ByteBuffer.allocate(match_len);
    for (i = 0; i < match_len; i++) {
        mbuf.put(data.get());
    }
    mbuf.rewind();
    this.match.readFrom(mbuf);
    if (this.actionFactory == null)
        throw new RuntimeException("OFActionFactory not set");
    /*
     * action list may be preceded by a padding of 0 to 7 bytes based upon this:
     */
    short pad_size = (short) (((match_len + 7) / 8) * 8 - match_len);
    for (i = 0; i < pad_size; i++)
        data.get();
    int action_len = this.length - MINIMUM_LENGTH - (match_len + pad_size);
    if (action_len > 0)
        this.actions = this.actionFactory.parseActions(data, action_len);
}

From source file:com.sm.connector.server.MRStore.java

@Override
public void setCurrent(int current) {
    if (current >= getTotalRecord())
        throw new RuntimeException("current " + current + " >= total " + getTotalRecord());
    else {/*w w w  . ja  v a 2  s . c om*/
        curRecord = current;
        if (current == 0)
            return;
        //set 3 channel position
        long pos = OFFSET + (long) getCurRecord() * RECORD_SIZE;
        try {
            //read the first index record
            ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
            //re read the index buffer
            indexChannel.read(buf, pos);
            buf.rewind();
            long len = indexChannel.read(indexBuf, pos);
            indexBuf.rewind();
            indexPos = pos + len;
            // data and key pos, need to get from previous index record
            buf.get(); //get status byte
            long key = buf.getLong();
            long keyOff = getOffset(key);
            //long keyLen = getLen( key);
            len = keyChannel.read(keyBuf, keyOff);
            keyBuf.rewind();
            keyPos = keyOff + len;
            //read data channel
            long dataOffset2Len = buf.getLong();
            //long block2version = buf.getLong();
            long dataLen = getLen(dataOffset2Len);
            long offset = getOffset(dataOffset2Len);
            //long blockSize = getSize( block2version);
            len = dataChannel.read(dataBuf, offset);
            dataBuf.rewind();
            dataPos = offset + len;
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }
}

From source file:com.sastix.cms.server.services.content.impl.ZipFileHandlerServiceImpl.java

@Override
public DataMaps unzip(byte[] bytes) throws IOException {
    Map<String, String> foldersMap = new HashMap<>();

    Map<String, byte[]> extractedBytesMap = new HashMap<>();
    InputStream byteInputStream = new ByteArrayInputStream(bytes);
    //validate that it is a zip file
    if (isZipFile(bytes)) {
        try {// w  w  w  .ja  va2s  .co  m
            //get the zip file content
            ZipInputStream zis = new ZipInputStream(byteInputStream);
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                if (!ze.isDirectory()) {//if entry is a directory, we should not add it as a file
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    try {
                        ByteBuffer bufIn = ByteBuffer.allocate(1024);
                        int bytesRead;
                        while ((bytesRead = zis.read(bufIn.array())) > 0) {
                            baos.write(bufIn.array(), 0, bytesRead);
                            bufIn.rewind();
                        }
                        bufIn.clear();
                        extractedBytesMap.put(fileName, baos.toByteArray());
                    } finally {
                        baos.close();
                    }
                } else {
                    foldersMap.put(fileName, fileName);
                }
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    DataMaps dataMaps = new DataMaps();
    dataMaps.setBytesMap(extractedBytesMap);
    dataMaps.setFoldersMap(foldersMap);
    return dataMaps;
}

From source file:org.cosmo.common.util.Util.java

public static short[] longToShorts2(long value) {
    short[] shorts = new short[4];
    ByteBuffer b = ByteBuffer.allocate(8);
    b.putLong(value);//w w w  .  j a  va2  s .c om

    b.rewind();
    shorts[0] = b.getShort();
    shorts[1] = b.getShort();
    shorts[2] = b.getShort();
    shorts[3] = b.getShort();
    return shorts;

}

From source file:com.mycustomloader.vsamloader.VSAMLoader.java

private void readField(ByteBuffer buf, int fieldID) {
    if (mRequiredColumns == null || (mRequiredColumns.length > fieldID && mRequiredColumns[fieldID])) {
        byte[] bytes = new byte[buf.position()];
        buf.rewind();
        buf.get(bytes, 0, bytes.length);
        mProtoTuple.add(new DataByteArray(bytes));
    }//from  w  ww.j a  v  a  2 s.c  om
    buf.clear();
}

From source file:com.sm.connector.server.ServerStore.java

protected Pair<byte[], byte[]> next(int current, ByteBuffer buf) throws IOException {
    buf.clear();/* w ww. j  a  va 2  s . com*/
    long pos = OFFSET + (long) current * RECORD_SIZE;
    indexChannel.read(buf, pos);
    buf.rewind();
    byte status = buf.get();
    if (isDeleted(status)) {
        return null;
    } else {
        long keyLen = buf.getLong();
        byte[] keys = readChannel(keyLen, keyChannel);
        long data = buf.getLong();
        long block2version = buf.getLong();
        CacheBlock block = new CacheBlock(current, data, block2version, status);
        if (block.getDataOffset() <= 0 || block.getDataLen() <= 0
                || block.getBlockSize() < block.getDataLen()) {
            throw new StoreException("data reading error");
        } else {
            //Key key = toKey(keys);
            byte[] datas = readChannel(block.getDataOffset2Len(), dataChannel);
            return new Pair(keys, datas);
        }
    }
}

From source file:gobblin.util.io.StreamUtilsTest.java

private void verifyBuffer(byte[] srcBytes, ByteBuffer buffer) throws IOException {
    buffer.put(srcBytes);/*from   w w  w .  j a v  a 2 s . c  om*/
    buffer.flip();

    ByteArrayOutputStream bOs = new ByteArrayOutputStream();
    StreamUtils.byteBufferToOutputStream(buffer, bOs);
    Assert.assertEquals(bOs.toByteArray(), srcBytes);

    bOs = new ByteArrayOutputStream();
    buffer.rewind();

    // consume one character from the buf; make sure it is not included in the output by
    // byteBufferToOutputStream
    buffer.getChar();
    StreamUtils.byteBufferToOutputStream(buffer, bOs);
    byte[] offByTwo = bOs.toByteArray();
    Assert.assertEquals(offByTwo.length, srcBytes.length - 2);
    for (int i = 0; i < offByTwo.length; i++) {
        Assert.assertEquals(offByTwo[i], srcBytes[i + 2]);
    }
}

From source file:fr.cls.atoll.motu.processor.wps.StringList.java

public static void print(ByteBuffer bb) {
    while (bb.hasRemaining()) {
        System.out.print(bb.get() + " ");
    }//from   w  ww.j a  v a 2s .  com
    System.out.println();
    bb.rewind();
}

From source file:org.apache.hadoop.io.TestIOUtils.java

@Test
public void testWriteFully() throws IOException {
    final int INPUT_BUFFER_LEN = 10000;
    final int HALFWAY = 1 + (INPUT_BUFFER_LEN / 2);
    byte[] input = new byte[INPUT_BUFFER_LEN];
    for (int i = 0; i < input.length; i++) {
        input[i] = (byte) (i & 0xff);
    }// ww  w . j  a  v  a  2 s  .  com
    byte[] output = new byte[input.length];

    try {
        RandomAccessFile raf = new RandomAccessFile(TEST_FILE_NAME, "rw");
        FileChannel fc = raf.getChannel();
        ByteBuffer buf = ByteBuffer.wrap(input);
        IOUtils.writeFully(fc, buf);
        raf.seek(0);
        raf.read(output);
        for (int i = 0; i < input.length; i++) {
            assertEquals(input[i], output[i]);
        }
        buf.rewind();
        IOUtils.writeFully(fc, buf, HALFWAY);
        for (int i = 0; i < HALFWAY; i++) {
            assertEquals(input[i], output[i]);
        }
        raf.seek(0);
        raf.read(output);
        for (int i = HALFWAY; i < input.length; i++) {
            assertEquals(input[i - HALFWAY], output[i]);
        }
    } finally {
        File f = new File(TEST_FILE_NAME);
        if (f.exists()) {
            f.delete();
        }
    }
}

From source file:jext2.Superblock.java

private ByteBuffer allocateByteBuffer() {
    ByteBuffer buf = ByteBuffer.allocate(this.blocksize);
    buf.rewind();
    return buf;//w w  w. ja  v a 2  s  . co  m
}