Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

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

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:com.castis.sysComp.PoisConverterSysComp.java

public void parseRegionFile(File file) throws Exception {
    String line = "";
    FileInputStream in = null;// www . j a v a2  s  .com
    Reader isReader = null;
    LineNumberReader bufReader = null;

    FileOutputStream fos = null;
    String fileName = file.getName();

    int index = fileName.indexOf("-");
    if (index != -1) {
        fileName = fileName.substring(index + 1, fileName.length());
    }

    String dir = filePolling.getValidFileDirectory(resultDir);

    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);

    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        in = new FileInputStream(file);
        isReader = new InputStreamReader(in, "UTF-16LE");
        bufReader = new LineNumberReader(isReader);

        boolean first = true;
        while ((line = bufReader.readLine()) != null) {

            byte[] utf8 = line.getBytes("UTF-8");
            String string = new String(utf8, "UTF-8");

            String data[] = string.split("\t");

            if (first == true) {
                first = false;

                if (data[0] == null || data[0].contains("region") == false) {
                    throw new DataParsingException("data parsing error(not formatted)");
                }
                continue;
            }

            if (data[0] == null || data[0].equals("")) {
                throw new DataParsingException("data parsing error(region id)");
            }
            if (data[1] == null || data[1].equals("")) {
                throw new DataParsingException("data parsing error(region name)");
            }
            if (data[2] == null || data[2].equals("")) {
                throw new DataParsingException("data parsing error(parent id)");
            }

            StringBuffer strBuffer = new StringBuffer();
            strBuffer.append(data[0]);
            strBuffer.append("\t");
            strBuffer.append(data[1]);
            strBuffer.append("\t");
            strBuffer.append(data[2]);

            strBuffer.append("\r\n");

            byte[] outByte = null;
            try {
                outByte = strBuffer.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
            }
            byteBuffer.put(outByte);
            byteBuffer.flip();
            try {
                outByteCh.write(byteBuffer);
            } catch (IOException e) {
            }
            byteBuffer.clear();
        }

        fos.close();

        index = fileName.indexOf("_");

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);
        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/" + directory;
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line
                + "] : ";
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    } finally {
        if (in != null)
            in.close();
        if (isReader != null)
            isReader.close();
        if (bufReader != null)
            bufReader.close();
    }
}

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

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

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

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

    try {

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

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

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

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

        while (this.fileBuffer.hasRemaining()) {

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

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

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

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

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

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

                    frameByteCount = frameByteCount + 3;

                    this.state = 1;
                    break;

                } else {
                    break;

                }

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

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

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

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

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

                    } else {

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

                    }

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

                    frameCount++;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        currentFrameBuffer.clear();

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

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

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

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

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

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

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

                        currentFrameBuffer.clear();

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

                    }

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

                } else {

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

                }

            } // end switch()

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

        } // end while()

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

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

    }

}

From source file:com.castis.sysComp.PoisConverterSysComp.java

