Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get();

Source Link

Document

Returns the byte at the current position and increases the position by 1.

Usage

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

static ByteBuffer findFreeRowSpace(UsageMap ownedPages, UsageMap freeSpacePages, TempPageHolder rowBufferH)
        throws IOException {
    // find last data page (Not bothering to check other pages for free
    // space.)/*from   w w w.  ja  v  a  2s.c  om*/
    UsageMap.PageCursor revPageCursor = ownedPages.cursor();
    revPageCursor.afterLast();
    while (true) {
        int tmpPageNumber = revPageCursor.getPreviousPage();
        if (tmpPageNumber < 0) {
            break;
        }
        // only use if actually listed in free space pages
        if (!freeSpacePages.containsPageNumber(tmpPageNumber)) {
            continue;
        }
        ByteBuffer dataPage = rowBufferH.setPage(ownedPages.getPageChannel(), tmpPageNumber);
        if (dataPage.get() == PageTypes.DATA) {
            // found last data page with free space
            return dataPage;
        }
    }

    return null;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

public RequestPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);//from  w  w  w.  jav  a 2  s. c  o  m
    int exactLength = bbuf.position();

    this.requestID = bbuf.getLong();
    this.stop = bbuf.get() == (byte) 1;
    exactLength += (8 + 1);

    // addresses
    byte[] ca = new byte[4];
    bbuf.get(ca);
    int cport = (int) bbuf.getShort();
    cport = cport >= 0 ? cport : cport + 2 * (Short.MAX_VALUE + 1);
    this.clientAddress = cport != 0 ? new InetSocketAddress(InetAddress.getByAddress(ca), cport) : null;
    byte[] la = new byte[4];
    bbuf.get(la);
    int lport = (int) bbuf.getShort();
    lport = lport >= 0 ? lport : lport + 2 * (Short.MAX_VALUE + 1);
    this.listenAddress = lport != 0 ? new InetSocketAddress(InetAddress.getByAddress(la), lport) : null;
    exactLength += (4 + 2 + 4 + 2);

    // other non-final fields
    this.entryReplica = bbuf.getInt();
    this.entryTime = bbuf.getLong();
    this.shouldReturnRequestValue = bbuf.get() == (byte) 1;
    this.forwardCount = bbuf.getInt();
    exactLength += (4 + 8 + 1 + 4);

    // digest related fields
    this.broadcasted = bbuf.get() == (byte) 1;
    int digestLength = bbuf.getInt();
    if (digestLength > 0)
        bbuf.get(this.digest = new byte[digestLength]);

    // highly variable length fields

    // requestValue
    int reqValLen = bbuf.getInt();
    byte[] reqValBytes = new byte[reqValLen];
    bbuf.get(reqValBytes);
    this.requestValue = reqValBytes.length > 0 ? new String(reqValBytes, CHARSET) : null;
    exactLength += (4 + reqValBytes.length);

    // responseValue
    int respValLen = bbuf.getInt();
    byte[] respValBytes = new byte[respValLen];
    bbuf.get(respValBytes);
    this.responseValue = respValBytes.length > 0 ? new String(respValBytes, CHARSET) : null;
    exactLength += (4 + respValBytes.length);

    int numBatched = bbuf.getInt();
    if (numBatched == 0)
        return;
    // else
    // batched requests
    this.batched = new RequestPacket[numBatched];
    for (int i = 0; i < numBatched; i++) {
        int len = bbuf.getInt();
        byte[] element = new byte[len];
        bbuf.get(element);
        this.batched[i] = new RequestPacket(element);
    }
    assert (exactLength > 0);
}

From source file:edu.hawaii.soest.kilonalu.tchain.TChainSource.java

/**
 * A method that executes the streaming of data from the source to the RBNB
 * server after all configuration of settings, connections to hosts, and
 * thread initiatizing occurs.  This method contains the detailed code for 
 * streaming the data and interpreting the stream.
 *//*from  w  ww  . j  a va 2 s.  c o m*/
