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.Table.java

private ByteBuffer findFreeRowSpace(int rowSize, ByteBuffer dataPage, int pageNumber) throws IOException {
    if (dataPage == null) {

        // find last data page (Not bothering to check other pages for free
        // space.)
        UsageMap.PageCursor revPageCursor = _ownedPages.cursor();
        revPageCursor.afterLast();//from w ww  .j a  v  a2  s .  c  o  m
        while (true) {
            int tmpPageNumber = revPageCursor.getPreviousPage();
            if (tmpPageNumber < 0) {
                break;
            }
            dataPage = _addRowBufferH.setPage(getPageChannel(), tmpPageNumber);
            if (dataPage.get() == PageTypes.DATA) {
                // found last data page, only use if actually listed in free space
                // pages
                if (_freeSpacePages.containsPageNumber(tmpPageNumber)) {
                    pageNumber = tmpPageNumber;
                }
                break;
            }
        }

        if (pageNumber == PageChannel.INVALID_PAGE_NUMBER) {
            // No data pages exist (with free space).  Create a new one.
            return newDataPage();
        }

    }

    if (!rowFitsOnDataPage(rowSize, dataPage, getFormat())) {

        // Last data page is full.  Create a new one.
        writeDataPage(dataPage, pageNumber);
        _freeSpacePages.removePageNumber(pageNumber);

        dataPage = newDataPage();
    }

    return dataPage;
}

From source file:com.aionemu.gameserver.services.LegionService.java

/**
 * @param player//  w w w  .  j a v  a  2 s  .  co  m
 * @param legionEmblem
 * @param legionId
 * @param legionName
 */
public void sendEmblemData(Player player, LegionEmblem legionEmblem, int legionId, String legionName) {
    PacketSendUtility.sendPacket(player,
            new SM_LEGION_SEND_EMBLEM(legionId, legionEmblem.getEmblemId(), legionEmblem.getColor_r(),
                    legionEmblem.getColor_g(), legionEmblem.getColor_b(), legionName,
                    legionEmblem.getEmblemType(), legionEmblem.getCustomEmblemData().length));
    ByteBuffer buf = ByteBuffer.allocate(legionEmblem.getCustomEmblemData().length);
    buf.put(legionEmblem.getCustomEmblemData()).position(0);
    log.debug("legionEmblem size: " + buf.capacity() + " bytes");
    int maxSize = 7993;
    int currentSize;
    byte[] bytes;
    do {
        log.debug("legionEmblem data position: " + buf.position());
        currentSize = buf.capacity() - buf.position();
        log.debug("legionEmblem data remaining capacity: " + currentSize + " bytes");

        if (currentSize >= maxSize) {
            bytes = new byte[maxSize];
            for (int i = 0; i < maxSize; i++) {
                bytes[i] = buf.get();
            }
            log.debug("legionEmblem data send size: " + (bytes.length) + " bytes");
            PacketSendUtility.sendPacket(player, new SM_LEGION_SEND_EMBLEM_DATA(maxSize, bytes));
        } else {
            bytes = new byte[currentSize];
            for (int i = 0; i < currentSize; i++) {
                bytes[i] = buf.get();
            }
            log.debug("legionEmblem data send size: " + (bytes.length) + " bytes");
            PacketSendUtility.sendPacket(player, new SM_LEGION_SEND_EMBLEM_DATA(currentSize, bytes));
        }
    } while (buf.capacity() != buf.position());
}

From source file:gephi.spade.panel.fcsFile.java

/**
 * readBinIntData ---/*from   w ww  .  j  a va  2  s .c o  m*/
 * <p>
 * Reads binary integers in list mode in the DATA segment and updates
 * eventList.
 * </p>
 *
 * <p>
 * Assumes that the bits for the values are byte-aligned. It needs to be
 * fixed if that is not the case.
 * </p>
 *
 * @param data
 *            <code>ByteBuffer</code> containing the DATA segment of the
 *            underlying file.
 */
private void readBinIntData(ByteBuffer data) {
    // Allocate the eventList
    eventList = new double[parameters][totalEvents];

    int numBytes, value, range, currByte;

    final int totalEvents = this.totalEvents;
    final int parameters = this.parameters;

    for (int i = 0; i < totalEvents; i++) {
        for (int j = 0; j < parameters; j++) {
            // Calculate the number of bytes used from the number of bits
            // used
            // Round up to the next full byte
            numBytes = (int) Math.ceil(((double) channelBits[j]) / Byte.SIZE);

            // Get the range of the current parameter - will let us build
            // the mask
            range = (int) channelRange[j];

            // Initialize the current value to 0
            value = 0;

            if (littleEndianP) {
                // If the byte order is little endian, then build the value
                // to the left.
                for (int k = 0; k < numBytes; k++) {
                    // Get the next 8 bits masking to make sure Java doesn't
                    // prepend 1's
                    currByte = (data.get() & 0xFF);

                    // Shift the 8 bits into position
                    currByte <<= (8 * k);

                    // Or the 8 bits with the current value
                    value |= currByte;
                }
            } else {
                // Otherwise, the byte order is big endian, so build the
                // value to the right.
                for (int k = 0; k < numBytes; k++) {
                    // Left shift the previous bits in value to make room
                    value <<= 8;

                    // Grab the next 8 bits masking to make sure Java
                    // doesn't prepend 1's
                    value |= (data.get() & 0xFF);
                }
            }

            /**
             * From the FCS specification: --- The remaining bits are
             * usually unused and set to "0"; however, some file writers
             * store non-data information in that bit-space. Implementers
             * must use a bit mask when reading these list mode parameter
             * values to insure that erroneous values are not read from the
             * unused bits.
             */
            // Mask the value based on the range
            value &= (range - 1);

            // Store the value into the array
            eventList[j][i] = value;
        }
    }
}

From source file:com.MainFiles.Functions.java