private void writeClientUIFile(List<sceneDTO> list, String platform, File file) throws FileNotFoundException {

    FileOutputStream fos = null;//from   ww  w .  j av  a 2  s.c  o  m
    String dir = filePolling.getValidFileDirectory(resultDir);

    String fileName = file.getName();
    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);
    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        for (int i = 0; i < list.size(); i++) {
            sceneDTO scene = list.get(i);
            StringBuffer strBuffer = new StringBuffer();
            if (i == 0) {
                strBuffer.append("policy");
                strBuffer.append("|");
                strBuffer.append(platform);
                strBuffer.append("|");
                strBuffer.append("ClientUI");

                strBuffer.append("\r\n");
            }
            strBuffer.append("info");
            strBuffer.append("|");
            strBuffer.append(platform);
            strBuffer.append("|");
            strBuffer.append(scene.getId());
            strBuffer.append("|");
            strBuffer.append(scene.getName());
            strBuffer.append("|");
            strBuffer.append(scene.getTemplateFileName());
            strBuffer.append("|");
            strBuffer.append(scene.getMenuId());
            strBuffer.append("|");
            strBuffer.append(scene.getMenuName());
            strBuffer.append("|");
            strBuffer.append(scene.getSpaceId());
            strBuffer.append("|");
            strBuffer.append(scene.getSpaceName());
            strBuffer.append("|");
            strBuffer.append(scene.getResolution());
            strBuffer.append("|");
            strBuffer.append(scene.getResolutionOnFocus());
            strBuffer.append("|");
            strBuffer.append(scene.getSizeLimit());
            strBuffer.append("|");
            strBuffer.append(scene.getClickable());

            strBuffer.append("\r\n");

            byte[] outByte = null;
            try {
                outByte = strBuffer.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
            }
            byteBuffer.put(outByte);
            byteBuffer.flip();
            try {
                outByteCh.write(byteBuffer);
            } catch (IOException e) {
            }
            byteBuffer.clear();
        }

        fos.close();

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);

        int index = fileName.indexOf("-");
        if (index != -1) {
            fileName = fileName.substring(index + 1, fileName.length());
        }
        index = fileName.indexOf("_");

        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/" + directory;
        }

        index = fileName.indexOf(".");
        if (index != -1) {
            fileName = fileName.substring(0, index) + ".csv";
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = e.getMessage();
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    }
}

From source file:edu.harvard.iq.dvn.core.web.ExploreDataPage.java

private void writeFile(File fileIn, char[] charArrayIn, int bufSize) {
    try {/*ww  w  .  j a v  a  2 s  . c  o m*/

        FileOutputStream outputFile = null;
        outputFile = new FileOutputStream(fileIn, true);
        FileChannel outChannel = outputFile.getChannel();
        ByteBuffer buf = ByteBuffer.allocate((bufSize * 2) + 1000);
        for (char ch : charArrayIn) {
            buf.putChar(ch);
        }

        buf.flip();

        try {
            outChannel.write(buf);
            outputFile.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }

    } catch (IOException e) {
        throw new EJBException(e);
    }

}

From source file:org.apache.hadoop.hdfs.hoss.db.FileStreamStore.java

/**
 * Read block from file/*from   w  w w.jav  a2s .c o m*/
 * 
 * @param offset
 *            of block
 * @param ByteBuffer
 * @return new offset (offset+headerlen+datalen+footer)
 */
public synchronized long read(long offset, final ByteBuffer buf) {
    if (!validState)
        throw new InvalidStateException();
    try {
        int readed;
        while (true) {
            if (offset >= offsetOutputCommited) {
                if (bufOutput.position() > 0) {
                    LOG.warn("WARN: autoflush forced");
                    flushBuffer();
                }
            }
            bufInput.clear();
            readed = fcInput.position(offset).read(bufInput); // Read 1
            // sector
            if (readed < HEADER_LEN) { // short+int (6 bytes)
                return -1;
            }
            bufInput.flip();
            final int magicB1 = (bufInput.get() & 0xFF); // Header - Magic
            // (short, 2 bytes, msb-first)
            final int magicB2 = (bufInput.get() & 0xFF); // Header - Magic
            // (short, 2 bytes, lsb-last)
            if (alignBlocks && (magicB1 == MAGIC_PADDING)) {
                final int diffOffset = nextBlockBoundary(offset);
                if (diffOffset > 0) {
                    offset += diffOffset;
                    continue;
                }
            }
            final int magic = ((magicB1 << 8) | magicB2);
            if (magic != MAGIC) {
                LOG.error("MAGIC HEADER fake=" + Integer.toHexString(magic) + " expected="
                        + Integer.toHexString(MAGIC));
                return -1;
            }
            break;
        }
        // Header - Data Size (int, 4 bytes)
        final int datalen = bufInput.getInt();
        final int dataUnderFlow = (datalen - (readed - HEADER_LEN));
        int footer = -12345678;
        if (dataUnderFlow < 0) {
            footer = bufInput.get(datalen + HEADER_LEN); // Footer (byte)
        }
        bufInput.limit(Math.min(readed, datalen + HEADER_LEN));
        buf.put(bufInput);
        if (dataUnderFlow > 0) {
            buf.limit(datalen);
            int len = fcInput.read(buf);
            if (len < dataUnderFlow) {
                LOG.error("Unable to read payload readed=" + len + " expected=" + dataUnderFlow);
                return -1;
            }
        }
        if (dataUnderFlow >= 0) {
            // Read Footer (byte)
            bufInput.clear();
            bufInput.limit(FOOTER_LEN);
            if (fcInput.read(bufInput) < FOOTER_LEN)
                return -1;
            bufInput.flip();
            footer = bufInput.get();
        }
        if (footer != MAGIC_FOOT) {
            LOG.error("MAGIC FOOT fake=" + Integer.toHexString(footer) + " expected="
                    + Integer.toHexString(MAGIC_FOOT));
            return -1;
        }
        buf.flip();
        return (offset + HEADER_LEN + datalen + FOOTER_LEN);
    } catch (Exception e) {
        LOG.error("Exception in read(" + offset + ")", e);
    }
    return -1;
}

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. j  a 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:com.healthmarketscience.jackcess.Column.java