protected boolean execute() {
    logger.debug("TChainSource.execute() called.");
    // do not execute the stream if there is no connection
    if (!isConnected())
        return false;

    boolean failed = false;

    SocketChannel socket = getSocketConnection();

    // while data are being sent, read them into the buffer
    try {
        // create four byte placeholders used to evaluate up to a four-byte 
        // window.  The FIFO layout looks like:
        //           -------------------------
        //   in ---> | One | Two |Three|Four |  ---> out
        //           -------------------------
        byte byteOne = 0x00, // set initial placeholder values
                byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00;

        // Create a buffer that will store the sample bytes as they are read
        ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize());

        // create a byte buffer to store bytes from the TCP stream
        ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize());

        // add a channel of data that will be pushed to the server.  
        // Each sample will be sent to the Data Turbine as an rbnb frame.
        ChannelMap rbnbChannelMap = new ChannelMap();

        // while there are bytes to read from the socket ...
        while (socket.read(buffer) != -1 || buffer.position() > 0) {

            // prepare the buffer for reading
            buffer.flip();

            // while there are unread bytes in the ByteBuffer
            while (buffer.hasRemaining()) {
                byteOne = buffer.get();
                logger.debug("char: " + (char) byteOne + "\t" + "b1: "
                        + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: "
                        + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: "
                        + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: "
                        + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: "
                        + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t"
                        + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t"
                        + "buffer rem: " + buffer.remaining() + "\t" + "state: " + state);

                // Use a State Machine to process the byte stream.
                // Start building an rbnb frame for the entire sample, first by 
                // inserting a timestamp into the channelMap.  This time is merely
                // the time of insert into the data turbine, not the time of
                // observations of the measurements.  That time should be parsed out
                // of the sample in the Sink client code

                switch (state) {

                case 0:

                    // sample line ending is '\r\n' (carraige return, newline)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) {
                        // we've found the end of a sample, move on
                        state = 1;
                        break;

                    } else {
                        break;
                    }

                case 1: // read the rest of the bytes to the next EOL characters

                    // sample line is terminated by record delimiter bytes (usually \r\n or \n)
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) {

                        // rewind the sample to overwrite the line ending so we can add
                        // in the timestamp (then add the line ending)
                        sampleBuffer.position(sampleBuffer.position() - 1);
                        --sampleByteCount;

                        // add the delimiter to the end of the sample.
                        byte[] delimiterAsBytes = getFieldDelimiter().getBytes("US-ASCII");

                        for (byte delim : delimiterAsBytes) {
                            sampleBuffer.put(delim);
                            sampleByteCount++;
                        }

                        // then add a timestamp to the end of the sample
                        DATE_FORMAT.setTimeZone(TZ);
                        byte[] sampleDateAsBytes = DATE_FORMAT.format(new Date()).getBytes("US-ASCII");
                        for (byte b : sampleDateAsBytes) {
                            sampleBuffer.put(b);
                            sampleByteCount++;
                        }

                        // add the last two bytes found (usually \r\n) to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                            sampleByteCount++;
                            sampleBuffer.put(byteTwo);
                            sampleByteCount++;

                        } else {
                            sampleBuffer.compact();
                            sampleBuffer.put(byteOne);
                            sampleByteCount++;
                            sampleBuffer.put(byteTwo);
                            sampleByteCount++;

                        }

                        // extract just the length of the sample bytes out of the
                        // sample buffer, and place it in the channel map as a 
                        // byte array.  Then, send it to the data turbine.
                        byte[] sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        // send the sample to the data turbine
                        rbnbChannelMap.PutTimeAuto("server");
                        String sampleString = new String(sampleArray, "US-ASCII");
                        int channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
                        rbnbChannelMap.PutMime(channelIndex, "text/plain");
                        rbnbChannelMap.PutDataAsString(channelIndex, sampleString);
                        getSource().Flush(rbnbChannelMap);
                        logger.info("Sample: " + sampleString.substring(0, sampleString.length() - 2)
                                + " sent data to the DataTurbine. ");

                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;
                        sampleBuffer.clear();
                        sampleByteCount = 0;
                        rbnbChannelMap.Clear();
                        logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap.");
                        //state = 0;

                    } else { // not 0x0D20

                        // still in the middle of the sample, keep adding bytes
                        sampleByteCount++; // add each byte found

                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                        } else {
                            sampleBuffer.compact();
                            logger.debug("Compacting sampleBuffer ...");
                            sampleBuffer.put(byteOne);

                        }

                        break;
                    } // end if for 0x0D20 EOL

                } // end switch statement

                // shift the bytes in the FIFO window
                byteFour = byteThree;
                byteThree = byteTwo;
                byteTwo = byteOne;

            } //end while (more unread bytes)

            // prepare the buffer to read in more bytes from the stream
            buffer.compact();

        } // end while (more socket bytes to read)
        socket.close();

    } catch (IOException e) {
        // handle exceptions
        // In the event of an i/o exception, log the exception, and allow execute()
        // to return false, which will prompt a retry.
        failed = true;
        e.printStackTrace();
        return !failed;
    } catch (SAPIException sapie) {
        // In the event of an RBNB communication  exception, log the exception, 
        // and allow execute() to return false, which will prompt a retry.
        failed = true;
        sapie.printStackTrace();
        return !failed;
    }

    return !failed;
}

From source file:com.woodcomputing.bobbin.format.JEFFormat.java