public int createStan() {
    int x = 0;//from   w w w  . j  a  va2  s  .  c  om

    String filename = COUNT_FILE;
    File inwrite = new File(filename);

    // Get a file channel for the file
    try {
        FileChannel channel = new RandomAccessFile(inwrite, "rw").getChannel();
        // Use the file channel to create a lock on the file.
        // This method blocks until it can retrieve the lock.
        FileLock lock = channel.lock();
        //  if(!inwrite.exists()) {
        String s = "";
        try {
            int fileSize = (int) channel.size();
            //    System.out.println("int is" + fileSize);
            ByteBuffer bafa = ByteBuffer.allocate(fileSize);
            int numRead = 0;
            while (numRead >= 0) {
                numRead = channel.read(bafa);
                bafa.rewind();
                for (int i = 0; i < numRead; i++) {
                    int b = (int) bafa.get();
                    char c = (char) b;
                    s = s + c;
                }
            }

            x = Integer.parseInt(s);
            if (x > 999999) {
                x = 100000;
            } else if (x < 100000) {
                x = 100000;
            }
            x = ++x;
            String xx = String.valueOf(x);
            byte[] yy = xx.getBytes();
            channel.truncate(0);
            channel.write(ByteBuffer.wrap(yy));
            // channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        lock.release();
        // Close the file
        channel.close();
    } catch (FileNotFoundException e) {
        String message = "The file " + inwrite.getName() + " does not exist. So no input can be written on it";
        System.out.println(message);
        e.printStackTrace();
        //log to error file
    } catch (IOException e) {
        System.out.println("Problem writing to the logfile " + inwrite.getName());

    }

    filename = "";
    return x;
}

From source file:edu.hawaii.soest.kilonalu.ctd.CTDSource.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   www  .ja  va 2  s  .c om*/
protected boolean execute() {
    logger.debug("CTDSource.execute() called.");

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

    boolean failed = false;

    // test the connection type
    if (this.connectionType.equals("serial")) {

        // create a serial connection to the local serial port
        this.channel = getSerialConnection();

    } else if (this.connectionType.equals("socket")) {

        // otherwise create a TCP or UDP socket connection to the remote host
        this.channel = getSocketConnection();

    } else {
        logger.info("There was an error establishing either a serial or "
                + "socket connection to the instrument.  Please be sure "
                + "the connection type is set to either 'serial' or 'socket'.");
        return false;

    }

    // 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());

        // Declare sample variables to be used in the response parsing
        byte[] sampleArray;

        // 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 channel ...
        while (this.channel.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("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: " + this.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 (this.state) {

                case 0: // wake up the instrument

                    // check for instrument metadata fields
                    if (this.enableSendCommands && !this.hasMetadata) {

                        // wake the instrument with an initial '\r\n' command
                        this.command = this.commandSuffix;
                        this.sentCommand = queryInstrument(this.command);
                        this.sentCommand = queryInstrument(this.command);
                        streamingThread.sleep(2000);

                        this.state = 1;
                        break;

                    } else {

                        this.state = 11;
                        break;

                    }

                case 1: // stop the sampling

                    // be sure the instrument woke (look for S> prompt)
                    //if (byteOne == 0x3E && byteTwo == 0x53 ) {
                    //  
                    //  sampleByteCount = 0;
                    //  sampleBuffer.clear();
                    //  
                    //  // send the stop sampling command
                    this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix;
                    this.sentCommand = queryInstrument(command);

                    sampleBuffer.clear();
                    sampleByteCount = 0;
                    this.state = 2;
                    break;

                //} else {
                //  // handle instrument hardware response
                //  sampleByteCount++; // add the last byte found to the count
                //  
                //  // add the last byte found to the sample buffer
                //  if ( sampleBuffer.remaining() > 0 ) {
                //    sampleBuffer.put(byteOne);
                //  
                //  } else {
                //    sampleBuffer.compact();
                //    sampleBuffer.put(byteOne);
                //    
                //  }                
                //  
                //  break; // continue reading bytes
                //  
                //}

                case 2: // based on outputType, get metadata from the instrument

                    // the response should end in <Executed/>
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        sampleBuffer.clear();
                        sampleByteCount = 0;
                        this.samplingIsStopped = true;

                        // for newer firmware CTDs, use xml-based query commands
                        if (getOutputType().equals("xml")) {
                            // create the CTD parser instance used to parse CTD output
                            this.ctdParser = new CTDParser();
                            this.state = 3;
                            break;

                            // otherwise, use text-based query commands
                        } else if (getOutputType().equals("text")) {
                            this.state = 12; // process DS and DCal commands
                            break;

                        } else {

                            logger.info("The CTD output type is not recognized. "
                                    + "Please set the output type to either " + "'xml' or 'text'.");
                            failed = true;
                            this.state = 0;

                            // close the serial or socket channel
                            if (this.channel != null && this.channel.isOpen()) {
                                try {
                                    this.channel.close();

                                } catch (IOException cioe) {
                                    logger.debug("An error occurred trying to close the byte channel. "
                                            + " The error message was: " + cioe.getMessage());
                                    return !failed;

                                }
                            }

                            // disconnect from the RBNB
                            if (isConnected()) {
                                disconnect();
                            }

                            return !failed;

                        }

                    } else {

                        // handle instrument hardware response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        break; // continue reading bytes

                    }

                case 3: // get the instrument status metadata

                    if (!this.ctdParser.getHasStatusMetadata()) {

                        this.command = this.commandPrefix + this.getStatusCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);
                        this.state = 4;
                        break;

                    } else {

                        // get the configuration metadata
                        this.command = this.commandPrefix + this.getConfigurationCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);
                        this.state = 5;
                        break;

                    }

                case 4: // handle instrument status response

                    // command response ends with <Executed/> (so find: ed/>)
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        // handle instrument status response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // set the CTD metadata
                        int executedIndex = this.responseString.indexOf("<Executed/>");
                        this.responseString = this.responseString.substring(0, executedIndex - 1);

                        this.ctdParser.setMetadata(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // then get the instrument configuration metadata
                        if (!this.ctdParser.getHasConfigurationMetadata()) {

                            this.command = this.commandPrefix + this.getConfigurationCommand
                                    + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 5;
                            break;

                        } else {

                            // get the calibration metadata
                            this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 6;
                            break;

                        }

                    } else {
                        break; // continue reading bytes

                    }

                case 5: // handle the instrument configuration metadata

                    // command response ends with <Executed/> (so find: ed/>)
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        // handle instrument configration response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // set the CTD metadata
                        int executedIndex = this.responseString.indexOf("<Executed/>");
                        this.responseString = this.responseString.substring(0, executedIndex - 1);

                        this.ctdParser.setMetadata(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // then get the instrument calibration metadata
                        if (!this.ctdParser.getHasCalibrationMetadata()) {

                            this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 6;
                            break;

                        } else {

                            this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 7;
                            break;

                        }

                    } else {
                        break; // continue reading bytes

                    }

                case 6: // handle the instrument calibration metadata

                    // command response ends with <Executed/> (so find: ed/>)
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        // handle instrument calibration response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // set the CTD metadata
                        int executedIndex = this.responseString.indexOf("<Executed/>");
                        this.responseString = this.responseString.substring(0, executedIndex - 1);

                        this.ctdParser.setMetadata(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // then get the instrument event metadata
                        if (!this.ctdParser.getHasEventMetadata()) {

                            this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 7;
                            break;

                        } else {

                            this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 8;
                            break;

                        }

                    } else {
                        break; // continue reading bytes

                    }

                case 7: // handle instrument event metadata

                    // command response ends with <Executed/> (so find: ed/>)
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        // handle instrument events response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // set the CTD metadata
                        int executedIndex = this.responseString.indexOf("<Executed/>");
                        this.responseString = this.responseString.substring(0, executedIndex - 1);

                        this.ctdParser.setMetadata(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // then get the instrument hardware metadata
                        if (!this.ctdParser.getHasHardwareMetadata()) {

                            this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                            streamingThread.sleep(5000);
                            this.state = 8;
                            break;

                        } else {

                            this.state = 9;
                            break;

                        }

                    } else {
                        break; // continue reading bytes

                    }

                case 8: // handle the instrument hardware response

                    // command response ends with <Executed/> (so find: ed/>)
                    if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) {

                        // handle instrument hardware response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // set the CTD metadata
                        int executedIndex = this.responseString.indexOf("<Executed/>");
                        this.responseString = this.responseString.substring(0, executedIndex - 1);

                        this.ctdParser.setMetadata(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // sync the clock if it is not synced
                        if (!this.clockIsSynced) {

                            this.state = 9;
                            break;

                        } else {
                            this.state = 10;
                            break;

                        }

                    } else {
                        break; // continue reading bytes

                    }

                case 9: // set the instrument clock

                    // is sampling stopped?
                    if (!this.samplingIsStopped) {
                        // wake the instrument with an initial '\r\n' command
                        this.command = this.commandSuffix;
                        this.sentCommand = queryInstrument(this.command);
                        streamingThread.sleep(2000);

                        // then stop the sampling
                        this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        this.samplingIsStopped = true;

                    }

                    // now set the clock
                    if (this.sentCommand) {
                        this.clockSyncDate = new Date();
                        DATE_FORMAT.setTimeZone(TZ);
                        String dateAsString = DATE_FORMAT.format(this.clockSyncDate);

                        this.command = this.commandPrefix + this.setDateTimeCommand + dateAsString
                                + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);
                        this.clockIsSynced = true;
                        logger.info("The instrument clock has bee synced at " + this.clockSyncDate.toString());
                        this.state = 10;
                        break;

                    } else {

                        break; // try the clock sync again due to failure

                    }

                case 10: // restart the instrument sampling

                    if (this.samplingIsStopped) {

                        this.hasMetadata = true;

                        this.command = this.commandPrefix + this.startSamplingCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);

                        if (this.sentCommand) {
                            this.state = 11;
                            break;

                        } else {
                            break; // try starting the sampling again due to failure
                        }

                    } else {

                        break;

                    }

                case 11: // read bytes to the next EOL characters

                    // sample line is terminated by \r\n
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0A && byteTwo == 0x0D) {

                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // 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.
                        sampleArray = new byte[sampleByteCount];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        this.responseString = new String(sampleArray, "US-ASCII");

                        // test if the sample is not just an instrument message
                        if (this.responseString.matches("^# [0-9].*\r\n")
                                || this.responseString.matches("^#  [0-9].*\r\n")
                                || this.responseString.matches("^ [0-9].*\r\n")) {

                            // add the data observations string to the CTDParser object
                            // and populate the CTDParser data fields
                            //this.ctdParser.setData(this.responseString);
                            //this.ctdParser.parse();

                            // build the channel map with all of the data and metadata channels:                  
                            int channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
                            rbnbChannelMap.PutMime(channelIndex, "text/plain");
                            rbnbChannelMap.PutTimeAuto("server");

                            // add the ASCII sample data field
                            rbnbChannelMap.PutDataAsString(channelIndex, this.responseString);

                            // add other metadata and data fields to the map if metadata was collected
                            if (this.hasMetadata && this.ctdParser != null) {

                                // add the samplingMode field data                                                                                 
                                channelIndex = rbnbChannelMap.Add("samplingMode");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getSamplingMode()); // String

                                // add the temperatureSerialNumber field data                                                                      
                                channelIndex = rbnbChannelMap.Add("temperatureSerialNumber");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getTemperatureSerialNumber()); // String   

                                // add the conductivitySerialNumber field data                                                                     
                                channelIndex = rbnbChannelMap.Add("conductivitySerialNumber");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getConductivitySerialNumber()); // String   

                                // add the mainBatteryVoltage field data                                                                           
                                channelIndex = rbnbChannelMap.Add("mainBatteryVoltage");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getMainBatteryVoltage() }); // double   

                                // add the lithiumBatteryVoltage field data                                                                        
                                channelIndex = rbnbChannelMap.Add("lithiumBatteryVoltage");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getLithiumBatteryVoltage() }); // double   

                                // add the operatingCurrent field data                                                                             
                                channelIndex = rbnbChannelMap.Add("operatingCurrent");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getOperatingCurrent() }); // double   

                                // add the pumpCurrent field data                                                                                  
                                channelIndex = rbnbChannelMap.Add("pumpCurrent");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPumpCurrent() }); // double   

                                // add the channels01ExternalCurrent field data                                                                    
                                channelIndex = rbnbChannelMap.Add("channels01ExternalCurrent");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getChannels01ExternalCurrent() }); // double   

                                // add the channels23ExternalCurrent field data                                                                    
                                channelIndex = rbnbChannelMap.Add("channels23ExternalCurrent");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getChannels23ExternalCurrent() }); // double   

                                // add the loggingStatus field data                                                                                
                                channelIndex = rbnbChannelMap.Add("loggingStatus");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getLoggingStatus()); // String   

                                // add the numberOfScansToAverage field data                                                                       
                                channelIndex = rbnbChannelMap.Add("numberOfScansToAverage");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getNumberOfScansToAverage() }); // int      

                                // add the numberOfSamples field data                                                                              
                                channelIndex = rbnbChannelMap.Add("numberOfSamples");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getNumberOfSamples() }); // int      

                                // add the numberOfAvailableSamples field data                                                                     
                                channelIndex = rbnbChannelMap.Add("numberOfAvailableSamples");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getNumberOfAvailableSamples() }); // int      

                                // add the sampleInterval field data                                                                               
                                channelIndex = rbnbChannelMap.Add("sampleInterval");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getSampleInterval() }); // int      

                                // add the measurementsPerSample field data                                                                        
                                channelIndex = rbnbChannelMap.Add("measurementsPerSample");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getMeasurementsPerSample() }); // int      

                                // add the transmitRealtime field data                                                                             
                                channelIndex = rbnbChannelMap.Add("transmitRealtime");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getTransmitRealtime()); // String   

                                // add the numberOfCasts field data                                                                                
                                channelIndex = rbnbChannelMap.Add("numberOfCasts");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getNumberOfCasts() }); // int      

                                // add the minimumConductivityFrequency field data                                                                 
                                channelIndex = rbnbChannelMap.Add("minimumConductivityFrequency");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getMinimumConductivityFrequency() }); // int      

                                // add the pumpDelay field data                                                                                    
                                channelIndex = rbnbChannelMap.Add("pumpDelay");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsInt32(channelIndex,
                                        new int[] { this.ctdParser.getPumpDelay() }); // int      

                                // add the automaticLogging field data                                                                             
                                channelIndex = rbnbChannelMap.Add("automaticLogging");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getAutomaticLogging()); // String   

                                // add the ignoreMagneticSwitch field data                                                                         
                                channelIndex = rbnbChannelMap.Add("ignoreMagneticSwitch");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getIgnoreMagneticSwitch()); // String   

                                // add the batteryType field data                                                                                  
                                channelIndex = rbnbChannelMap.Add("batteryType");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryType()); // String   

                                // add the batteryCutoff field data                                                                                
                                channelIndex = rbnbChannelMap.Add("batteryCutoff");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryCutoff()); // String   

                                // add the pressureSensorType field data                                                                           
                                channelIndex = rbnbChannelMap.Add("pressureSensorType");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getPressureSensorType()); // String   

                                // add the pressureSensorRange field data                                                                          
                                channelIndex = rbnbChannelMap.Add("pressureSensorRange");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getPressureSensorRange()); // String   

                                // add the sbe38TemperatureSensor field data                                                                       
                                channelIndex = rbnbChannelMap.Add("sbe38TemperatureSensor");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getSbe38TemperatureSensor()); // String   

                                // add the gasTensionDevice field data                                                                             
                                channelIndex = rbnbChannelMap.Add("gasTensionDevice");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getGasTensionDevice()); // String   

                                // add the externalVoltageChannelZero field data                                                                   
                                channelIndex = rbnbChannelMap.Add("externalVoltageChannelZero");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getExternalVoltageChannelZero()); // String   

                                // add the externalVoltageChannelOne field data                                                                    
                                channelIndex = rbnbChannelMap.Add("externalVoltageChannelOne");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getExternalVoltageChannelOne()); // String   

                                // add the externalVoltageChannelTwo field data                                                                    
                                channelIndex = rbnbChannelMap.Add("externalVoltageChannelTwo");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getExternalVoltageChannelTwo()); // String   

                                // add the externalVoltageChannelThree field data                                                                  
                                channelIndex = rbnbChannelMap.Add("externalVoltageChannelThree");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getExternalVoltageChannelThree()); // String   

                                // add the echoCommands field data                                                                                 
                                channelIndex = rbnbChannelMap.Add("echoCommands");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getEchoCommands()); // String   

                                // add the outputFormat field data                                                                                 
                                channelIndex = rbnbChannelMap.Add("outputFormat");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getOutputFormat()); // String   

                                // add the temperatureCalibrationDate field data                                                                   
                                channelIndex = rbnbChannelMap.Add("temperatureCalibrationDate");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getTemperatureCalibrationDate()); // String   

                                // add the temperatureCoefficientTA0 field data                                                                    
                                channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getTemperatureCoefficientTA0() }); // double   

                                // add the temperatureCoefficientTA1 field data                                                                    
                                channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA1");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getTemperatureCoefficientTA1() }); // double   

                                // add the temperatureCoefficientTA2 field data                                                                    
                                channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA2");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getTemperatureCoefficientTA2() }); // double   

                                // add the temperatureCoefficientTA3 field data                                                                    
                                channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA3");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getTemperatureCoefficientTA3() }); // double   

                                // add the temperatureOffsetCoefficient field data                                                                 
                                channelIndex = rbnbChannelMap.Add("temperatureOffsetCoefficient");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getTemperatureOffsetCoefficient() }); // double   

                                // add the conductivityCalibrationDate field data                                                                  
                                channelIndex = rbnbChannelMap.Add("conductivityCalibrationDate");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getConductivityCalibrationDate()); // String   

                                // add the conductivityCoefficientG field data                                                                     
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientG");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientG() }); // double   

                                // add the conductivityCoefficientH field data                                                                     
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientH");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientH() }); // double   

                                // add the conductivityCoefficientI field data                                                                     
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientI");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientI() }); // double   

                                // add the conductivityCoefficientJ field data                                                                     
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientJ");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientJ() }); // double   

                                // add the conductivityCoefficientCF0 field data                                                                   
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientCF0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientCF0() }); // double   

                                // add the conductivityCoefficientCPCOR field data                                                                 
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientCPCOR");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientCPCOR() }); // double   

                                // add the conductivityCoefficientCTCOR field data                                                                 
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientCTCOR");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientCTCOR() }); // double   

                                // add the conductivityCoefficientCSLOPE field data                                                                
                                channelIndex = rbnbChannelMap.Add("conductivityCoefficientCSLOPE");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getConductivityCoefficientCSLOPE() }); // double   

                                // add the pressureSerialNumber field data                                                                         
                                channelIndex = rbnbChannelMap.Add("pressureSerialNumber");
                                rbnbChannelMap.PutMime(channelIndex, "text/plain");
                                rbnbChannelMap.PutDataAsString(channelIndex,
                                        this.ctdParser.getPressureSerialNumber()); // String   

                                // add the pressureCoefficientPA0 field data                                                                       
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPA0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPA0() }); // double   

                                // add the pressureCoefficientPA1 field data                                                                       
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPA1");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPA1() }); // double   

                                // add the pressureCoefficientPA2 field data                                                                       
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPA2");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPA2() }); // double   

                                // add the pressureCoefficientPTCA0 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCA0() }); // double   

                                // add the pressureCoefficientPTCA1 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA1");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCA1() }); // double   

                                // add the pressureCoefficientPTCA2 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA2");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCA2() }); // double   

                                // add the pressureCoefficientPTCB0 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCB0() }); // double   

                                // add the pressureCoefficientPTCB1 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB1");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCB1() }); // double   

                                // add the pressureCoefficientPTCB2 field data                                                                     
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB2");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTCB2() }); // double   

                                // add the pressureCoefficientPTEMPA0 field data                                                                   
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA0");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTEMPA0() }); // double   

                                // add the pressureCoefficientPTEMPA1 field data                                                                   
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA1");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTEMPA1() }); // double   

                                // add the pressureCoefficientPTEMPA2 field data                                                                   
                                channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA2");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureCoefficientPTEMPA2() }); // double   

                                // add the pressureOffsetCoefficient field data                                                                    
                                channelIndex = rbnbChannelMap.Add("pressureOffsetCoefficient");
                                rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                                rbnbChannelMap.PutDataAsFloat64(channelIndex,
                                        new double[] { this.ctdParser.getPressureOffsetCoefficient() }); // double   
                            }

                            // send the sample to the data turbine
                            getSource().Flush(rbnbChannelMap);
                            logger.info("Sent sample to the DataTurbine: " + this.responseString);

                            // reset variables for the next sample
                            sampleBuffer.clear();
                            sampleByteCount = 0;
                            channelIndex = 0;
                            rbnbChannelMap.Clear();
                            logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap.");

                            // check if the clock needs syncing (daily)
                            if (this.enableSendCommands) {

                                // get the current datetime
                                Calendar currentCalendar = Calendar.getInstance();
                                currentCalendar.setTime(new Date());
                                Calendar lastSyncedCalendar = Calendar.getInstance();
                                lastSyncedCalendar.setTime(this.clockSyncDate);

                                // round the dates to the day
                                currentCalendar.clear(Calendar.MILLISECOND);
                                currentCalendar.clear(Calendar.SECOND);
                                currentCalendar.clear(Calendar.MINUTE);
                                currentCalendar.clear(Calendar.HOUR);

                                lastSyncedCalendar.clear(Calendar.MILLISECOND);
                                lastSyncedCalendar.clear(Calendar.SECOND);
                                lastSyncedCalendar.clear(Calendar.MINUTE);
                                lastSyncedCalendar.clear(Calendar.HOUR);

                                // sync the clock daily
                                if (currentCalendar.before(lastSyncedCalendar)) {
                                    this.state = 8;

                                }
                            }

                            // otherwise stay in state = 11                   
                            break;

                            // the sample looks more like an instrument message, don't flush
                        } else {

                            logger.info("This string does not look like a sample, "
                                    + "and was not sent to the DataTurbine.");
                            logger.info("Skipping sample: " + this.responseString);

                            // reset variables for the next sample
                            sampleBuffer.clear();
                            sampleByteCount = 0;
                            //rbnbChannelMap.Clear();                      
                            logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap.");
                            this.state = 11;
                            break;

                        }

                    } else { // not 0x0A0D

                        // 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 0x0A0D EOL

                case 12: // alternatively use legacy DS and DCal commands

                    if (this.enableSendCommands) {

                        // start by getting the DS status output
                        this.command = this.commandPrefix + this.displayStatusCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);
                        this.state = 13;
                        break;

                    } else {

                        this.state = 0;
                        break;

                    }

                case 13: // handle the DS command response

                    // command should end with the S> prompt
                    if (byteOne == 0x7E && byteTwo == 0x53) {

                        // handle instrument status response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount - 2]; //subtract "S>"
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);
                        this.responseString = new String(sampleArray, "US-ASCII");

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        // then get the instrument calibration metadata
                        this.command = this.commandPrefix + this.displayCalibrationCommand + this.commandSuffix;
                        this.sentCommand = queryInstrument(command);
                        streamingThread.sleep(5000);
                        this.state = 14;
                        break;

                    } else {
                        break; // continue reading bytes

                    }

                case 14: // handle the DCal command response

                    // command should end with the S> prompt
                    if (byteOne == 0x7E && byteTwo == 0x53) {

                        // handle instrument status response
                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // extract the sampleByteCount length from the sampleBuffer
                        sampleArray = new byte[sampleByteCount - 2]; // subtract "S>"
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        // append the DCal output to the DS output
                        this.responseString = this.responseString.concat(new String(sampleArray, "US-ASCII"));

                        // and add the data delimiter expected in the CTDParser
                        this.responseString = this.responseString.concat("*END*\r\n\r\n");

                        // build the CTDParser object with legacy DS and DCal metadata
                        this.ctdParser = new CTDParser(this.responseString);

                        // reset variables for the next sample
                        sampleBuffer.clear();
                        sampleByteCount = 0;

                        this.state = 9; // set the clock and start sampling
                        break;

                    } else {
                        break; // continue reading bytes

                    }

                } // 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 channel bytes to read)

        this.channel.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;
        this.state = 0;

        // close the serial or socket channel
        if (this.channel != null && this.channel.isOpen()) {
            try {
                this.channel.close();

            } catch (IOException cioe) {
                logger.debug("An error occurred trying to close the byte channel. " + " The error message was: "
                        + cioe.getMessage());

            }
        }

        // disconnect from the RBNB
        if (isConnected()) {
            disconnect();
        }

        e.printStackTrace();
        return !failed;

    } catch (InterruptedException intde) {
        // in the event that the streamingThread is interrupted
        failed = true;
        this.state = 0;

        // close the serial or socket channel
        if (this.channel != null && this.channel.isOpen()) {
            try {
                this.channel.close();

            } catch (IOException cioe) {
                logger.debug("An error occurred trying to close the byte channel. " + " The error message was: "
                        + cioe.getMessage());

            }
        }

        // disconnect from the RBNB
        if (isConnected()) {
            disconnect();
        }

        intde.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.
        //this.channel.close();
        failed = true;
        this.state = 0;

        // close the serial or socket channel
        if (this.channel != null && this.channel.isOpen()) {
            try {
                this.channel.close();

            } catch (IOException cioe) {
                logger.debug("An error occurred trying to close the byte channel. " + " The error message was: "
                        + cioe.getMessage());

            }
        }

        // disconnect from the RBNB
        if (isConnected()) {
            disconnect();
        }

        sapie.printStackTrace();
        return !failed;

    } catch (ParseException pe) {
        failed = true;
        this.state = 0;

        // close the serial or socket channel
        if (this.channel != null && this.channel.isOpen()) {
            try {
                this.channel.close();

            } catch (IOException cioe) {
                logger.debug("An error occurred trying to close the byte channel. " + " The error message was: "
                        + cioe.getMessage());

            }
        }

        // disconnect from the RBNB
        if (isConnected()) {
            disconnect();
        }

        logger.info("There was an error parsing the metadata response. " + "The error message was: "
                + pe.getMessage());
        return !failed;

    } finally {

        this.state = 0;

        // close the serial or socket channel
        if (this.channel != null && this.channel.isOpen()) {
            try {
                this.channel.close();

            } catch (IOException cioe) {
                logger.debug("An error occurred trying to close the byte channel. " + " The error message was: "
                        + cioe.getMessage());

            }
        }

    }

    return !failed;
}