/**
 * Serialize an Object into a raw byte value for this column
 * @param obj Object to serialize/*from  ww w. j av a2s . c om*/
 * @param order Order in which to serialize
 * @return A buffer containing the bytes
 * @usage _advanced_method_
 */
public ByteBuffer writeFixedLengthField(Object obj, ByteOrder order) throws IOException {
    int size = getType().getFixedSize(_columnLength);

    // create buffer for data
    ByteBuffer buffer = getPageChannel().createBuffer(size, order);

    // since booleans are not written by this method, it's safe to convert any
    // incoming boolean into an integer.
    obj = booleanToInteger(obj);

    switch (getType()) {
    case BOOLEAN:
        //Do nothing
        break;
    case BYTE:
        buffer.put(toNumber(obj).byteValue());
        break;
    case INT:
        buffer.putShort(toNumber(obj).shortValue());
        break;
    case LONG:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case MONEY:
        writeCurrencyValue(buffer, obj);
        break;
    case FLOAT:
        buffer.putFloat(toNumber(obj).floatValue());
        break;
    case DOUBLE:
        buffer.putDouble(toNumber(obj).doubleValue());
        break;
    case SHORT_DATE_TIME:
        writeDateValue(buffer, obj);
        break;
    case TEXT:
        // apparently text numeric values are also occasionally written as fixed
        // length...
        int numChars = getLengthInUnits();
        // force uncompressed encoding for fixed length text
        buffer.put(encodeTextValue(obj, numChars, numChars, true));
        break;
    case GUID:
        writeGUIDValue(buffer, obj, order);
        break;
    case NUMERIC:
        // yes, that's right, occasionally numeric values are written as fixed
        // length...
        writeNumericValue(buffer, obj);
        break;
    case BINARY:
    case UNKNOWN_0D:
    case UNKNOWN_11:
    case COMPLEX_TYPE:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case UNSUPPORTED_FIXEDLEN:
        byte[] bytes = toByteArray(obj);
        if (bytes.length != getLength()) {
            throw new IOException(
                    "Invalid fixed size binary data, size " + getLength() + ", got " + bytes.length);
        }
        buffer.put(bytes);
        break;
    default:
        throw new IOException("Unsupported data type: " + getType());
    }
    buffer.flip();
    return buffer;
}

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 .jav 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.umass.cs.gigapaxos.paxospackets.RequestPacket.java