@Override
public Design load(File file) {

    byte[] bytes = null;
    try (InputStream is = new FileInputStream(file)) {
        bytes = IOUtils.toByteArray(is);
    } catch (IOException ex) {
        log.catching(ex);/*from  ww  w  .  ja  va 2 s. c  o  m*/
    }
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    JEF jef = new JEF();
    jef.setFirstStitchLocation(bb.getInt(0));
    log.debug("First Stitch Location: {}", jef.getFirstStitchLocation());
    jef.setThreadChangeCount(bb.getInt(24));
    log.debug("Threads Changes: {}", jef.getThreadChangeCount());
    jef.setHoop(Hoop.id2Hoop(bb.getInt(32)));
    log.debug("Hoop: {}", jef.getHoop());
    jef.setStitchCount(bb.getInt(28));
    log.debug("Stitch Count: {}", jef.getStitchCount());

    bb.position(116);
    JEFColor[] colors = new JEFColor[jef.getThreadChangeCount()];
    for (int i = 0; i < jef.getThreadChangeCount(); i++) {
        colors[i] = jefColorMap.get(bb.getInt());
    }
    jef.setThreadColors(colors);
    for (int i = 0; i < jef.getThreadChangeCount(); i++) {
        log.debug("ThreadType{}: {}", i, bb.getInt());
    }

    int dx = 0;
    int dy = 0;
    int cx = 0;
    int cy = 0;
    int nx = 0;
    int ny = 0;

    int change = 1;
    int stitches = 0;
    boolean isMove = false;
    bb.position(jef.getFirstStitchLocation());
    JEFColor color = jef.getThreadColors()[change - 1];
    Design design = new Design();
    StitchGroup stitchGroup = new StitchGroup();
    stitchGroup.setColor(color.getRgb());

    for (int stitch = 1; stitch < jef.getStitchCount(); stitch++) {
        dx = bb.get();
        dy = bb.get();
        if (dx == -128) {
            switch (dy) {
            case 1:
                log.debug("change: {}", bb.position());
                change++;
                color = jef.getThreadColors()[change - 1];
                design.getStitchGroups().add(stitchGroup);
                stitchGroup = new StitchGroup();
                stitchGroup.setColor(color.getRgb());
                //                        bb.get();
                //                        bb.get();
                continue;
            case 2:
                //                        log.debug("move");
                isMove = true;
                break;
            case 16:
                log.debug("last");
                isMove = true;
                break;
            }
        } else {
            nx = cx + dx;
            ny = cy + dy;
            if (isMove) {
                isMove = false;
            }
            //                } else {
            //                    log.debug("stitch");
            stitches++;
            Stitch designStitch = new Stitch(cx, -cy, nx, -ny);
            stitchGroup.getStitches().add(designStitch);
            //                }
            cx = nx;
            cy = ny;
        }
    }
    log.debug("Changes: {} Stitches {} End: {}", change, stitches, bb.position());
    return design;
}

From source file:edu.hawaii.soest.hioos.storx.StorXParser.java

/**
 * Parses the binary STOR-X file.  The binary file format is a sequence of
 * 'frames' that all begin with 'SAT'.  The parser creates a list with the
 * individual frames.  Some frames are StorX frames (SATSTX), some are from 
 * external sensors (ISUS: 'SATNLB', 'SATNDB'; SBE CTD: 'SATSBE')
 *
 * @param fileBuffer - the binary data file as a ByteBuffer
 *///w w  w  .jav  a2 s. com
