Example usage for java.nio BufferOverflowException getMessage

List of usage examples for java.nio BufferOverflowException getMessage

Introduction

In this page you can find the example usage for java.nio BufferOverflowException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:edu.hawaii.soest.kilonalu.utilities.SerialChannel.java

/**
 * A method used to read bytes from the serial port
 *
 * @param readBuffer - the ByteBuffer used to store the bytes read
 * @return count - the number of bytes read as an integer
 *//*from  w ww .j  a va  2s .co  m*/
public int read(ByteBuffer readBuffer) throws IOException {

    //byte[] buffer = new byte[8192];
    StringBuffer buffer = new StringBuffer();
    int count = 0;
    String line = null;

    try {
        line = this.serialReader.readLine();

        // only append the line read if it is not null, has a length
        // greater than zero, and isn't just a single null character.
        if (line != null && line.length() > 0 && !line.equals(new String(new byte[] { 0x00 }))) {
            buffer.append(line);
            buffer.append("\r\n"); //add termination bytes back into the line

            // filter null characters out of the string before putting the
            // bytes into the ByteBuffer.
            byte[] lineAsBytes = buffer.toString().getBytes();
            for (int i = 0; i < lineAsBytes.length; i++) {
                if (lineAsBytes[i] != 0x00) {
                    readBuffer.put(lineAsBytes[i]);
                    count++;
                }
            }

        } else {
            count = 0;

        }

    } catch (BufferOverflowException bofe) {
        logger.info("There a problem reading from the serial port: " + bofe.getMessage());
        this.serialPort.close();
        throw new IOException(bofe.getMessage());

    } catch (ReadOnlyBufferException robe) {
        logger.info("There a problem reading from the serial port: " + robe.getMessage());
        this.serialPort.close();
        throw new IOException(robe.getMessage());

    }
    return count;

}