Example usage for java.nio ByteBuffer mark

List of usage examples for java.nio ByteBuffer mark

Introduction

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

Prototype

public final Buffer mark() 

Source Link

Document

Marks the current position, so that the position may return to this point later by calling reset().

Usage

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Resets the <code>ByteBuffer</code> position and adds the opcode and
 * version values to it.//from ww  w  .j av a2 s  .c  o  m
 *
 * @param aBuffer Packet byte buffer object.
 * @param anOpCode Application specific operation code value.
 * @param aVersion Application specific operation version code value.
 */
public static void setHeader(ByteBuffer aBuffer, int anOpCode, int aVersion) {
    if (aBuffer != null) {
        aBuffer.mark();
        aBuffer.position(0);
        aBuffer.putInt(anOpCode);
        aBuffer.putInt(aVersion);
        aBuffer.reset();
    }
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Retrieves the operation code stored within the header of the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @return Application specific operation code value.
 *///from   w  ww  . j a  v  a 2  s  . c om
public static int getOpCode(ByteBuffer aBuffer) {
    int opCode;

    if (aBuffer == null)
        return -1;
    else {
        aBuffer.mark();
        aBuffer.position(0);
        opCode = aBuffer.getInt();
        aBuffer.reset();

        return opCode;
    }
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Retrieves the operation version code stored within the header of the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @return Application specific operation code version value.
 *//*from ww  w  .j a va  2s. c  om*/
@SuppressWarnings({ "UnusedAssignment" })
public static int getVersion(ByteBuffer aBuffer) {
    int opCode, versionId;

    if (aBuffer == null)
        return -1;
    else {
        aBuffer.mark();
        aBuffer.position(0);
        opCode = aBuffer.getInt();
        versionId = aBuffer.getInt();
        aBuffer.reset();

        return versionId;
    }
}

From source file:com.stratagis.geoevent.adapter.nmeaplus.NmeaPlusInboundAdapter.java

private static List<byte[]> index(ByteBuffer in) {

    List<byte[]> messages = new ArrayList<byte[]>();
    for (int i = -1; in.hasRemaining();) {
        byte b = in.get();
        if (b == ((byte) '$')) // bom
        {/*from  w ww .jav a 2 s  . c o m*/
            i = in.position();
            in.mark();
        } else if (b == ((byte) '\r') || b == ((byte) '\n')) // eom
        {
            if (i != -1) {
                byte[] message = new byte[in.position() - 1 - i];
                System.arraycopy(in.array(), i, message, 0, message.length);
                messages.add(message);
            }
            i = -1;
            in.mark();
        } else if (messages.isEmpty() && i == -1)
            in.mark();
    }
    return messages;
}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.java

public static int findText(Text text, Text subtext, int start) {
    if (start < 0)
        return -1;

    ByteBuffer src = ByteBuffer.wrap(text.getBytes(), 0, text.getLength());
    ByteBuffer tgt = ByteBuffer.wrap(subtext.getBytes(), 0, subtext.getLength());
    byte b = tgt.get();
    src.position(start);//from  w  w w  .  j a v a 2 s .c o  m

    while (src.hasRemaining()) {
        if (b == src.get()) {
            src.mark();
            tgt.mark();
            boolean found = true;
            int pos = src.position() - 1;
            while (tgt.hasRemaining()) {
                if (!src.hasRemaining()) {
                    tgt.reset();
                    src.reset();
                    found = false;
                    break;
                }
                if (!(tgt.get() == src.get())) {
                    tgt.reset();
                    src.reset();
                    found = false;
                    break;
                }
            }
            if (found)
                return pos;
        }
    }
    return -1;
}

From source file:io.Text.java

/**
 * Returns the next code point at the current position in
 * the buffer. The buffer's position will be incremented.
 * Any mark set on this buffer will be changed by this method!
 *///from  w  ww. ja va2s. co m
public static int bytesToCodePoint(ByteBuffer bytes) {
    bytes.mark();
    byte b = bytes.get();
    bytes.reset();
    int extraBytesToRead = bytesFromUTF8[(b & 0xFF)];
    if (extraBytesToRead < 0)
        return -1; // trailing byte!
    int ch = 0;

    switch (extraBytesToRead) {
    case 5:
        ch += (bytes.get() & 0xFF);
        ch <<= 6; /* remember, illegal UTF-8 */
    case 4:
        ch += (bytes.get() & 0xFF);
        ch <<= 6; /* remember, illegal UTF-8 */
    case 3:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 2:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 1:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 0:
        ch += (bytes.get() & 0xFF);
    }
    ch -= offsetsFromUTF8[extraBytesToRead];

    return ch;
}

From source file:org.apache.arrow.vector.util.Text.java

/**
 * Returns the next code point at the current position in the buffer. The buffer's position will be incremented. Any
 * mark set on this buffer will be changed by this method!
 *///from  w w  w. j  a va2s .  c o  m
public static int bytesToCodePoint(ByteBuffer bytes) {
    bytes.mark();
    byte b = bytes.get();
    bytes.reset();
    int extraBytesToRead = bytesFromUTF8[(b & 0xFF)];
    if (extraBytesToRead < 0) {
        return -1; // trailing byte!
    }
    int ch = 0;

    switch (extraBytesToRead) {
    case 5:
        ch += (bytes.get() & 0xFF);
        ch <<= 6; /* remember, illegal UTF-8 */
    case 4:
        ch += (bytes.get() & 0xFF);
        ch <<= 6; /* remember, illegal UTF-8 */
    case 3:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 2:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 1:
        ch += (bytes.get() & 0xFF);
        ch <<= 6;
    case 0:
        ch += (bytes.get() & 0xFF);
    }
    ch -= offsetsFromUTF8[extraBytesToRead];

    return ch;
}

From source file:org.commoncrawl.service.queryserver.master.S3Helper.java

static int scanForGZIPHeader(ByteBuffer byteBuffer) throws IOException {

    LOG.info("*** SCANNING FOR GZIP MAGIC Bytes:" + Byte.toString((byte) StreamingArcFileReader.GZIP_MAGIC)
            + " " + Byte.toString((byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) + " BufferSize is:"
            + byteBuffer.limit() + " Remaining:" + byteBuffer.remaining());
    int limit = byteBuffer.limit();

    while (byteBuffer.position() + 2 < limit) {
        //LOG.info("Reading Byte At:"+ byteBuffer.position());
        int b = byteBuffer.get();
        //LOG.info("First Byte is:"+ b);
        if (b == (byte) (StreamingArcFileReader.GZIP_MAGIC)) {

            byteBuffer.mark();

            byte b2 = byteBuffer.get();
            //LOG.info("Second Byte is:"+ b2);
            if (b2 == (byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) {

                byte b3 = byteBuffer.get();
                if (b3 == Deflater.DEFLATED) {
                    LOG.info("Found GZip Magic at:" + (byteBuffer.position() - 3));
                    return byteBuffer.position() - 3;
                }//from w ww  .j a v a 2 s  .  c  om
            }
            byteBuffer.reset();
        }
    }
    LOG.error("Failed to Find GZIP Magic!!");
    //LOG.error(Arrays.toString(byteBuffer.array()));
    return -1;
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks).// ww w.  j a va2s.  c o m
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * 24 + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * 24);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * 24);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[24];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = ByteBuffer.wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./*from www  .  j  a  va 2s.c  om*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[TarEntry.SIZE];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}