public void parse(ByteBuffer fileBuffer) throws Exception {

    logger.debug("StorXParser.parse() called.");

    this.fileBuffer = fileBuffer;
    //logger.debug(this.fileBuffer.toString());

    try {

        // Create a buffer that will store a single frame of the file
        ByteBuffer frameBuffer = ByteBuffer.allocate(1024);

        // create four byte placeholders used to evaluate up to a four-byte 
        // window.  The FIFO layout looks like:
        //           ---------------------------
        //   in ---> | Four | Three | Two | One |  ---> out
        //           ---------------------------
        byte byteOne = 0x00, // set initial placeholder values
                byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00;

        int frameByteCount = 0; // keep track of bytes per frame
        int frameCount = 0; // keep track of frames

        this.fileBuffer.position(0);
        this.fileBuffer.limit(this.fileBuffer.capacity());

        while (this.fileBuffer.hasRemaining()) {

            // load the next byte into the FIFO window
            byteOne = fileBuffer.get();

            // show the byte stream coming in
            //logger.debug("b1: " + new String(Hex.encodeHex(new byte[]{byteOne}))   + "\t" +
            //             "b2: " + new String(Hex.encodeHex(new byte[]{byteTwo}))   + "\t" +
            //             "b3: " + new String(Hex.encodeHex(new byte[]{byteThree})) + "\t" +
            //             "b4: " + new String(Hex.encodeHex(new byte[]{byteFour}))  + "\t" +
            //             "st: " + Integer.toString(this.state)                     + "\t" +
            //             "po: " + this.fileBuffer.position()                       + "\t" +
            //             "cp: " + this.fileBuffer.capacity()
            //             );

            // evaluate the bytes, separate the file frame by frame (SAT ...)
            switch (this.state) {

            case 0: // find a frame beginning (SAT) 53 41 54

                if (byteOne == 0x54 && byteTwo == 0x41 && byteThree == 0x53) {

                    // found a line, add the beginning to the line buffer 
                    frameBuffer.put(byteThree);
                    frameBuffer.put(byteTwo);
                    frameBuffer.put(byteOne);

                    frameByteCount = frameByteCount + 3;

                    this.state = 1;
                    break;

                } else {
                    break;

                }

            case 1: // find the next frame beginning (SAT) 53 41 54

                if ((byteOne == 0x54 && byteTwo == 0x41 && byteThree == 0x53)
                        || fileBuffer.position() == fileBuffer.capacity()) {

                    // we have a line ending. store the line in the arrayList
                    frameBuffer.put(byteOne);
                    frameByteCount++;
                    frameBuffer.flip();
                    byte[] frameArray = frameBuffer.array();
                    ByteBuffer currentFrameBuffer;

                    if (fileBuffer.position() == fileBuffer.capacity()) {

                        // create a true copy of the byte array subset (no trailing 'SAT')
                        byte[] frameCopy = new byte[frameByteCount];
                        System.arraycopy(frameArray, 0, frameCopy, 0, frameByteCount);
                        currentFrameBuffer = ByteBuffer.wrap(frameCopy);

                    } else {

                        // create a true copy of the byte array subset (less the 'SAT')
                        byte[] frameCopy = new byte[frameByteCount - 3];
                        System.arraycopy(frameArray, 0, frameCopy, 0, frameByteCount - 3);
                        currentFrameBuffer = ByteBuffer.wrap(frameCopy);

                    }

                    // parse the current frame and add it to the frameMap

                    frameCount++;

                    // create a map to store frames as they are encountered
                    BasicHierarchicalMap frameMap = new BasicHierarchicalMap();

                    // peek at the first six header bytes as a string
                    byte[] sixBytes = new byte[6];
                    currentFrameBuffer.get(sixBytes);
                    currentFrameBuffer.position(0);
                    String frameHeader = new String(sixBytes, "US-ASCII");

                    // determine the frame type based on the header
                    if (frameHeader.matches(this.STOR_X_HEADER_ID)) {
                        frameMap.put("rawFrame", currentFrameBuffer);
                        frameMap.put("id", frameHeader);
                        frameMap.put("type", frameHeader.substring(3, 6));
                        frameMap.put("serialNumber", null);
                        frameMap.put("date", null);
                        String headerString = new String(currentFrameBuffer.array());
                        // trim trailing null characters and line endings
                        int nullIndex = headerString.indexOf(0);
                        headerString = headerString.substring(0, nullIndex).trim();
                        frameMap.put("parsedFrameObject", headerString);

                        // Add the frame to the frames map
                        this.framesMap.add("/frames/frame", (BasicHierarchicalMap) frameMap.clone());

                        frameMap.removeAll("frame");
                        currentFrameBuffer.clear();

                    } else if (frameHeader.matches(this.STOR_X_FRAME_ID)) {

                        // test if the frame is complete
                        if (currentFrameBuffer.capacity() == this.STOR_X_FRAME_SIZE) {

                            // convert the frame buffer to a StorXFrame
                            StorXFrame storXFrame = new StorXFrame(currentFrameBuffer);

                            frameMap.put("rawFrame", currentFrameBuffer);
                            frameMap.put("id", frameHeader);
                            frameMap.put("type", frameHeader.substring(3, 6));
                            frameMap.put("serialNumber", storXFrame.getSerialNumber());
                            frameMap.put("date", parseTimestamp(storXFrame.getTimestamp()));
                            frameMap.put("parsedFrameObject", storXFrame);

                            // Add the frame to the frames map
                            this.framesMap.add("/frames/frame", (BasicHierarchicalMap) frameMap.clone());

                            frameMap.removeAll("frame");
                            currentFrameBuffer.clear();

                        } else {
                            logger.debug(frameHeader + " frame " + frameCount + " length is "
                                    + currentFrameBuffer.capacity() + " not " + this.STOR_X_FRAME_SIZE);
                        }

                    } else if (frameHeader.matches(this.SBE_CTD_FRAME_ID)) {

                        // convert the frame buffer to a CTDFrame
                        CTDFrame ctdFrame = new CTDFrame(currentFrameBuffer);

                        // add in a sample if it matches a general data sample pattern
                        if (ctdFrame.getSample().matches(" [0-9].*[0-9]\r\n")) {

                            // extract the sample bytes from the frame
                            frameMap.put("rawFrame", currentFrameBuffer);
                            frameMap.put("id", frameHeader);
                            frameMap.put("type", frameHeader.substring(3, 6));
                            frameMap.put("serialNumber", ctdFrame.getSerialNumber());
                            frameMap.put("date", parseTimestamp(ctdFrame.getTimestamp()));
                            frameMap.put("parsedFrameObject", ctdFrame);

                            // Add the frame to the frames map
                            this.framesMap.add("/frames/frame", (BasicHierarchicalMap) frameMap.clone());

                        } else {
                            logger.debug("This CTD frame is not a data sample."
                                    + " Skipping it. The string is: " + ctdFrame.getSample());
                        }

                        frameMap.removeAll("frame");
                        currentFrameBuffer.clear();

                    } else if (frameHeader.matches(this.ISUS_DARK_FRAME_ID)) {

                        // test if the frame is complete
                        if (currentFrameBuffer.capacity() == this.ISUS_FRAME_SIZE) {

                            // convert the frame buffer to a ISUSFrame
                            ISUSFrame isusFrame = new ISUSFrame(currentFrameBuffer);

                            frameMap.put("rawFrame", currentFrameBuffer);
                            frameMap.put("id", frameHeader);
                            frameMap.put("type", frameHeader.substring(3, 6));
                            frameMap.put("serialNumber", isusFrame.getSerialNumber());
                            frameMap.put("date", parseTimestamp(isusFrame.getTimestamp()));
                            frameMap.put("parsedFrameObject", isusFrame);

                            // Add the frame to the frames map
                            this.framesMap.add("/frames/frame", (BasicHierarchicalMap) frameMap.clone());

                            frameMap.removeAll("frame");
                            currentFrameBuffer.clear();

                        } else {
                            logger.debug(frameHeader + " frame " + frameCount + " length is "
                                    + currentFrameBuffer.capacity() + " not " + this.ISUS_FRAME_SIZE);
                        }

                        currentFrameBuffer.clear();

                    } else if (frameHeader.matches(this.ISUS_LIGHT_FRAME_ID)) {

                        // test if the frame is complete
                        if (currentFrameBuffer.capacity() == this.ISUS_FRAME_SIZE) {

                            // convert the frame buffer to a ISUSFrame
                            ISUSFrame isusFrame = new ISUSFrame(currentFrameBuffer);

                            frameMap.put("rawFrame", currentFrameBuffer);
                            frameMap.put("id", frameHeader);
                            frameMap.put("type", frameHeader.substring(3, 6));
                            frameMap.put("serialNumber", isusFrame.getSerialNumber());
                            frameMap.put("date", parseTimestamp(isusFrame.getTimestamp()));
                            frameMap.put("parsedFrameObject", isusFrame);

                            // Add the frame to the frames map
                            this.framesMap.add("/frames/frame", (BasicHierarchicalMap) frameMap.clone());

                            frameMap.removeAll("frame");
                            currentFrameBuffer.clear();

                        } else {
                            logger.debug(frameHeader + " frame " + frameCount + " length is "
                                    + currentFrameBuffer.capacity() + " not " + this.ISUS_FRAME_SIZE);
                        }

                        currentFrameBuffer.clear();

                    } else {
                        logger.info("The current frame type is not recognized. "
                                + "Discarding it.  The header was: " + frameHeader);
                        currentFrameBuffer.clear();

                    }

                    // reset the frame buffer for the next frame, but add the 'SAT'
                    // bytes already encountered
                    frameBuffer.clear();
                    frameByteCount = 0;
                    this.fileBuffer.position(this.fileBuffer.position() - 3);
                    this.state = 0;
                    break;

                } else {

                    // no full line yet, keep adding bytes
                    frameBuffer.put(byteOne);
                    frameByteCount++;
                    break;

                }

            } // end switch()

            // shift the bytes in the FIFO window
            byteFour = byteThree;
            byteThree = byteTwo;
            byteTwo = byteOne;

        } // end while()

        logger.debug(this.framesMap.toXMLString(1000));

    } catch (Exception e) {
        logger.debug("Failed to parse the data file.  The error message was:" + e.getMessage());
        e.printStackTrace();

    }

}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