From source file:edu.hawaii.soest.kilonalu.ctd.SeahorseSource.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  . j a  v  a 2  s.  co m*/
protected boolean execute() {
    logger.debug("SeahorseSource.execute() called.");
    // do not execute the stream if there is no connection
    if (!isConnected())
        return false;

    boolean failed = false;

    this.socketChannel = 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;

        // define a byte array that will be used to manipulate the incoming bytes
        byte[] resultArray;
        String resultString;

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

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

        this.rbnbChannelMap = new ChannelMap();
        this.channelIndex = 0;

        // initiate the session with the modem, test if is network registered
        this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND + this.MODEM_COMMAND_SUFFIX;
        this.sentCommand = queryInstrument(this.command);

        // allow time for the modem to respond
        streamingThread.sleep(this.SLEEP_INTERVAL);

        // while there are bytes to read from the socketChannel ...
        while (socketChannel.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("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" +
                //             "result pos: "   + resultBuffer.position()                  + "\t" +
                //             "result rem: "   + resultBuffer.remaining()                 + "\t" +
                //             "result cnt: "   + resultByteCount                          + "\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:

                    // the network registration status should end in OK\r\n
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) {

                        logger.debug("Received the registration status result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the network registration status string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Network Registration Result: " + resultString.trim());

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        resultArray = new byte[0];
                        resultString = "";
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        // send a request for the signal strength
                        this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND
                                + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);
                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        state = 1;
                        break;

                    } else {
                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        break;
                    }

                case 1: // report the signal strength of the Iridium modem

                    // the signal strength status should end in OK\r\n
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) {

                        logger.debug("Received the signal strength result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the signal strength status string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Signal Strength Result: " + resultString.trim());

                        int signalStrengthIndex = resultString.indexOf(this.SIGNAL_STRENGTH) + 5;

                        int signalStrength = new Integer(
                                resultString.substring(signalStrengthIndex, signalStrengthIndex + 1))
                                        .intValue();

                        // test if the signal strength is above the threshold
                        if (signalStrength > SIGNAL_THRESHOLD) {

                            resultBuffer.clear();
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 2;
                            break;

                            // the signal strength is too low, check again
                        } else {

                            resultBuffer.clear();
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            // resend a request for the signal strength
                            this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND
                                    + this.MODEM_COMMAND_SUFFIX;
                            this.sentCommand = queryInstrument(this.command);
                            // allow time for the modem to respond
                            streamingThread.sleep(this.SLEEP_INTERVAL);

                            state = 1;
                            break;

                        }

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;
                    }

                case 2: // handle the RING command from the instrument

                    // listen for the RING command 
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x47 && byteTwo == 0x4E && byteThree == 0x49 && byteFour == 0x52) {

                        logger.debug("Received the RING command.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        resultArray = new byte[0];
                        resultString = "";
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        // answer the call
                        this.command = this.MODEM_COMMAND_PREFIX + this.ANSWER_COMMAND
                                + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);
                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        state = 3;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 3: // acknowledge the connection

                    // the ready status string should end in READY\r
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x59 && byteThree == 0x44 && byteFour == 0x41) {

                        logger.debug("Received the ready status result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the connect rate and ready status string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");

                        // test the connect rate
                        logger.debug("Result from ATA: " + resultString);

                        if (resultString.indexOf(this.CONNECT_RATE) > 0) {
                            logger.debug("Connect Rate Result: " + this.CONNECT_RATE);

                            // test the ready status
                            if (resultString.indexOf(this.READY_STATUS) > 0) {
                                logger.debug("Connect Rate Result: " + this.READY_STATUS);

                                resultBuffer.clear();
                                this.resultByteCount = 0;
                                resultArray = new byte[0];
                                resultString = "";
                                byteOne = 0x00;
                                byteTwo = 0x00;
                                byteThree = 0x00;
                                byteFour = 0x00;

                                // acknowledge the ready status
                                this.command = this.ACKNOWLEDGE_COMMAND + this.MODEM_COMMAND_SUFFIX;
                                this.sentCommand = queryInstrument(this.command);

                                // allow time for the modem to receive the ACK
                                streamingThread.sleep(this.SLEEP_INTERVAL);

                                // query the instrument id
                                this.command = this.ID_COMMAND + this.MODEM_COMMAND_SUFFIX;
                                this.sentCommand = queryInstrument(this.command);

                                // allow time for the modem to respond
                                streamingThread.sleep(this.SLEEP_INTERVAL);

                                state = 4;
                                break;

                            } else {
                                logger.debug("The ready status differs from: " + this.READY_STATUS);

                                // throw an exception here?
                                break;
                            }

                        } else {
                            logger.debug("The connect rate differs from: " + this.CONNECT_RATE);

                            // throw an exception here?
                            break;
                        }

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 4: // get the instrument id

                    // the instrument ID string should end in \r
                    if (byteOne == 0x0D) {

                        logger.debug("Received the instrument ID result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the instrument ID string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Seahorse Instrument ID: " + resultString.trim());

                        // set the platformID variable
                        this.platformID = resultString.substring(0, resultString.length() - 1);

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        resultArray = new byte[0];
                        resultString = "";
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        // query the battery voltage
                        this.command = this.BATTERY_VOLTAGE_COMMAND + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        state = 5;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 5: // get the seahorse battery voltage

                    // the battery voltage string should end in \r
                    if (byteOne == 0x0D) {

                        logger.debug("Received the instrument battery voltage result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the battery voltage string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Seahorse Battery Voltage: " + resultString.trim());

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        resultArray = new byte[0];
                        resultString = "";
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        // query the GPS location
                        this.command = this.GPRMC_COMMAND + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        state = 6;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 6:

                    // the GPRMC string should end in END\r
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x44 && byteThree == 0x4E && byteFour == 0x45) {

                        logger.debug("Received the GPRMS result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the GPRMC string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Seahorse GPRMC string: " + resultString.trim());

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        resultArray = new byte[0];
                        resultString = "";
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        // query the file name for transfer
                        this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        state = 7;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 7:

                    // the file name string should end in .Z\r
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x5A && byteThree == 0x2E) {

                        logger.debug("Received the file name result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the file name string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("File name result: " + resultString.trim());

                        resultString = resultString.trim();
                        int fileNameIndex = resultString.indexOf(this.FILENAME_PREFIX);

                        //extract just the filename from the result (excise the "FILE=")
                        this.fileNameToDownload = resultString.substring(
                                (fileNameIndex + (this.FILENAME_PREFIX).length()), resultString.length());

                        logger.debug("File name to download: " + this.fileNameToDownload);

                        // test to see if the GFN command returns FILES=NONE
                        if (!(resultString.indexOf(this.END_OF_FILES) > 0)) {

                            // there is a file to download. parse the file name,
                            // get the number of blocks to transfer
                            this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX;
                            this.sentCommand = queryInstrument(this.command);

                            // allow time for the modem to respond
                            streamingThread.sleep(this.SLEEP_INTERVAL);

                            resultBuffer.clear();
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 8;
                            break;

                        } else {

                            // We have downloaded all files. Parse the data string,
                            // build the channel map, and flush the data to the Dataturbine
                            // by iterating through the data matrix.  The metadata and
                            // ASCII data strings are flushed once with the first matrix
                            // row.

                            // Parse the data file, not the cast file.
                            try {

                                // parse the CTD data file
                                this.ctdParser = new CTDParser(this.dataFileString);

                                // convert the raw frequencies and voltages to engineering
                                // units and return the data as a matrix
                                CTDConverter ctdConverter = new CTDConverter(this.ctdParser);
                                ctdConverter.convert();
                                RealMatrix convertedDataMatrix = ctdConverter.getConvertedDataValuesMatrix();

                                // Register the data and metadata channels;
                                failed = register();

                                if (!failed) {
                                    // format the first sample date and use it as the first insert
                                    // date.  Add the sampleInterval on each iteration to insert
                                    // subsequent data rows.  Sample interval is by default 
                                    // 4 scans/second for the CTD.
                                    DATE_FORMAT.setTimeZone(TZ);
                                    this.sampleDateTime = Calendar.getInstance();
                                    this.sampleDateTime
                                            .setTime(DATE_FORMAT.parse(ctdParser.getFirstSampleTime()));

                                    for (int row = 0; row < convertedDataMatrix.getRowDimension(); row++) {

                                        // Only insert the metadata fields and full ASCII text strings
                                        // with the first row of data
                                        if (row == 0) {
                                            // Add the samplingMode data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("samplingMode");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getSamplingMode());

                                            // Add the firstSampleTime data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("firstSampleTime");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getFirstSampleTime());

                                            // Add the fileName data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("fileName");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getFileName());

                                            // Add the temperatureSerialNumber data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureSerialNumber");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getTemperatureSerialNumber());

                                            // Add the conductivitySerialNumber data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivitySerialNumber");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getConductivitySerialNumber());

                                            // Add the systemUpLoadTime data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("systemUpLoadTime");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getSystemUpLoadTime());

                                            // Add the cruiseInformation data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("cruiseInformation");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getCruiseInformation());

                                            // Add the stationInformation data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("stationInformation");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getStationInformation());

                                            // Add the shipInformation data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("shipInformation");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getShipInformation());

                                            // Add the chiefScientist data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("chiefScientist");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getChiefScientist());

                                            // Add the organization data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("organization");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getOrganization());

                                            // Add the areaOfOperation data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("areaOfOperation");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getAreaOfOperation());

                                            // Add the instrumentPackage data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("instrumentPackage");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getInstrumentPackage());

                                            // Add the mooringNumber data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("mooringNumber");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getMooringNumber());

                                            // Add the instrumentLatitude data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("instrumentLatitude");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getInstrumentLatitude() });

                                            // Add the instrumentLongitude data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("instrumentLongitude");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getInstrumentLongitude() });

                                            // Add the depthSounding data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("depthSounding");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getDepthSounding() });

                                            // Add the profileNumber data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("profileNumber");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getProfileNumber());

                                            // Add the profileDirection data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("profileDirection");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getProfileDirection());

                                            // Add the deploymentNotes data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("deploymentNotes");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getDeploymentNotes());

                                            // Add the mainBatteryVoltage data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("mainBatteryVoltage");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getMainBatteryVoltage() });

                                            // Add the lithiumBatteryVoltage data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("lithiumBatteryVoltage");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getLithiumBatteryVoltage() });

                                            // Add the operatingCurrent data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("operatingCurrent");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getOperatingCurrent() });

                                            // Add the pumpCurrent data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("pumpCurrent");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser.getPumpCurrent() });

                                            // Add the channels01ExternalCurrent data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("channels01ExternalCurrent");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getChannels01ExternalCurrent() });

                                            // Add the channels23ExternalCurrent data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("channels23ExternalCurrent");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getChannels23ExternalCurrent() });

                                            // Add the loggingStatus data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("loggingStatus");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getLoggingStatus());

                                            // Add the numberOfScansToAverage data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("numberOfScansToAverage");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getNumberOfScansToAverage() });

                                            // Add the numberOfSamples data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("numberOfSamples");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getNumberOfSamples() });

                                            // Add the numberOfAvailableSamples data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("numberOfAvailableSamples");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getNumberOfAvailableSamples() });

                                            // Add the sampleInterval data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("sampleInterval");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getSampleInterval() });

                                            // Add the measurementsPerSample data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("measurementsPerSample");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getMeasurementsPerSample() });

                                            // Add the transmitRealtime data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("transmitRealtime");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getTransmitRealtime());

                                            // Add the numberOfCasts data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("numberOfCasts");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getNumberOfCasts() });

                                            // Add the minimumConductivityFrequency data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("minimumConductivityFrequency");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] {
                                                    this.ctdParser.getMinimumConductivityFrequency() });

                                            // Add the pumpDelay data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("pumpDelay");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsInt32(this.channelIndex,
                                                    new int[] { this.ctdParser.getPumpDelay() });

                                            // Add the automaticLogging data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("automaticLogging");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getAutomaticLogging());

                                            // Add the ignoreMagneticSwitch data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("ignoreMagneticSwitch");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getIgnoreMagneticSwitch());

                                            // Add the batteryType data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("batteryType");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getBatteryType());

                                            // Add the batteryCutoff data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("batteryCutoff");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getBatteryCutoff());

                                            // Add the pressureSensorType data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("pressureSensorType");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getPressureSensorType());

                                            // Add the pressureSensorRange data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("pressureSensorRange");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getPressureSensorRange());

                                            // Add the sbe38TemperatureSensor data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("sbe38TemperatureSensor");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getSbe38TemperatureSensor());

                                            // Add the gasTensionDevice data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("gasTensionDevice");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getGasTensionDevice());

                                            // Add the externalVoltageChannelZero data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("externalVoltageChannelZero");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getExternalVoltageChannelZero());

                                            // Add the externalVoltageChannelOne data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("externalVoltageChannelOne");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getExternalVoltageChannelOne());

                                            // Add the externalVoltageChannelTwo data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("externalVoltageChannelTwo");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getExternalVoltageChannelTwo());

                                            // Add the externalVoltageChannelThree data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("externalVoltageChannelThree");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getExternalVoltageChannelThree());

                                            // Add the echoCommands data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("echoCommands");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getEchoCommands());

                                            // Add the outputFormat data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("outputFormat");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getOutputFormat());

                                            // Add the temperatureCalibrationDate data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureCalibrationDate");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getTemperatureCalibrationDate());

                                            // Add the temperatureCoefficientTA0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureCoefficientTA0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getTemperatureCoefficientTA0() });

                                            // Add the temperatureCoefficientTA1 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureCoefficientTA1");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getTemperatureCoefficientTA1() });

                                            // Add the temperatureCoefficientTA2 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureCoefficientTA2");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getTemperatureCoefficientTA2() });

                                            // Add the temperatureCoefficientTA3 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureCoefficientTA3");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getTemperatureCoefficientTA3() });

                                            // Add the temperatureOffsetCoefficient data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("temperatureOffsetCoefficient");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getTemperatureOffsetCoefficient() });

                                            // Add the conductivityCalibrationDate data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCalibrationDate");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getConductivityCalibrationDate());

                                            // Add the conductivityCoefficientG data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientG");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientG() });

                                            // Add the conductivityCoefficientH data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientH");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientH() });

                                            // Add the conductivityCoefficientI data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientI");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientI() });

                                            // Add the conductivityCoefficientJ data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientJ");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientJ() });

                                            // Add the conductivityCoefficientCF0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientCF0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientCF0() });

                                            // Add the conductivityCoefficientCPCOR data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientCPCOR");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientCPCOR() });

                                            // Add the conductivityCoefficientCTCOR data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientCTCOR");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getConductivityCoefficientCTCOR() });

                                            // Add the conductivityCoefficientCSLOPE data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("conductivityCoefficientCSLOPE");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] { this.ctdParser
                                                            .getConductivityCoefficientCSLOPE() });

                                            // Add the pressureSerialNumber data to the channel map
                                            this.channelIndex = this.rbnbChannelMap.Add("pressureSerialNumber");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.ctdParser.getPressureSerialNumber());

                                            // Add the pressureCoefficientPA0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPA0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPA0() });

                                            // Add the pressureCoefficientPA1 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPA1");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPA1() });

                                            // Add the pressureCoefficientPA2 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPA2");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPA2() });

                                            // Add the pressureCoefficientPTCA0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCA0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCA0() });

                                            // Add the pressureCoefficientPTCA1 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCA1");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCA1() });

                                            // Add the pressureCoefficientPTCA2 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCA2");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCA2() });

                                            // Add the pressureCoefficientPTCB0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCB0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCB0() });

                                            // Add the pressureCoefficientPTCB1 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCB1");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCB1() });

                                            // Add the pressureCoefficientPTCB2 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTCB2");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTCB2() });

                                            // Add the pressureCoefficientPTEMPA0 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTEMPA0");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTEMPA0() });

                                            // Add the pressureCoefficientPTEMPA1 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTEMPA1");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTEMPA1() });

                                            // Add the pressureCoefficientPTEMPA2 data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureCoefficientPTEMPA2");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureCoefficientPTEMPA2() });

                                            // Add the pressureOffsetCoefficient data to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add("pressureOffsetCoefficient");
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            this.ctdParser.getPressureOffsetCoefficient() });

                                            // Insert the file into the channel map. 
                                            this.channelIndex = this.rbnbChannelMap.Add(this.rbnbChannelName);
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.dataFileString);

                                            this.channelIndex = this.rbnbChannelMap.Add("ASCIICastData");
                                            this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain");
                                            this.rbnbChannelMap.PutDataAsString(this.channelIndex,
                                                    this.castFileString);

                                        }

                                        // Add in the matrix data row to the map here
                                        List<String> variableNames = ctdParser.getDataVariableNames();
                                        List<String> variableUnits = ctdParser.getDataVariableUnits();

                                        // iterate through the variable names and add them to
                                        // the channel map.
                                        for (int variableIndex = 0; variableIndex < variableNames
                                                .size(); variableIndex++) {

                                            //  Add the variable name to the channel map
                                            this.channelIndex = this.rbnbChannelMap
                                                    .Add(variableNames.get(variableIndex));
                                            // The matrix is a double array, so set the data type below
                                            this.rbnbChannelMap.PutMime(this.channelIndex,
                                                    "application/octet-stream");
                                            // add the data to the map from the [row,column] of the
                                            // matrix (row is from the outer for loop)
                                            this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex,
                                                    new double[] {
                                                            convertedDataMatrix.getEntry(row, variableIndex) });

                                        }

                                        // Flush the channel map to the RBNB
                                        double sampleTimeAsSecondsSinceEpoch = (double) (this.sampleDateTime
                                                .getTimeInMillis() / 1000);
                                        this.rbnbChannelMap.PutTime(sampleTimeAsSecondsSinceEpoch, 0d);
                                        getSource().Flush(this.rbnbChannelMap);

                                        logger.info("Flushed data to the DataTurbine.");
                                        this.rbnbChannelMap.Clear();

                                        // samples are taken 4x per second, so increment the
                                        // sample time by 250 milliseconds for the next insert                     
                                        this.sampleDateTime.add(Calendar.MILLISECOND, 250);

                                    } // end for loop 

                                } //  end if !failed

                            } catch (Exception e) {
                                logger.debug("Failed to parse the CTD data file: " + e.getMessage());

                            }

                            // there are no more files to read. close the Tx session.
                            this.command = this.CLOSE_TRANSFER_SESSION_COMMAND + this.MODEM_COMMAND_SUFFIX;
                            this.sentCommand = queryInstrument(this.command);

                            // allow time for the modem to respond
                            streamingThread.sleep(this.SLEEP_INTERVAL);

                            // clean up
                            resultBuffer.clear();
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 10;
                            break;

                        }

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 8:

                    // the number of blocks string should end in \r
                    if (byteOne == 0x0D) {

                        logger.debug("Received the number of blocks result.");

                        this.resultByteCount++; // add the last byte found to the count

                        // add the last byte found to the result buffer
                        if (resultBuffer.remaining() > 0) {
                            resultBuffer.put(byteOne);

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

                        }

                        // report the number of blocks string
                        resultArray = new byte[this.resultByteCount];
                        resultBuffer.flip();
                        resultBuffer.get(resultArray);
                        resultString = new String(resultArray, "US-ASCII");
                        logger.debug("Number of bytes reported: " + resultString.trim());

                        int numberOfBlocksIndex = resultString.indexOf(this.BLOCKSIZE_PREFIX);

                        // If 'BLOCKSIZE=' is not found, set the index to 0
                        if (numberOfBlocksIndex == -1) {
                            numberOfBlocksIndex = 0;

                        }

                        resultString = resultString.substring(
                                (numberOfBlocksIndex + (this.BLOCKSIZE_PREFIX).length()),
                                resultString.length());

                        // convert the string to an integer
                        try {
                            this.numberOfBlocks = new Integer(resultString.trim()).intValue();
                            logger.debug("Number of bytes to download: " + this.numberOfBlocks);

                        } catch (java.lang.NumberFormatException nfe) {
                            failed = true;
                            nfe.printStackTrace();
                            logger.debug("Failed to convert returned string value "
                                    + "to an integer value.  The returned string is: " + this.numberOfBlocks);

                        }

                        // test to see if the GNB command returns DONE\r
                        if (!(resultString.indexOf(this.TRANSFER_COMPLETE) > 0)) {

                            // there are bytes to transfer. send the transfer command

                            this.command = this.TRANSFER_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX;
                            this.sentCommand = queryInstrument(this.command);

                            // allow time for the modem to respond
                            streamingThread.sleep(this.SLEEP_INTERVAL);

                            //resultBuffer.clear(); dont clear the buffer
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 9;
                            break;

                        } else {

                            // there are no more bytes to transfer.  

                            // Decompress the file, which is under zlib compression.  
                            Inflater inflater = new Inflater();
                            inflater.setInput(resultBuffer.array());
                            byte[] output = new byte[resultBuffer.capacity()];

                            int numDecompressed = inflater.inflate(output);

                            // set the appropriate string variable
                            if (this.fileNameToDownload.indexOf(DATA_FILE_PREFIX) > 0) {
                                this.dataFileString = new String(output);

                                //report the file contents to the log
                                logger.debug("File " + this.fileNameToDownload + ": ");
                                logger.debug(this.dataFileString);

                            } else {
                                this.castFileString = new String(output);

                                //report the file contents to the log
                                logger.debug("File " + this.fileNameToDownload + ": ");
                                logger.debug(this.castFileString);

                            }

                            // Ask for the next file.
                            this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX;
                            this.sentCommand = queryInstrument(this.command);

                            // allow time for the modem to respond
                            streamingThread.sleep(this.SLEEP_INTERVAL);

                            //resultBuffer.clear(); dont clear the buffer
                            this.resultByteCount = 0;
                            resultArray = new byte[0];
                            resultString = "";
                            byteOne = 0x00;
                            byteTwo = 0x00;
                            byteThree = 0x00;
                            byteFour = 0x00;

                            state = 7; //back to the file name state
                            break;

                        }

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 9:

                    // transfer up to the reported number of bytes
                    if (this.resultByteCount == this.numberOfBlocks) {

                        // we have downloaded the reported bytes. get the next section.
                        // get the number of blocks to transfer
                        this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        //resultBuffer.clear();
                        this.resultByteCount = 0;
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        state = 8;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 10:

                    // the response from the modem should end in BYE\r
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x45 && byteThree == 0x59 && byteFour == 0x42) {

                        logger.debug("Received the BYE command.");

                        // continue to disconnect. send the escape sequence
                        this.command = this.ESCAPE_SEQUENCE_COMMAND + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        state = 11;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 11:

                    // the response from the modem should end in OK\r\n
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) {

                        // now hang up.
                        this.command = this.MODEM_COMMAND_PREFIX + this.HANGUP_COMMAND
                                + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        state = 12;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                case 12:

                    // the response from the modem should end in OK\r\n
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) {

                        // we are done. re-test if is network registered
                        this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND
                                + this.MODEM_COMMAND_SUFFIX;
                        this.sentCommand = queryInstrument(this.command);

                        // allow time for the modem to respond
                        streamingThread.sleep(this.SLEEP_INTERVAL);

                        resultBuffer.clear();
                        this.resultByteCount = 0;
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;

                        state = 0;
                        break;

                    } else {

                        // still in the middle of the result, keep adding bytes
                        this.resultByteCount++; // add each byte found

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

                        }

                        break;

                    }

                } // 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 socketChannel bytes to read)
        socketChannel.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 (java.lang.InterruptedException ine) {
        failed = true;
        ine.printStackTrace();
        return !failed;

    } catch (java.util.zip.DataFormatException dfe) {
        failed = true;
        dfe.printStackTrace();
        return !failed;
    }

    return !failed;
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxSource.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  . ja  va  2s . c o  m
protected boolean execute() {
    logger.debug("DavisWxSource.execute() called.");
    // do not execute the stream if there is no connection
    if (!isConnected())
        return false;

    boolean failed = false;

    // while data are being sent, read them into the buffer
    try {

        this.socketChannel = getSocketConnection();

        // 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();
        int channelIndex = 0;

        // add the raw binary LOOP packet data
        //channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
        //rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the barTrendAsString field data
        channelIndex = rbnbChannelMap.Add("barTrendAsString"); // Falling Slowly
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the barometer field data
        channelIndex = rbnbChannelMap.Add("barometer"); // 29.9
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch Hg");

        // add the insideTemperature field data
        channelIndex = rbnbChannelMap.Add("insideTemperature"); // 83.9
        rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees F");

        // add the insideHumidity field data
        channelIndex = rbnbChannelMap.Add("insideHumidity"); // 51
        rbnbChannelMap.PutUserInfo(channelIndex, "units=percent");

        // add the outsideTemperature field data
        channelIndex = rbnbChannelMap.Add("outsideTemperature"); // 76.7
        rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees F");

        // add the windSpeed field data
        channelIndex = rbnbChannelMap.Add("windSpeed"); // 5
        rbnbChannelMap.PutUserInfo(channelIndex, "units=mph");

        // add the tenMinuteAverageWindSpeed field data
        channelIndex = rbnbChannelMap.Add("tenMinuteAverageWindSpeed"); // 4
        rbnbChannelMap.PutUserInfo(channelIndex, "units=mph");

        // add the windDirection field data
        channelIndex = rbnbChannelMap.Add("windDirection"); // 80
        rbnbChannelMap.PutUserInfo(channelIndex, "units=degrees");

        // add the outsideHumidity field data
        channelIndex = rbnbChannelMap.Add("outsideHumidity"); // 73
        rbnbChannelMap.PutUserInfo(channelIndex, "units=percent");

        // add the rainRate field data
        channelIndex = rbnbChannelMap.Add("rainRate"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch/hour");

        // add the uvRadiation field data
        channelIndex = rbnbChannelMap.Add("uvRadiation"); // 0
        rbnbChannelMap.PutUserInfo(channelIndex, "UV index");

        // add the solarRadiation field data
        channelIndex = rbnbChannelMap.Add("solarRadiation"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "watt/m^2");

        // add the stormRain field data
        channelIndex = rbnbChannelMap.Add("stormRain"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "inch");

        // add the currentStormStartDate field data
        channelIndex = rbnbChannelMap.Add("currentStormStartDate"); // -1--1-1999
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the dailyRain field data
        channelIndex = rbnbChannelMap.Add("dailyRain"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the monthlyRain field data
        channelIndex = rbnbChannelMap.Add("monthlyRain"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the yearlyRain field data
        channelIndex = rbnbChannelMap.Add("yearlyRain"); // 15.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the dailyEvapoTranspiration field data
        channelIndex = rbnbChannelMap.Add("dailyEvapoTranspiration"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the monthlyEvapoTranspiration field data
        channelIndex = rbnbChannelMap.Add("monthlyEvapoTranspiration"); // 0.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the yearlyEvapoTranspiration field data
        channelIndex = rbnbChannelMap.Add("yearlyEvapoTranspiration"); // 93.0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=inch");

        // add the transmitterBatteryStatus field data
        channelIndex = rbnbChannelMap.Add("transmitterBatteryStatus"); // 0
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the consoleBatteryVoltage field data
        channelIndex = rbnbChannelMap.Add("consoleBatteryVoltage"); // 4.681640625
        rbnbChannelMap.PutUserInfo(channelIndex, "units=volts");

        // add the forecastAsString field data
        channelIndex = rbnbChannelMap.Add("forecastAsString"); // Partially Cloudy
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the forecastRuleNumberAsString field data
        //channelIndex = rbnbChannelMap.Add("forecastRuleNumberAsString");      // Increasing clouds with little temperature change.
        //rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the timeOfSunrise field data
        channelIndex = rbnbChannelMap.Add("timeOfSunrise"); // 05:49
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // add the timeOfSunset field data
        channelIndex = rbnbChannelMap.Add("timeOfSunset"); // 19:11
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        channelIndex = rbnbChannelMap.Add("DecimalASCIISampleData"); // sample data as ASCII
        rbnbChannelMap.PutUserInfo(channelIndex, "units=none");

        // register the channel map of variables and units with the DataTurbine
        getSource().Register(rbnbChannelMap);
        // reset variables for use with the incoming data
        rbnbChannelMap.Clear();
        channelIndex = 0;

        // wake the instrument with an initial '\n' command
        this.command = this.commandSuffix;
        this.sentCommand = queryInstrument(this.command);

        // allow time for the instrument response
        streamingThread.sleep(2000);
        this.command = this.commandPrefix + this.takeSampleCommand + this.commandSuffix;
        this.sentCommand = queryInstrument(command);

        // while there are bytes to read from the socket ...
        while (this.socketChannel.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("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 is begun by "ACK L" (the first part of ACK + "LOOP")
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x4C && byteTwo == 0x06) {

                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }

                        // we've found the beginning 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 "\n\r"
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x0D && byteTwo == 0x0A) {

                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

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

                        }
                        state = 3;
                        break;
                    } else { // not 0x0A0D

                        // 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 0x0A0D EOL

                case 3:

                    // At this point, we've found the \n\r delimiter, read the first
                    // of 2 CRC bytes
                    sampleByteCount++; // add the last byte found to the count

                    // add the last byte found to the sample buffer
                    if (sampleBuffer.remaining() > 0) {
                        sampleBuffer.put(byteOne);

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

                    }
                    state = 4;
                    break;

                case 4:

                    // At this point, we've found the \n\r delimiter, read the second
                    // of 2 CRC bytes
                    sampleByteCount++; // add the last byte found to the count

                    // add the last byte found to the sample buffer
                    if (sampleBuffer.remaining() > 0) {
                        sampleBuffer.put(byteOne);

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

                    }
                    state = 0;

                    // 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];

                    try {
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        // parse and send the sample to the data turbine
                        this.davisWxParser = new DavisWxParser(sampleBuffer);

                    } catch (java.lang.Exception e) {
                        logger.info(
                                "There was a problem parsing the binary weather LOOP packet. Skipping this sample.");
                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;
                        sampleBuffer.clear();
                        sampleByteCount = 0;
                        rbnbChannelMap.Clear();
                        break;
                    }

                    // create a character string to store characters from the TCP stream
                    StringBuilder decimalASCIISampleData = new StringBuilder();

                    rbnbChannelMap.PutTimeAuto("server");

                    // add the raw binary LOOP packet data
                    //channelIndex = rbnbChannelMap.Add(getRBNBChannelName());
                    //rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    //rbnbChannelMap.PutDataAsByteArray(channelIndex, sampleArray);         // raw binary LOOP packet

                    // add the barTrendAsString field data
                    channelIndex = rbnbChannelMap.Add("barTrendAsString"); // Falling Slowly
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getBarTrendAsString());
                    decimalASCIISampleData.append(
                            String.format("\"%16s\"", (Object) davisWxParser.getBarTrendAsString()) + ", ");

                    // add the packetType field to the ASCII string only
                    decimalASCIISampleData.append(
                            String.format("%1d", (Object) new Integer(davisWxParser.getPacketType())) + ", ");

                    // add the nextRecord field to the ASCII string only
                    decimalASCIISampleData.append(
                            String.format("%04d", (Object) new Integer(davisWxParser.getNextRecord())) + ", ");

                    // add the barometer field data
                    channelIndex = rbnbChannelMap.Add("barometer"); // 29.9
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getBarometer() });
                    decimalASCIISampleData.append(
                            String.format("%06.4f", (Object) new Float(davisWxParser.getBarometer())) + ", ");

                    // add the insideTemperature field data
                    channelIndex = rbnbChannelMap.Add("insideTemperature"); // 83.9
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getInsideTemperature() });
                    decimalASCIISampleData.append(
                            String.format("%05.2f", (Object) new Float(davisWxParser.getInsideTemperature()))
                                    + ", ");

                    // add the insideHumidity field data
                    channelIndex = rbnbChannelMap.Add("insideHumidity"); // 51
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex,
                            new int[] { davisWxParser.getInsideHumidity() });
                    decimalASCIISampleData.append(
                            String.format("%03d", (Object) new Integer(davisWxParser.getInsideHumidity()))
                                    + ", ");

                    // add the outsideTemperature field data
                    channelIndex = rbnbChannelMap.Add("outsideTemperature"); // 76.7
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getOutsideTemperature() });
                    decimalASCIISampleData.append(
                            String.format("%05.2f", (Object) new Float(davisWxParser.getOutsideTemperature()))
                                    + ", ");

                    // add the windSpeed field data
                    channelIndex = rbnbChannelMap.Add("windSpeed"); // 5
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getWindSpeed() });
                    decimalASCIISampleData.append(
                            String.format("%03d", (Object) new Integer(davisWxParser.getWindSpeed())) + ", ");

                    // add the tenMinuteAverageWindSpeed field data
                    channelIndex = rbnbChannelMap.Add("tenMinuteAverageWindSpeed"); // 4
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex,
                            new int[] { davisWxParser.getTenMinuteAverageWindSpeed() });
                    decimalASCIISampleData.append(String.format("%03d",
                            (Object) new Integer(davisWxParser.getTenMinuteAverageWindSpeed())) + ", ");

                    // add the windDirection field data
                    channelIndex = rbnbChannelMap.Add("windDirection"); // 80
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getWindDirection() });
                    decimalASCIISampleData.append(
                            String.format("%03d", (Object) new Integer(davisWxParser.getWindDirection()))
                                    + ", ");

                    // add the extraTemperature fields as ASCII only
                    float[] extraTemperatures = davisWxParser.getExtraTemperatures();
                    for (float temperature : extraTemperatures) {
                        decimalASCIISampleData
                                .append(String.format("%05.2f", (Object) new Float(temperature)) + ", ");

                    }

                    // add the soilTemperature fields as ASCII only
                    float[] soilTemperatures = davisWxParser.getSoilTemperatures();
                    for (float soil : soilTemperatures) {
                        decimalASCIISampleData.append(String.format("%05.2f", (Object) new Float(soil)) + ", ");

                    }

                    // add the leafTemperature fields as ASCII only
                    float[] leafTemperatures = davisWxParser.getLeafTemperatures();
                    for (float leaf : leafTemperatures) {
                        decimalASCIISampleData.append(String.format("%05.2f", (Object) new Float(leaf)) + ", ");

                    }

                    // add the outsideHumidity field data
                    channelIndex = rbnbChannelMap.Add("outsideHumidity"); // 73
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex,
                            new int[] { davisWxParser.getOutsideHumidity() });
                    decimalASCIISampleData.append(
                            String.format("%03d", (Object) new Integer(davisWxParser.getOutsideHumidity()))
                                    + ", ");

                    // add the rainRate field data
                    channelIndex = rbnbChannelMap.Add("rainRate"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getRainRate() });
                    decimalASCIISampleData.append(
                            String.format("%04.2f", (Object) new Float(davisWxParser.getRainRate())) + ", ");

                    // add the uvRadiation field data
                    channelIndex = rbnbChannelMap.Add("uvRadiation"); // 0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { davisWxParser.getUvRadiation() });
                    decimalASCIISampleData.append(
                            String.format("%03d", (Object) new Integer(davisWxParser.getUvRadiation())) + ", ");

                    // add the solarRadiation field data
                    channelIndex = rbnbChannelMap.Add("solarRadiation"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getSolarRadiation() });
                    decimalASCIISampleData.append(
                            String.format("%04.1f", (Object) new Float(davisWxParser.getSolarRadiation()))
                                    + ", ");

                    // add the stormRain field data
                    channelIndex = rbnbChannelMap.Add("stormRain"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getStormRain() });
                    decimalASCIISampleData.append(
                            String.format("%04.2f", (Object) new Float(davisWxParser.getStormRain())) + ", ");

                    // add the currentStormStartDate field data
                    channelIndex = rbnbChannelMap.Add("currentStormStartDate"); // -1--1-1999
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getCurrentStormStartDate());
                    decimalASCIISampleData.append(
                            String.format("%10s", (Object) davisWxParser.getCurrentStormStartDate()) + ", ");

                    // add the dailyRain field data
                    channelIndex = rbnbChannelMap.Add("dailyRain"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { davisWxParser.getDailyRain() });
                    decimalASCIISampleData.append(
                            String.format("%04.2f", (Object) new Float(davisWxParser.getDailyRain())) + ", ");

                    // add the monthlyRain field data
                    channelIndex = rbnbChannelMap.Add("monthlyRain"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getMonthlyRain() });
                    decimalASCIISampleData.append(
                            String.format("%04.2f", (Object) new Float(davisWxParser.getMonthlyRain())) + ", ");

                    // add the yearlyRain field data
                    channelIndex = rbnbChannelMap.Add("yearlyRain"); // 15.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getYearlyRain() });
                    decimalASCIISampleData.append(
                            String.format("%04.2f", (Object) new Float(davisWxParser.getYearlyRain())) + ", ");

                    // add the dailyEvapoTranspiration field data
                    channelIndex = rbnbChannelMap.Add("dailyEvapoTranspiration"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getDailyEvapoTranspiration() });
                    decimalASCIISampleData.append(String.format("%04.2f",
                            (Object) new Float(davisWxParser.getDailyEvapoTranspiration())) + ", ");

                    // add the monthlyEvapoTranspiration field data
                    channelIndex = rbnbChannelMap.Add("monthlyEvapoTranspiration"); // 0.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getMonthlyEvapoTranspiration() });
                    decimalASCIISampleData.append(String.format("%04.2f",
                            (Object) new Float(davisWxParser.getMonthlyEvapoTranspiration())) + ", ");

                    // add the yearlyEvapoTranspiration field data
                    channelIndex = rbnbChannelMap.Add("yearlyEvapoTranspiration"); // 93.0
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getYearlyEvapoTranspiration() });
                    decimalASCIISampleData.append(String.format("%04.2f",
                            (Object) new Float(davisWxParser.getYearlyEvapoTranspiration())) + ", ");

                    // add the consoleBatteryVoltage field data
                    channelIndex = rbnbChannelMap.Add("consoleBatteryVoltage"); // 4.681640625
                    rbnbChannelMap.PutMime(channelIndex, "application/octet-stream");
                    rbnbChannelMap.PutDataAsFloat32(channelIndex,
                            new float[] { davisWxParser.getConsoleBatteryVoltage() });
                    decimalASCIISampleData.append(String.format("%04.2f",
                            (Object) new Float(davisWxParser.getConsoleBatteryVoltage())) + ", ");

                    // add the forecastAsString field data
                    channelIndex = rbnbChannelMap.Add("forecastAsString"); // Partially Cloudy
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getForecastAsString());
                    decimalASCIISampleData.append(
                            String.format("\"%47s\"", (Object) davisWxParser.getForecastAsString()) + ", ");

                    // add the forecastRuleNumberAsString field data as ASCII only
                    decimalASCIISampleData.append(
                            String.format("\"%167s\"", (Object) davisWxParser.getForecastRuleNumberAsString())
                                    + ", ");

                    // add the timeOfSunrise field data
                    channelIndex = rbnbChannelMap.Add("timeOfSunrise"); // 05:49
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getTimeOfSunrise());
                    decimalASCIISampleData
                            .append(String.format("%5s", (Object) davisWxParser.getTimeOfSunrise()) + ", ");

                    // add the timeOfSunset field data
                    channelIndex = rbnbChannelMap.Add("timeOfSunset"); // 19:11
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, davisWxParser.getTimeOfSunset());
                    decimalASCIISampleData
                            .append(String.format("%5s", (Object) davisWxParser.getTimeOfSunset()) + ", ");

                    // then add a timestamp to the end of the sample
                    DATE_FORMAT.setTimeZone(TZ);
                    String sampleDateAsString = DATE_FORMAT.format(new Date()).toString();
                    decimalASCIISampleData.append(sampleDateAsString);
                    decimalASCIISampleData.append("\n");

                    // add the ASCII CSV string of selected fields as a channel
                    channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); // 19:11
                    rbnbChannelMap.PutMime(channelIndex, "text/plain");
                    rbnbChannelMap.PutDataAsString(channelIndex, decimalASCIISampleData.toString());

                    // finally, send the channel map of data to the DataTurbine
                    getSource().Flush(rbnbChannelMap);
                    String sampleString = new String(Hex.encodeHex(sampleArray));
                    logger.info("Sample: " + sampleString);
                    logger.debug("barTrendAsString:               " + davisWxParser.getBarTrendAsString());
                    logger.debug("barometer:                      " + davisWxParser.getBarometer());
                    logger.debug("insideTemperature:              " + davisWxParser.getInsideTemperature());
                    logger.debug("insideHumidity:                 " + davisWxParser.getInsideHumidity());
                    logger.debug("outsideTemperature:             " + davisWxParser.getOutsideTemperature());
                    logger.debug("windSpeed:                      " + davisWxParser.getWindSpeed());
                    logger.debug(
                            "tenMinuteAverageWindSpeed:      " + davisWxParser.getTenMinuteAverageWindSpeed());
                    logger.debug("windDirection:                  " + davisWxParser.getWindDirection());
                    logger.debug("outsideHumidity:                " + davisWxParser.getOutsideHumidity());
                    logger.debug("rainRate:                       " + davisWxParser.getRainRate());
                    logger.debug("uvRadiation:                    " + davisWxParser.getUvRadiation());
                    logger.debug("solarRadiation:                 " + davisWxParser.getSolarRadiation());
                    logger.debug("stormRain:                      " + davisWxParser.getStormRain());
                    logger.debug("currentStormStartDate:          " + davisWxParser.getCurrentStormStartDate());
                    logger.debug("dailyRain:                      " + davisWxParser.getDailyRain());
                    logger.debug("monthlyRain:                    " + davisWxParser.getMonthlyRain());
                    logger.debug("yearlyRain:                     " + davisWxParser.getYearlyRain());
                    logger.debug(
                            "dailyEvapoTranspiration:        " + davisWxParser.getDailyEvapoTranspiration());
                    logger.debug(
                            "monthlyEvapoTranspiration:      " + davisWxParser.getMonthlyEvapoTranspiration());
                    logger.debug(
                            "yearlyEvapoTranspiration:       " + davisWxParser.getYearlyEvapoTranspiration());
                    logger.debug("transmitterBatteryStatus:       "
                            + Arrays.toString(davisWxParser.getTransmitterBatteryStatus()));
                    logger.debug("consoleBatteryVoltage:          " + davisWxParser.getConsoleBatteryVoltage());
                    logger.debug("forecastAsString:               " + davisWxParser.getForecastAsString());
                    //logger.debug("forecastRuleNumberAsString:     " + davisWxParser.getForecastRuleNumberAsString());
                    logger.debug("timeOfSunrise:                  " + davisWxParser.getTimeOfSunrise());
                    logger.debug("timeOfSunset:                   " + davisWxParser.getTimeOfSunset());
                    logger.info(" flushed 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;

                    // Once the sample is flushed, take a new sample
                    // allow time for the instrument response
                    streamingThread.sleep(2000);
                    this.command = this.commandPrefix + this.takeSampleCommand + this.commandSuffix;
                    this.sentCommand = queryInstrument(command);

                } // 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)
        this.socketChannel.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;
    } catch (java.lang.InterruptedException ine) {
        failed = true;
        ine.printStackTrace();
        return !failed;

    }

    return !failed;
}