/**
 * The weird constant above is to try to avoid mistakes in the painful (but
 * totally worth it) byte'ification method below. Using bytes as opposed to
 * json strings makes a non-trivial difference (~2x over json-smart and >4x
 * over org.json. So we just chuck json libraries and use our own byte[]
 * serializer for select packets./*from   w  ww.ja va  2s  .c om*/
 * 
 * The serialization overhead really matters most for RequestPacket and
 * AcceptPacket. Every request, even with batching, must be deserialized by
 * the coordinator and must be serialized back while sending out the
 * AcceptPacket. The critical path is the following at a coordinator and is
 * incurred at least in part even with batching for every request: (1)
 * receive request, (2) send accept, (3) receive accept_replies, (4) send
 * commit Accordingly, we use byteification for {@link RequestPacket},
 * {@link AcceptPacket}, {@link BatchedAcceptReply} and
 * {@link BatchedCommit}.
 * 
 * */

protected byte[] toBytes(boolean instrument) {
    // return cached value if already present
    if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
            && this.byteifiedSelf != null && !instrument)
        return this.byteifiedSelf;
    // check if we can use byteification at all; if not, use toString()
    if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) {
        try {
            if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
                return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache
            return this.toString().getBytes(CHARSET);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
            return null;
        }
    }

    // else byteify
    try {
        int exactLength = 0;
        byte[] array = new byte[this.lengthEstimate()];
        ByteBuffer bbuf = ByteBuffer.wrap(array);
        assert (bbuf.position() == 0);

        // paxospacket stuff
        super.toBytes(bbuf);
        int ppPos = bbuf.position(); // for assertion
        assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != "
                        + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                        + SIZEOF_PAXOSPACKET_FIXED;
        exactLength += (bbuf.position());

        bbuf.putLong(this.requestID);
        bbuf.put(this.stop ? (byte) 1 : (byte) 0);
        exactLength += (Long.BYTES + 1);

        // addresses
        /* Note: 0 is ambiguous with wildcard address, but that's okay
         * because an incoming packet will never come with a wildcard
         * address. */
        bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0);
        /* Note: 0 is an ambiguous wildcard address that could also be a
         * legitimate value of the listening socket address. If the request
         * happens to have no listening address, we will end up assuming it
         * was received on the wildcard address. At worst, the matching for
         * the corresponding response back to the client can fail. */
        bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0);
        exactLength += 2 * (Integer.BYTES + Short.BYTES);

        // other non-final fields
        bbuf.putInt(this.entryReplica);
        bbuf.putLong(this.entryTime);
        bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0);
        bbuf.putInt(this.forwardCount);
        exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES);

        // digest related fields: broadcasted, digest
        // whether this request was already broadcasted
        bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0);
        exactLength += 1;
        assert (exactLength ==
        // where parent left us off
        ppPos + SIZEOF_REQUEST_FIXED
        // for the three int fields not yet filled
                - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - "
                        + 4 * Integer.BYTES + "]";
        // digest length and digest iteself
        bbuf.putInt(this.digest != null ? this.digest.length : 0);
        exactLength += Integer.BYTES;
        if (this.digest != null)
            bbuf.put(this.digest);
        exactLength += (this.digest != null ? this.digest.length : 0);
        // /////////// end of digest related fields //////////

        // highly variable length fields
        // requestValue
        byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0);
        bbuf.put(reqValBytes);
        exactLength += (4 + reqValBytes.length);

        // responseValue
        byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(respValBytes != null ? respValBytes.length : 0);
        bbuf.put(respValBytes);
        exactLength += (4 + respValBytes.length);

        // batched requests batchSize|(length:batchedReqBytes)+
        bbuf.putInt(this.batchSize());
        exactLength += (4);
        if (this.batchSize() > 0)
            for (RequestPacket req : this.batched) {
                byte[] element = req.toBytes();
                bbuf.putInt(element.length);
                bbuf.put(element);
                exactLength += (4 + element.length);
            }

        // bbuf.array() was a generous allocation
        byte[] exactBytes = new byte[exactLength];
        bbuf.flip();
        assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength;
        bbuf.get(exactBytes);

        if (this.getType() == PaxosPacketType.REQUEST)
            this.byteifiedSelf = exactBytes;

        return exactBytes;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}