/**
 * Decodes a NUMERIC field.// www.j  a v a  2 s.  c o m
 */
private BigDecimal readNumericValue(ByteBuffer buffer) {
    boolean negate = (buffer.get() != 0);

    byte[] tmpArr = ByteUtil.getBytes(buffer, 16);

    if (buffer.order() != ByteOrder.BIG_ENDIAN) {
        fixNumericByteOrder(tmpArr);
    }

    return toBigDecimal(tmpArr, negate, getScale());
}

From source file:edu.hawaii.soest.kilonalu.adcp.ADCPSource.java

/**
 * A method that executes the streaming of data from the source to the RBNB
 * server after all configuration of settings, connections to hosts, and
 * thread initiatizing occurs.  This method contains the detailed code for 
 * streaming the data and interpreting the stream.
 *///from  w  w w.  jav a2  s.  com
protected boolean execute() {

    // do not execute the stream if there is no connection
    if (!isConnected())
        return false;

    boolean failed = false;

    SocketChannel socket = getSocketConnection();

    // while data are being sent, read them into the buffer
    try {
        // create four byte placeholders used to evaluate up to a four-byte 
        // window.  The FIFO layout looks like:
        //           -------------------------
        //   in ---> | One | Two |Three|Four |  ---> out
        //           -------------------------
        byte byteOne = 0x00, // set initial placeholder values
                byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00;

        // Create a buffer that will store the ensemble bytes as they are read
        ByteBuffer ensembleBuffer = ByteBuffer.allocate(getBufferSize());

        // create a byte buffer to store bytes from the TCP stream
        ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize());

        // add a channel of data that will be pushed to the server.  
        // Each ensemble will be sent to the Data Turbine as an rbnb frame.
        ChannelMap rbnbChannelMap = new ChannelMap();
        int channelIndex = rbnbChannelMap.Add(getRBNBChannelName());

        // while there are bytes to read from the socket ...
        while (socket.read(buffer) != -1 || buffer.position() > 0) {
            // prepare the buffer for reading
            buffer.flip();

            // while there are unread bytes in the ByteBuffer
            while (buffer.hasRemaining()) {
                byteOne = buffer.get();

                // Use a State Machine to process the byte stream.
                // Start building an rbnb frame for the entire ensemble, first by 
                // inserting a timestamp into the channelMap.  This time is merely
                // the time of insert into the data turbine, not the time of
                // observations of the measurements.  That time should be parsed out
                // of the ensemble in the Sink client code

                System.out.print("\rProcessed byte # " + ensembleByteCount + " "
                        + new String(Hex.encodeHex((new byte[] { byteOne }))) + " - log msg is: ");

                switch (state) {

                case 0: // find ensemble header id
                    if (byteOne == 0x7F && byteTwo == 0x7F) {
                        ensembleByteCount++; // add Header ID
                        ensembleChecksum += (byteTwo & 0xFF);
                        ensembleByteCount++; // add Data Source ID
                        ensembleChecksum += (byteOne & 0xFF);

                        state = 1;
                        break;

                    } else {
                        break;

                    }

                case 1: // find the Ensemble Length (LSB)
                    ensembleByteCount++; // add Ensemble Byte Count (LSB)
                    ensembleChecksum += (byteOne & 0xFF);

                    state = 2;
                    break;

                case 2: // find the Ensemble Length (MSB)
                    ensembleByteCount++; // add Ensemble Byte Count (MSB)
                    ensembleChecksum += (byteOne & 0xFF);

                    int upperEnsembleByte = (byteOne & 0xFF) << 8;
                    int lowerEnsembleByte = (byteTwo & 0xFF);
                    ensembleBytes = upperEnsembleByte + lowerEnsembleByte;
                    logger.debug("Number of Bytes in the Ensemble: " + ensembleBytes);

                    if (ensembleBuffer.remaining() > 0) {

                        ensembleBuffer.put(byteFour);
                        ensembleBuffer.put(byteThree);
                        ensembleBuffer.put(byteTwo);
                        ensembleBuffer.put(byteOne);
                    } else {

                        ensembleBuffer.compact();
                        ensembleBuffer.put(byteFour);
                        ensembleBuffer.put(byteThree);
                        ensembleBuffer.put(byteTwo);
                        ensembleBuffer.put(byteOne);
                    }

                    state = 3;
                    break;

                // verify that the header is real, not a random 0x7F7F
                case 3: // find the number of data types in the ensemble

                    // set the numberOfDataTypes byte
                    if (ensembleByteCount == NUMBER_OF_DATA_TYPES_OFFSET - 1) {
                        ensembleByteCount++;
                        ensembleChecksum += (byteOne & 0xFF);
                        numberOfDataTypes = (byteOne & 0xFF);
                        // calculate the number of bytes to the Fixed Leader ID
                        dataTypeOneOffset = 6 + (2 * numberOfDataTypes);

                        if (ensembleBuffer.remaining() > 0) {
                            ensembleBuffer.put(byteOne);

                        } else {
                            ensembleBuffer.compact();
                            ensembleBuffer.put(byteOne);

                        }
                        state = 4;
                        break;

                    } else {
                        ensembleByteCount++;
                        ensembleChecksum += (byteOne & 0xFF);

                        if (ensembleBuffer.remaining() > 0) {
                            ensembleBuffer.put(byteOne);

                        } else {
                            ensembleBuffer.compact();
                            ensembleBuffer.put(byteOne);

                        }

                        break;
                    }

                case 4: // find the offset to data type #1 and verify the header ID
                    if ((ensembleByteCount == dataTypeOneOffset + 1) && byteOne == 0x00 && byteTwo == 0x00) {
                        ensembleByteCount++;
                        ensembleChecksum += (byteOne & 0xFF);
                        // we are confident that the previous sequence of 0x7F7F is truly
                        // an headerID and not a random occurrence in the stream because
                        // we have identified the Fixed Leader ID (0x0000) the correct
                        // number of bytes beyond the 0x7F7F
                        headerIsVerified = true;

                        if (ensembleBuffer.remaining() > 0) {
                            ensembleBuffer.put(byteOne);

                        } else {
                            ensembleBuffer.compact();
                            ensembleBuffer.put(byteOne);

                        }

                        state = 5;
                        break;

                    } else {

                        if (ensembleByteCount > dataTypeOneOffset + 1) {
                            // We've hit a random 0x7F7F byte sequence that is not a true
                            // ensemble header id.  Reset the processing and look for the 
                            // next 0x7F7F sequence in the stream
                            ensembleByteCount = 0;
                            ensembleChecksum = 0;
                            dataTypeOneOffset = 0;
                            numberOfDataTypes = 0;
                            headerIsVerified = false;
                            ensembleBuffer.clear();
                            rbnbChannelMap.Clear();
                            channelIndex = rbnbChannelMap.Add(getRBNBChannelName());

                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 0;

                            if (ensembleBuffer.remaining() > 0) {
                                ensembleBuffer.put(byteOne);

                            } else {
                                ensembleBuffer.compact();
                                ensembleBuffer.put(byteOne);

                            }

                            break;

                        } else {
                            // We are still parsing bytes between the purported header ID
                            // and fixed leader ID. Keep parsing until we hit the fixed
                            // leader ID, or until we are greater than the dataTypeOneOffset
                            // stated value.
                            ensembleByteCount++;
                            ensembleChecksum += (byteOne & 0xFF);

                            if (ensembleBuffer.remaining() > 0) {
                                ensembleBuffer.put(byteOne);

                            } else {
                                ensembleBuffer.compact();
                                ensembleBuffer.put(byteOne);

                            }

                            break;
                        }

                    }

                case 5: // read the rest of the bytes to the next Header ID 

                    // if we've made it to the next ensemble's header id, prepare to
                    // flush the data.  Also check that the calculated byte count 
                    // is greater than the recorded byte count in case of finding an
                    // arbitrary 0x7f 0x7f sequence in the data stream
                    if (byteOne == 0x7F && byteTwo == 0x7F && (ensembleByteCount == ensembleBytes + 3)
                            && headerIsVerified) {

                        // remove the last bytes from the count (byteOne and byteTwo)
                        ensembleByteCount -= 1;

                        // remove the last three bytes from the checksum: 
                        // the two checksum bytes are not included, and the two 0x7f 
                        //bytes belong to the next ensemble, and one of them was 
                        // previously added. Reset the buffer position due to this too.
                        //ensembleChecksum -= (byteOne   & 0xFF);
                        ensembleChecksum -= (byteTwo & 0xFF);
                        ensembleChecksum -= (byteThree & 0xFF);
                        ensembleChecksum -= (byteFour & 0xFF);
                        // We are consistently 1 byte over in the checksum.  Trim it.  We need to
                        // troubleshoot why this is. CSJ 12/18/2007
                        ensembleChecksum = ensembleChecksum - 1;

                        // jockey byteThree into LSB, byteFour into MSB
                        int upperChecksumByte = (byteThree & 0xFF) << 8;
                        int lowerChecksumByte = (byteFour & 0xFF);
                        int trueChecksum = upperChecksumByte + lowerChecksumByte;

                        if (ensembleBuffer.remaining() > 0) {
                            ensembleBuffer.put((byte) lowerChecksumByte);
                            ensembleBuffer.put((byte) (upperChecksumByte >> 8));
                        } else {
                            ensembleBuffer.compact();
                            ensembleBuffer.put((byte) lowerChecksumByte);
                            ensembleBuffer.put((byte) (upperChecksumByte >> 8));
                        }

                        // check if the calculated checksum (modulo 65535) is equal
                        // to the true checksum; if so, flush to the data turbine
                        // Also, if the checksums are off by 1 byte, also flush the
                        // data.  We need to troubleshoot this bug CSJ 06/11/2008
                        if (((ensembleChecksum % 65535) == trueChecksum)
                                || ((ensembleChecksum + 1) % 65535 == trueChecksum)
                                || ((ensembleChecksum - 1) % 65535 == trueChecksum)) {

                            // extract just the length of the ensemble bytes out of the
                            // ensemble buffer, and place it in the channel map as a 
                            // byte array.  Then, send it to the data turbine.
                            byte[] ensembleArray = new byte[ensembleByteCount];
                            ensembleBuffer.flip();
                            ensembleBuffer.get(ensembleArray);

                            // send the ensemble to the data turbine
                            rbnbChannelMap.PutTimeAuto("server");
                            rbnbChannelMap.PutDataAsByteArray(channelIndex, ensembleArray);
                            getSource().Flush(rbnbChannelMap);
                            logger.debug("flushed: " + ensembleByteCount + " " + "ens cksum: "
                                    + ensembleChecksum + "\t\t" + "ens pos: " + ensembleBuffer.position() + "\t"
                                    + "ens rem: " + ensembleBuffer.remaining() + "\t" + "buf pos: "
                                    + buffer.position() + "\t" + "buf rem: " + buffer.remaining() + "\t"
                                    + "state: " + state);
                            logger.info("Sent ADCP ensemble to the data turbine.");

                            // only clear all four bytes if we are not one or two bytes 
                            // from the end of the byte buffer (i.e. the header id 
                            // is split or is all in the previous buffer)
                            if (byteOne == 0x7f && byteTwo == 0x7f && ensembleByteCount > ensembleBytes
                                    && buffer.position() == 0) {
                                byteThree = 0x00;
                                byteFour = 0x00;
                                logger.debug("Cleared ONLY b3, b4.");
                            } else if (byteOne == 0x7f && ensembleByteCount > ensembleBytes
                                    && buffer.position() == 1) {
                                buffer.position(buffer.position() - 1);
                                byteTwo = 0x00;
                                byteThree = 0x00;
                                byteFour = 0x00;
                                logger.debug("Cleared ONLY b2, b3, b4.");

                            } else {
                                byteOne = 0x00;
                                byteTwo = 0x00;
                                byteThree = 0x00;
                                byteFour = 0x00;
                                logger.debug("Cleared ALL b1, b2, b3, b4.");
                            }

                            //rewind the position to before the next ensemble's header id
                            if (buffer.position() >= 2) {
                                buffer.position(buffer.position() - 2);
                                logger.debug("Moved position back two, now: " + buffer.position());
                            }

                            ensembleBuffer.clear();
                            ensembleByteCount = 0;
                            ensembleBytes = 0;
                            ensembleChecksum = 0;
                            state = 0;
                            break;

                        } else {

                            // The checksums don't match, move on 
                            logger.info("not equal: " + "calc chksum: " + (ensembleChecksum % 65535)
                                    + "\tens chksum: " + trueChecksum + "\tbuf pos: " + buffer.position()
                                    + "\tbuf rem: " + buffer.remaining() + "\tens pos: "
                                    + ensembleBuffer.position() + "\tens rem: " + ensembleBuffer.remaining()
                                    + "\tstate: " + state);

                            rbnbChannelMap.Clear();
                            channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
                            ensembleBuffer.clear();
                            ensembleByteCount = 0;
                            ensembleChecksum = 0;
                            ensembleBuffer.clear();
                            state = 0;
                            break;
                        }

                    } else {

                        // still in the middle of the ensemble, keep adding bytes
                        ensembleByteCount++; // add each byte found
                        ensembleChecksum += (byteOne & 0xFF);

                        if (ensembleBuffer.remaining() > 0) {
                            ensembleBuffer.put(byteOne);

                        } else {
                            ensembleBuffer.compact();
                            ensembleBuffer.put(byteOne);

                        }

                        break;
                    }
                }
                // shift the bytes in the FIFO window
                byteFour = byteThree;
                byteThree = byteTwo;
                byteTwo = byteOne;

                logger.debug("remaining:\t" + buffer.remaining() + "\tstate:\t" + state + "\tens byte count:\t"
                        + ensembleByteCount + "\tens bytes:\t" + ensembleBytes + "\tver:\t" + headerIsVerified
                        + "\tbyte value:\t" + new String(Hex.encodeHex((new byte[] { byteOne }))));
            } //end while (more unread bytes)

            // prepare the buffer to read in more bytes from the stream
            buffer.compact();

        } // end while (more socket bytes to read)
        socket.close();

    } catch (IOException e) {
        // handle exceptions
        // In the event of an i/o exception, log the exception, and allow execute()
        // to return false, which will prompt a retry.
        failed = true;
        e.printStackTrace();
        return !failed;
    } catch (SAPIException sapie) {
        // In the event of an RBNB communication  exception, log the exception, 
        // and allow execute() to return false, which will prompt a retry.
        failed = true;
        sapie.printStackTrace();
        return !failed;
    }

    return !failed;
}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