From source file:com.l2jfree.gameserver.network.L2ClientPacketHandlerFinal.java

@Override
public L2ClientPacket handlePacket(ByteBuffer buf, L2Client client, final int opcode) {
    L2ClientPacket msg = null;//from  w w  w  .j a  va  2  s.  c om
    GameClientState state = client.getState();

    switch (state) {
    case CONNECTED:
        switch (opcode) {
        case 0x0e:
            msg = new ProtocolVersion();
            break;
        case 0x2b:
            msg = new AuthLogin();
            break;
        default:
            printDebug(buf, client, opcode);
            break;
        }
        break;
    case AUTHED:
        switch (opcode) {
        case 0x00:
            msg = new Logout();
            break;
        case 0x0c:
            msg = new NewCharacter();
            break;
        case 0x0d:
            msg = new CharacterDelete();
            break;
        case 0x0f:
            // MoveBackwardsToLocation, lag issue
            break;
        case 0x12:
            msg = new CharacterSelected();
            break;
        case 0x13:
            msg = new NewCharacterInit();
            break;
        case 0x57:
            // RequestRestart, lag issue
            break;
        case 0x7b:
            msg = new CharacterRestore();
            break;
        case 0xd0:
            int id2 = -1;
            if (buf.remaining() >= 2) {
                id2 = buf.getShort() & 0xffff;
            } else {
                if (Config.PACKET_HANDLER_DEBUG)
                    _log.warn("Client: " + client.toString() + " sent a 0xd0 without the second opcode.");
                break;
            }

            switch (id2) {
            case 0x24:
                // RequestSaveInventoryOrder, lag issue
                break;
            case 0x36:
                msg = new CharacterPrevState();
                break;
            case 0x39:
                // most probably using L2NET
                break;
            case 0x3d: // client definitely sends it right now, enable if supposed to be
                //msg = new RequestAllFortressInfo();
                break;
            case 0x5a:
                int id3 = 0;
                if (buf.remaining() >= 4) {
                    id3 = buf.getInt() & 0xffffffff;
                } else {
                    if (Config.PACKET_HANDLER_DEBUG)
                        _log.warn("Client: " + client + " sent a 0xd0:0x5a without the third opcode.");
                    break;
                }

                switch (id3) {
                case 0x00:
                    msg = new RequestExCubeGameChangeTeam();
                    break;
                default:
                    printDebug(buf, client, opcode, id2, id3);
                    break;
                }
                break;
            default:
                printDebug(buf, client, opcode, id2);
            }
            break;
        // to avoid unnecessary warning about invalid opcode (if the client lags a bit, then it starts spamming this packet)
        case 0x59: // ValidatePosition
            break;
        //
        default:
            printDebug(buf, client, opcode);
            break;
        }
        break;
    case IN_GAME:
        switch (opcode) {
        // to avoid unnecessary warning about invalid opcode (player clicked the button multiple times)
        case 0x12: // CharacterSelected
            break;
        //
        case 0x00:
            msg = new Logout();
            break;
        case 0x01:
            msg = new AttackRequest();
            break;
        case 0x03:
            msg = new RequestStartPledgeWar();
            break;
        case 0x04:
            msg = new RequestReplyStartPledgeWar();
            break;
        case 0x05:
            msg = new RequestStopPledgeWar();
            break;
        case 0x06: // RequestSCCheck
            msg = new RequestReplyStopPledgeWar();
            break;
        case 0x07:
            msg = new RequestSurrenderPledgeWar();
            break;
        case 0x08:
            msg = new RequestReplySurrenderPledgeWar();
            break;
        case 0x09:
            msg = new RequestSetPledgeCrest();
            break;
        case 0x0b:
            msg = new RequestGiveNickName();
            break;
        case 0x0f:
            msg = new MoveBackwardToLocation();
            break;
        case 0x10:
            // Say
            break;
        case 0x11:
            msg = new EnterWorld();
            break;
        case 0x14:
            msg = new RequestItemList();
            break;
        case 0x15:
            // RequestEquipItem
            break;
        case 0x16:
            msg = new RequestUnEquipItem();
            break;
        case 0x17:
            msg = new RequestDropItem();
            break;
        case 0x19:
            msg = new UseItem();
            break;
        case 0x1a:
            msg = new TradeRequest();
            break;
        case 0x1b:
            msg = new AddTradeItem();
            break;
        case 0x1c:
            msg = new TradeDone();
            break;
        case 0x1f:
            msg = new Action();
            break;
        case 0x22:
            msg = new RequestLinkHtml();
            break;
        case 0x23:
            msg = new RequestBypassToServer();
            break;
        case 0x24:
            msg = new RequestBBSwrite();
            break;
        case 0x25:
            // RequestCreatePledge
            break;
        case 0x26:
            msg = new RequestJoinPledge();
            break;
        case 0x27:
            msg = new RequestAnswerJoinPledge();
            break;
        case 0x28:
            msg = new RequestWithdrawalPledge();
            break;
        case 0x29:
            msg = new RequestOustPledgeMember();
            break;
        case 0x2c:
            msg = new RequestGetItemFromPet();
            break;
        case 0x2e:
            msg = new RequestAllyInfo();
            break;
        case 0x2f:
            msg = new RequestCrystallizeItem();
            break;
        case 0x30:
            msg = new RequestPrivateStoreManageSell();
            break;
        case 0x31:
            msg = new SetPrivateStoreListSell();
            break;
        case 0x32:
            msg = new AttackRequest();
            break;
        case 0x33:
            // RequestTeleportPacket
            break;
        case 0x34:
            msg = new RequestSocialAction();
            break;
        case 0x35:
            msg = new ChangeMoveType();
            break;
        case 0x36:
            msg = new ChangeWaitType();
            break;
        case 0x37:
            msg = new RequestSellItem();
            break;
        case 0x38:
            // RequestMagicSkillList
            break;
        case 0x39:
            msg = new RequestMagicSkillUse();
            break;
        case 0x3a:
            msg = new SendAppearing();
            break;
        case 0x3b:
            if (Config.ALLOW_WAREHOUSE)
                msg = new SendWareHouseDepositList();
            break;
        case 0x3c:
            msg = new SendWareHouseWithDrawList();
            break;
        case 0x3d:
            msg = new RequestShortCutReg();
            break;
        case 0x3f:
            msg = new RequestShortCutDel();
            break;
        case 0x40:
            msg = new RequestBuyItem();
            break;
        case 0x41:
            // RequestDismissPledge
            break;
        case 0x42:
            msg = new RequestJoinParty();
            break;
        case 0x43:
            msg = new RequestAnswerJoinParty();
            break;
        case 0x44:
            msg = new RequestWithDrawalParty();
            break;
        case 0x45:
            msg = new RequestOustPartyMember();
            break;
        case 0x46:
            // RequestDismissParty
            break;
        case 0x47:
            msg = new CannotMoveAnymore();
            break;
        case 0x48:
            msg = new RequestTargetCanceld();
            break;
        case 0x49:
            msg = new Say2();
            break;
        case 0x4a:
            int id_2 = -1;
            if (buf.remaining() >= 2) {
                id_2 = buf.get() & 0xff;
            } else {
                if (Config.PACKET_HANDLER_DEBUG)
                    _log.warn("Client: " + client.toString() + " sent a 0x4a without the second opcode.");
                break;
            }

            switch (id_2) {
            case 0x00:
                msg = new SuperCmdCharacterInfo();
                break;
            case 0x01:
                msg = new SuperCmdSummonCmd();
                break;
            case 0x02:
                msg = new SuperCmdServerStatus();
                break;
            case 0x03:
                msg = new SendL2ParamSetting();
                break;
            default:
                printDebug(buf, client, opcode, id_2);
                break;
            }
            break;
        case 0x4d:
            msg = new RequestPledgeMemberList();
            break;
        case 0x4f:
            //RequestMagicList
            break;
        case 0x50:
            msg = new RequestSkillList();
            break;
        case 0x52:
            msg = new MoveWithDelta();
            break;
        case 0x53:
            msg = new RequestGetOnVehicle();
            break;
        case 0x54:
            msg = new RequestGetOffVehicle();
            break;
        case 0x55:
            msg = new AnswerTradeRequest();
            break;
        case 0x56:
            msg = new RequestActionUse();
            break;
        case 0x57:
            msg = new RequestRestart();
            break;
        case 0x58:
            msg = new RequestSiegeInfo();
            break;
        case 0x59:
            msg = new ValidatePosition();
            break;
        case 0x5a:
            // RequestSEKCustom
            break;
        case 0x5b:
            // StartRotating
            break;
        case 0x5c:
            // FinishRotating
            break;
        case 0x5e:
            msg = new RequestShowBoard();
            break;
        case 0x5f:
            msg = new RequestEnchantItem();
            break;
        case 0x60:
            msg = new RequestDestroyItem();
            break;
        case 0x62:
            msg = new RequestQuestList();
            break;
        case 0x63: // RequestDestroyQuest
            msg = new RequestQuestAbort();
            break;
        case 0x65:
            msg = new RequestPledgeInfo();
            break;
        case 0x66:
            msg = new RequestPledgeExtendedInfo();
            break;
        case 0x67:
            msg = new RequestPledgeCrest();
            break;
        case 0x6b:
            msg = new RequestSendFriendMsg();
            break;
        case 0x6c:
            msg = new RequestShowMiniMap();
            break;
        case 0x6d:
            // RequestSendMsnChatLog
            break;
        case 0x6e: //RequestReload
            msg = new RequestRecordInfo();
            break;
        case 0x6f:
            msg = new RequestHennaEquip();
            break;
        case 0x70:
            msg = new RequestHennaRemoveList();
            break;
        case 0x71:
            msg = new RequestHennaItemRemoveInfo();
            break;
        case 0x72:
            msg = new RequestHennaRemove();
            break;
        case 0x73:
            msg = new RequestAquireSkillInfo();
            break;
        case 0x74:
            msg = new SendBypassBuildCmd();
            break;
        case 0x75:
            msg = new RequestMoveToLocationInVehicle();
            break;
        case 0x76:
            msg = new CannotMoveAnymoreInVehicle();
            break;
        case 0x77:
            msg = new RequestFriendInvite();
            break;
        case 0x78:
            msg = new RequestAnswerFriendInvite();
            break;
        case 0x79:
            msg = new RequestFriendList();
            break;
        case 0x7a:
            msg = new RequestFriendDel();
            break;
        case 0x7c: // send when a skill to be learned is selected
            msg = new RequestAquireSkill();
            break;
        case 0x7d:
            msg = new RequestRestartPoint();
            break;
        case 0x7e:
            msg = new RequestGMCommand();
            break;
        case 0x7f:
            msg = new RequestPartyMatchConfig();
            break;
        case 0x80:
            msg = new RequestPartyMatchList();
            break;
        case 0x81:
            msg = new RequestPartyMatchDetail();
            break;
        case 0x83:
            msg = new RequestPrivateStoreBuy();
            break;
        case 0x85:
            msg = new RequestTutorialLinkHtml();
            break;
        case 0x86:
            msg = new RequestTutorialPassCmdToServer();
            break;
        case 0x87:
            msg = new RequestTutorialQuestionMark();
            break;
        case 0x88:
            msg = new RequestTutorialClientEvent();
            break;
        case 0x89:
            msg = new RequestPetition();
            break;
        case 0x8a:
            msg = new RequestPetitionCancel();
            break;
        case 0x8b:
            msg = new RequestGmList();
            break;
        case 0x8c:
            msg = new RequestJoinAlly();
            break;
        case 0x8d:
            msg = new RequestAnswerJoinAlly();
            break;
        case 0x8e:
            msg = new RequestWithdrawAlly();
            break;
        case 0x8f:
            msg = new RequestOustAlly();
            break;
        case 0x90:
            msg = new RequestDismissAlly();
            break;
        case 0x91:
            msg = new RequestSetAllyCrest();
            break;
        case 0x92:
            msg = new RequestAllyCrest();
            break;
        case 0x93:
            msg = new RequestChangePetName();
            break;
        case 0x94:
            msg = new RequestPetUseItem();
            break;
        case 0x95:
            msg = new RequestGiveItemToPet();
            break;
        case 0x96:
            msg = new RequestPrivateStoreQuitSell();
            break;
        case 0x97:
            msg = new SetPrivateStoreMsgSell();
            break;
        case 0x98:
            msg = new RequestPetGetItem();
            break;
        case 0x99:
            msg = new RequestPrivateStoreManageBuy();
            break;
        case 0x9a:
            msg = new SetPrivateStoreListBuy();
            break;
        case 0x9c:
            msg = new RequestPrivateStoreQuitBuy();
            break;
        case 0x9d:
            msg = new SetPrivateStoreMsgBuy();
            break;
        case 0x9f: // SendPrivateStoreBuyList
            msg = new RequestPrivateStoreSell();
            break;
        case 0xa0:
            //SendTimeCheckPacket
            break;
        case 0xa6:
            // RequestSkillCoolTime
            break;
        case 0xa7:
            msg = new RequestPackageSendableItemList();
            break;
        case 0xa8:
            msg = new RequestPackageSend();
            break;
        case 0xa9:
            msg = new RequestBlock();
            break;
        case 0xaa:
            msg = new RequestSiegeInfo();
            break;
        case 0xab:
            msg = new RequestSiegeAttackerList();
            break;
        case 0xac:
            msg = new RequestSiegeDefenderList();
            break;
        case 0xad:
            msg = new RequestJoinSiege();
            break;
        case 0xae:
            msg = new RequestConfirmSiegeWaitingList();
            break;
        case 0xaf:
            // RequestSetCastleSiegeTime
            break;
        case 0xb0:
            msg = new MultiSellChoose();
            break;
        case 0xb1:
            // NetPing
            break;
        case 0xb2:
            msg = new RequestRemainTime();
            break;
        case 0xb3:
            msg = new BypassUserCmd();
            break;
        case 0xb4:
            msg = new SnoopQuit();
            break;
        case 0xb5:
            msg = new RequestRecipeBookOpen();
            break;
        case 0xb6: // RequestRecipeItemDelete
            msg = new RequestRecipeBookDestroy();
            break;
        case 0xb7:
            msg = new RequestRecipeItemMakeInfo();
            break;
        case 0xb8:
            msg = new RequestRecipeItemMakeSelf();
            break;
        case 0xb9:
            // RequestRecipeShopManageList
            break;
        case 0xba:
            msg = new RequestRecipeShopMessageSet();
            break;
        case 0xbb:
            msg = new RequestRecipeShopListSet();
            break;
        case 0xbc:
            msg = new RequestRecipeShopManageQuit();
            break;
        case 0xbd:
            // RequestRecipeShopManageCancel
            break;
        case 0xbe:
            msg = new RequestRecipeShopMakeInfo();
            break;
        case 0xbf:
            msg = new RequestRecipeShopMakeItem();
            break;
        case 0xc0:
            msg = new RequestRecipeShopSellList();
            break;
        case 0xc1:
            msg = new RequestObserverEnd();
            break;
        case 0xc2: // VoteSociality
            msg = new VoteSociality();
            break;
        case 0xc3: // RequestHennaItemList
            msg = new RequestHennaDrawList();
            break;
        case 0xc4:
            msg = new RequestHennaItemDrawInfo();
            break;
        case 0xc5:
            msg = new RequestBuySeed();
            break;
        case 0xc6:
            msg = new ConfirmDlgAnswer();
            break;
        case 0xc7:
            msg = new RequestWearItem();
            break;
        case 0xc8:
            msg = new RequestSSQStatus();
            break;
        case 0xc9:
            // PetitionVote
            break;
        case 0xcb:
            msg = new GameGuardReply();
            break;
        case 0xcc:
            // Clan Privileges
            msg = new RequestPledgePower();
            break;
        case 0xcd:
            msg = new RequestMakeMacro();
            break;
        case 0xce:
            msg = new RequestDeleteMacro();
            break;
        // Manor
        case 0xcf: // RequestProcureCrop
            msg = new RequestBuyProcure();
            break;
        case 0xd0:
            int id2 = -1;
            if (buf.remaining() >= 2) {
                id2 = buf.getShort() & 0xffff;
            } else {
                if (Config.PACKET_HANDLER_DEBUG)
                    _log.warn("Client: " + client.toString() + " sent a 0xd0 without the second opcode.");
                break;
            }

            switch (id2) {
            case 0x01:
                msg = new RequestManorList();
                break;
            case 0x02:
                msg = new RequestProcureCropList();
                break;
            case 0x03:
                msg = new RequestSetSeed();
                break;
            case 0x04:
                msg = new RequestSetCrop();
                break;
            case 0x05:
                msg = new RequestWriteHeroWords();
                break;
            case 0x06:
                msg = new RequestExAskJoinMPCC();
                break;
            case 0x07:
                msg = new RequestExAcceptJoinMPCC();
                break;
            case 0x08:
                msg = new RequestExOustFromMPCC();
                break;
            case 0x09:
                msg = new RequestOustFromPartyRoom();
                break;
            case 0x0a:
                msg = new RequestDismissPartyRoom();
                break;
            case 0x0b:
                msg = new RequestWithdrawPartyRoom();
                break;
            case 0x0c:
                msg = new RequestChangePartyLeader();
                break;
            case 0x0d:
                msg = new RequestAutoSoulShot();
                break;
            case 0x0e:
                msg = new RequestExEnchantSkillInfo();
                break;
            case 0x0f:
                msg = new RequestExEnchantSkill();
                break;
            case 0x10:
                msg = new RequestExPledgeCrestLarge();
                break;
            case 0x11:
                msg = new RequestExSetPledgeCrestLarge();
                break;
            case 0x12:
                msg = new RequestPledgeSetAcademyMaster();
                break;
            case 0x13:
                msg = new RequestPledgePowerGradeList();
                break;
            case 0x14:
                msg = new RequestPledgeMemberPowerInfo();
                break;
            case 0x15:
                msg = new RequestPledgeSetMemberPowerGrade();
                break;
            case 0x16:
                msg = new RequestPledgeMemberInfo();
                break;
            case 0x17:
                msg = new RequestPledgeWarList();
                break;
            case 0x18:
                msg = new RequestExFishRanking();
                break;
            case 0x19:
                msg = new RequestPCCafeCouponUse();
                break;
            case 0x1b:
                msg = new RequestDuelStart();
                break;
            case 0x1c:
                msg = new RequestDuelAnswerStart();
                break;
            case 0x1d:
                // RequestExSetTutorial
                break;
            case 0x1e:
                msg = new RequestExRqItemLink();
                break;
            case 0x1f:
                // CanNotMoveAnymoreAirShip
                break;
            case 0x20:
                msg = new MoveToLocationInAirShip();
                break;
            case 0x21:
                msg = new RequestKeyMapping();
                break;
            case 0x22:
                // RequestSaveKeyMapping
                break;
            case 0x23:
                msg = new RequestExRemoveItemAttribute();
                break;
            case 0x24:
                msg = new RequestSaveInventoryOrder();
                break;
            case 0x25:
                msg = new RequestExitPartyMatchingWaitingRoom();
                break;
            case 0x26:
                msg = new RequestConfirmTargetItem();
                break;
            case 0x27:
                msg = new RequestConfirmRefinerItem();
                break;
            case 0x28:
                msg = new RequestConfirmGemStone();
                break;
            case 0x29:
                msg = new RequestOlympiadObserverEnd();
                break;
            case 0x2a:
                msg = new RequestCursedWeaponList();
                break;
            case 0x2b:
                msg = new RequestCursedWeaponLocation();
                break;
            case 0x2c:
                msg = new RequestPledgeReorganizeMember();
                break;
            case 0x2d:
                msg = new RequestExMPCCShowPartyMembersInfo();
                break;
            case 0x2e:
                msg = new RequestOlympiadMatchList();
                break;
            case 0x2f:
                msg = new RequestAskJoinPartyRoom();
                break;
            case 0x30:
                msg = new AnswerJoinPartyRoom();
                break;
            case 0x31:
                msg = new RequestListPartyMatchingWaitingRoom();
                break;
            case 0x32:
                msg = new RequestExEnchantSkillSafe();
                break;
            case 0x33:
                msg = new RequestExEnchantSkillUntrain();
                break;
            case 0x34:
                msg = new RequestExEnchantSkillRouteChange();
                break;
            case 0x35:
                msg = new RequestExEnchantItemAttribute();
                break;
            case 0x36:
                msg = new ExGetOnAirShip();
                break;
            case 0x38:
                // MoveToLocationAirShip
                break;
            case 0x39:
                msg = new RequestBidItemAuction();
                break;
            case 0x3a:
                msg = new RequestInfoItemAuction();
                break;
            case 0x3b:
                msg = new RequestExChangeName();
                break;
            case 0x3c:
                msg = new RequestAllCastleInfo();
                break;
            case 0x3d:
                msg = new RequestAllFortressInfo();
                break;
            case 0x3e:
                msg = new RequestAllAgitInfo();
                break;
            case 0x3f:
                msg = new RequestFortressSiegeInfo();
                break;
            case 0x40:
                msg = new RequestGetBossRecord();
                break;
            case 0x41:
                msg = new RequestRefine();
                break;
            case 0x42:
                msg = new RequestConfirmCancelItem();
                break;
            case 0x43:
                msg = new RequestRefineCancel();
                break;
            case 0x44:
                msg = new RequestExMagicSkillUseGround();
                break;
            case 0x45:
                msg = new RequestDuelSurrender();
                break;
            case 0x46:
                msg = new RequestExEnchantSkillInfoDetail();
                break;
            case 0x48:
                msg = new RequestFortressMapInfo();
                break;
            case 0x49:
                msg = new RequestPVPMatchRecord();
                break;
            case 0x4a:
                msg = new SetPrivateStoreWholeMsg();
                break;
            case 0x4b:
                msg = new RequestDispel();
                break;
            case 0x4c:
                msg = new RequestExTryToPutEnchantTargetItem();
                break;
            case 0x4d:
                msg = new RequestExTryToPutEnchantSupportItem();
                break;
            case 0x4e:
                msg = new RequestExCancelEnchantItem();
                break;
            case 0x4f:
                msg = new RequestChangeNicknameColor();
                break;
            case 0x50:
                msg = new RequestResetNickname();
                break;
            case 0x51:
                int id3 = 0;
                if (buf.remaining() >= 4) {
                    id3 = buf.getInt() & 0xffffffff;
                } else {
                    if (Config.PACKET_HANDLER_DEBUG)
                        _log.warn("Client: " + client + " sent a 0xd0:0x51 without the third opcode.");
                    break;
                }

                switch (id3) {
                case 0x00:
                    msg = new RequestBookMarkSlotInfo();
                    break;
                case 0x01:
                    msg = new RequestSaveBookMarkSlot();
                    break;
                case 0x02:
                    msg = new RequestModifyBookMarkSlot();
                    break;
                case 0x03:
                    msg = new RequestDeleteBookMarkSlot();
                    break;
                case 0x04:
                    msg = new RequestTeleportBookMark();
                    break;
                case 0x05:
                    // RequestChangeBookMarkSlot
                    break;
                default:
                    printDebug(buf, client, opcode, id2, id3);
                    break;
                }
                break;
            case 0x52:
                msg = new RequestWithDrawPremiumItem();
                break;
            case 0x53:
                msg = new RequestJump();
                break;
            case 0x54:
                msg = new RequestStartShowCrataeCubeRank();
                break;
            case 0x55:
                msg = new RequestStopShowCrataeCubeRank();
                break;
            case 0x56:
                msg = new NotifyStartMiniGame();
                break;
            case 0x57:
                msg = new RequestJoinDominionWar();
                break;
            case 0x58:
                msg = new RequestDominionInfo();
                break;
            case 0x59:
                msg = new RequestExCleftEnter();
                break;
            case 0x5a:
                id3 = 0;
                if (buf.remaining() >= 4) {
                    id3 = buf.getInt() & 0xffffffff;
                } else {
                    _log.warn("Client: " + client + " sent a 0xd0:0x5a without the third opcode.");
                    break;
                }

                switch (id3) {
                case 0x00:
                    msg = new RequestExCubeGameChangeTeam();
                    break;
                default:
                    printDebug(buf, client, opcode, id2, id3);
                    break;
                }
                break;
            case 0x5b:
                msg = new EndScenePlayer();
                break;
            case 0x5c:
                msg = new RequestExBlockGameVote();
                break;
            case 0x63:
                msg = new RequestSeedPhase();
                break;
            case 0x65:
                msg = new BrGamePoint();
                break;
            case 0x66:
                msg = new BrProductList();
                break;
            case 0x67:
                msg = new BrProductInfo();
                break;
            case 0x68:
                msg = new BrBuyProduct();
                break;
            default:
                printDebug(buf, client, opcode, id2);
                break;
            }
            break;
        /*
        * case 0xee: msg = new RequestChangePartyLeader(data,
        * _client); break;
        */
        default:
            printDebug(buf, client, opcode);
            break;
        }
        break;
    }
    return msg;
}