/**
 * Deserialize a raw byte value for this column into an Object
 * @param data The raw byte value/*from  w w w .j  a v a 2s .  c  o  m*/
 * @param order Byte order in which the raw value is stored
 * @return The deserialized Object
 * @usage _advanced_method_
 */
public Object read(byte[] data, ByteOrder order) throws IOException {
    ByteBuffer buffer = ByteBuffer.wrap(data).order(order);

    switch (getType()) {
    case BOOLEAN:
        throw new IOException("Tried to read a boolean from data instead of null mask.");
    case BYTE:
        return Byte.valueOf(buffer.get());
    case INT:
        return Short.valueOf(buffer.getShort());
    case LONG:
        return Integer.valueOf(buffer.getInt());
    case DOUBLE:
        return Double.valueOf(buffer.getDouble());
    case FLOAT:
        return Float.valueOf(buffer.getFloat());
    case SHORT_DATE_TIME:
        return readDateValue(buffer);
    case BINARY:
        return data;
    case TEXT:
        return decodeTextValue(data);
    case MONEY:
        return readCurrencyValue(buffer);
    case NUMERIC:
        return readNumericValue(buffer);
    case GUID:
        return readGUIDValue(buffer, order);
    case UNKNOWN_0D:
    case UNKNOWN_11:
        // treat like "binary" data
        return data;
    case COMPLEX_TYPE:
        return new ComplexValueForeignKeyImpl(this, buffer.getInt());
    default:
        throw new IOException("Unrecognized data type: " + _type);
    }
}

From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCache.java

/**
 * Read external with codec./*from   w  w w  . j  ava 2  s . co  m*/
 *
 * @param blockName the block name
 * @return the cacheable
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unused")
private Cacheable readExternalWithCodec(String blockName, boolean repeat, boolean caching) throws IOException {
    if (overflowExtEnabled == false)
        return null;
    // Check if we have  already this block in external storage cache
    try {
        // We use 16 - byte hash for external storage cache  
        byte[] hashed = Utils.hash128(blockName);

        StorageHandle handle = storage.newStorageHandle();
        byte[] data = (byte[]) extStorageCache.get(hashed);
        if (data == null) {
            if (repeat == false)
                extRefStats.miss(caching);
            return null;
        } else {
            extRefStats.hit(caching);
        }
        // Initialize handle 
        handle.fromBytes(data);

        ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
        SerDe serde = extStorageCache.getSerDe();
        Codec codec = extStorageCache.getCompressionCodec();

        buffer.clear();

        StorageHandle newHandle = storage.getData(handle, buffer);
        if (buffer.position() > 0)
            buffer.flip();
        int size = buffer.getInt();
        if (size == 0) {
            // BIGBASE-45
            // Remove reference from reference cache
            // reference is in L3-RAM cache but no object in L3-DISK cache was found
            // remove only if handle is invalid
            if (storage.isValid(handle) == false) {
                extStorageCache.remove(hashed);
            }
            return null;
        }
        // Skip key
        int keySize = buffer.getInt();
        buffer.position(8 + keySize);

        boolean inMemory = buffer.get() == (byte) 1;

        buffer.limit(size + 4);
        Cacheable obj = (Cacheable) serde.readCompressed(buffer/*, codec*/);
        offHeapCache.put(blockName, obj);

        if (newHandle.equals(handle) == false) {
            extStorageCache.put(hashed, newHandle.toBytes());
        }

        return obj;

    } catch (Throwable e) {
        fatalExternalReads.incrementAndGet();
        throw new IOException(e);
    }

}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Decodes a NUMERIC field.//from  w ww.j  ava2 s  .  com
 */
private BigDecimal readNumericValue(ByteBuffer buffer) {
    boolean negate = (buffer.get() != 0);

    byte[] tmpArr = new byte[16];
    buffer.get(tmpArr);

    if (buffer.order() != ByteOrder.BIG_ENDIAN) {
        fixNumericByteOrder(tmpArr);
    }

    BigInteger intVal = new BigInteger(tmpArr);
    if (negate) {
        intVal = intVal.negate();
    }
    return new BigDecimal(intVal, getScale());
}