Example usage for java.nio BufferUnderflowException getMessage

List of usage examples for java.nio BufferUnderflowException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:edu.tsinghua.lumaqq.qq.packets.Packet.java

/**
 * bufOutPacketbuf?????//from w w  w . ja v  a2 s . c  om
 * 
 * @param buf
 *          ByteBuffer
 * @param length
 *          ??
 * @throws PacketParseException
 *          ?
 */
protected Packet(ByteBuffer buf, int length, QQUser user) throws PacketParseException {
    this.user = user;
    // ?
    parseHeader(buf);
    // QQ?
    if (!validateHeader())
        throw new PacketParseException(": " + toString());
    // 
    byte[] body = getBodyBytes(buf, length);
    bodyDecrypted = decryptBody(body);
    if (bodyDecrypted == null)
        throw new PacketParseException("?: " + toString());
    // ByteBuffer
    ByteBuffer tempBuf = ByteBuffer.wrap(bodyDecrypted);
    try {
        // ?
        parseBody(tempBuf);
    } catch (BufferUnderflowException e) {
        throw new PacketParseException(e.getMessage());
    }
    parseTail(buf);
    // ???
    if (ds.isDebug()) {
        byte[] debugContent = dump();
        IDebugObject obj = new PacketDO(getPacketName(), debugContent, this instanceof InPacket,
                getHeadLength(), debugContent.length - getTailLength());
        ds.deliverDebugObject(obj);
    }
}

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

/**
 * Parses the binary STOR-X timestamp. The timestamp format is
 * YYYYDDD from the first 3 bytes, and HHMMSS.SSS from the last four:
 * Example:/*from w w  w. ja  va2 s. co m*/
 * 1E AC CC = 2010316 (year 2010, julian day 316)
 * 09 9D 3E 20 = 16:13:00.000 (4:13 pm)
 * @param timestamp - the timestamp to parse as a byte array
 * @return date - the timestamp as a Date object
 */
public Date parseTimestamp(byte[] timestamp) {

    Date convertedDate = new Date(0L); // initialize to the epoch

    try {
        ByteBuffer timestampBuffer = ByteBuffer.wrap(timestamp);

        // convert the year and day bytes
        int yearAndJulianDay = ((timestampBuffer.get() & 0xFF) << 16) | ((timestampBuffer.get() & 0xFF) << 8)
                | ((timestampBuffer.get() & 0xFF));

        String yearAndJulianDayString = new Integer(yearAndJulianDay).toString();

        // convert the hour, minute, second, millis bytes
        int hourMinuteSecondMillis = timestampBuffer.getInt();
        String hourMinuteSecondMillisString = String.format("%09d", hourMinuteSecondMillis);

        // concatenate the strings to get the timestamp
        String timestampString = yearAndJulianDayString + hourMinuteSecondMillisString;

        // convert to a Date object
        FRAME_DATE_FORMAT.setTimeZone(TZ);
        convertedDate = FRAME_DATE_FORMAT.parse(timestampString, new ParsePosition(0));

    } catch (BufferUnderflowException bue) {

        logger.debug(
                "There was a problem reading the timestamp. " + "The error message was: " + bue.getMessage());

    } catch (NullPointerException npe) {

        logger.debug("There was a problem converting the timestamp. " + "The error message was: "
                + npe.getMessage());

    } finally {

        return convertedDate;

    }

}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnNewJavaFieldCutter.java

public void cutColumns(InputStream in, int noCardsPerCase, int caseLength, String delimitor, String tabFileName)
        throws IOException {

    if (delimitor == null) {
        delimitor = defaultDelimitor;/*w  ww  .ja  v  a2 s  .co  m*/
    }

    OUT_LEN = colwidth; // calculated by parseList
    dbgLog.fine("out_len=" + OUT_LEN);

    String firstline = null;

    if (caseLength == 0) {

        int cread;
        int ccounter = 0;

        firstline = "";

        while (caseLength == 0 && (cread = in.read()) != -1) {
            ccounter++;
            if (cread == '\n') {
                caseLength = ccounter;
            }
            char c = (char) cread;
            firstline = firstline + c;
        }

    }

    if (caseLength == 0) {
        throw new IOException("Subsetting failed: could not read incoming byte stream. "
                + "(Requested file may be unavailable or missing)");

    }

    REC_LEN = caseLength;
    dbgLog.fine("REC_LEN=" + REC_LEN);

    for (int i = 0; i < cargSet.get(Long.valueOf(noCardsPerCase)).size(); i++) {
        int varEndOffset = cargSet.get(Long.valueOf(noCardsPerCase)).get(i).get(1);

        if (REC_LEN <= varEndOffset + 1) {
            throw new IOException("Failed to subset incoming byte stream. Invalid input. "
                    + "(Detected the first record of " + REC_LEN + " bytes; "
                    + "one of the columns requested ends at " + varEndOffset + " bytes).");
        }
    }

    Boolean dottednotation = false;
    Boolean foundData = false;

    // cutting a data file

    ReadableByteChannel rbc = Channels.newChannel(in);
    // input byte-buffer size = row-length + 1(=> new line char)
    ByteBuffer inbuffer = ByteBuffer.allocate(REC_LEN);

    OutputStream outs = new FileOutputStream(tabFileName);
    WritableByteChannel outc = Channels.newChannel(outs);
    ByteBuffer outbuffer = null;

    int pos = 0;
    int offset = 0;
    int outoffset = 0;

    int begin = 0;
    int end = 0;
    int blankoffset = 0;

    int blanktail = 0;
    int k;

    try {
        // lc: line counter
        int lc = 0;
        while (firstline != null || rbc.read(inbuffer) != -1) {

            if (firstline != null) {
                // we have the first line saved as a String:
                inbuffer.put(firstline.getBytes());
                firstline = null;
            }

            // calculate i-th card number
            lc++;
            k = lc % noCardsPerCase;
            if (k == 0) {
                k = noCardsPerCase;
            }
            //out.println("***** " +lc+ "-th line, recod k=" + k + " *****");
            byte[] line_read = new byte[OUT_LEN];
            byte[] junk = new byte[REC_LEN];
            byte[] line_final = new byte[OUT_LEN];

            //out.println("READ: " + offset);
            inbuffer.rewind();

            offset = 0;
            outoffset = 0;

            // how many variables are cut from this k-th card
            int noColumns = cargSet.get(Long.valueOf(k)).size();

            //out.println("noColumns=" + noColumns);
            //out.println("cargSet k =" + cargSet.get(Long.valueOf(k)));

            for (int i = 0; i < noColumns; i++) {
                //out.println("**** " + i +"-th col ****");
                begin = cargSet.get(Long.valueOf(k)).get(i).get(0); // bounds[2 * i];
                end = cargSet.get(Long.valueOf(k)).get(i).get(1); // bounds[2 * i + 1];

                //out.println("i: begin: " + begin + "\ti: end:" + end);

                try {
                    // throw away offect bytes
                    if (begin - offset - 1 > 0) {
                        inbuffer.get(junk, 0, (begin - offset - 1));
                    }
                    // get requested bytes
                    inbuffer.get(line_read, outoffset, (end - begin + 1));
                    // set outbound data
                    outbounds[2 * i] = outoffset;
                    outbounds[2 * i + 1] = outoffset + (end - begin);
                    // current position moved to outoffset
                    pos = outoffset;

                    dottednotation = false;
                    foundData = false;

                    blankoffset = 0;
                    blanktail = 0;

                    // as position increases
                    while (pos <= (outoffset + (end - begin))) {

                        //out.println("pos=" + pos + "\tline_read[pos]=" +
                        //    new String(line_read).replace("\000", "\052"));

                        // decimal octal
                        // 48 =>0 60
                        // 46 => . 56
                        // 32 = space 40

                        // dot: 
                        if (line_read[pos] == '\056') {
                            dottednotation = true;
                        }

                        // space:
                        if (line_read[pos] == '\040') {
                            if (foundData) {
                                blanktail = blanktail > 0 ? blanktail : pos - 1;
                            } else {
                                blankoffset = pos + 1;
                            }
                        } else {
                            foundData = true;
                            blanktail = 0;
                        }

                        pos++;
                    }
                    // increase the outoffset by width
                    outoffset += (end - begin + 1);
                    // dot false
                    if (!dottednotation) {
                        if (blankoffset > 0) {
                            // set outbound value to blankoffset
                            outbounds[2 * i] = blankoffset;
                        }
                        if (blanktail > 0) {
                            outbounds[2 * i + 1] = blanktail;
                        }
                    }

                } catch (BufferUnderflowException bufe) {
                    //bufe.printStackTrace();
                    throw new IOException(bufe.getMessage());
                }
                // set offset to the value of end-position
                offset = end;
            }

            outoffset = 0;
            // for each var
            for (int i = 0; i < noColumns; i++) {
                begin = outbounds[2 * i];
                end = outbounds[2 * i + 1];
                //out.println("begin=" + begin + "\t end=" + end);
                for (int j = begin; j <= end; j++) {
                    line_final[outoffset++] = line_read[j];
                }

                if (i < (noColumns - 1)) {
                    line_final[outoffset++] = '\011'; // tab x09
                } else {
                    if (k == cargSet.size()) {
                        line_final[outoffset++] = '\012'; // LF x0A
                    } else {
                        line_final[outoffset++] = '\011'; // tab x09
                    }
                }
            }
            //out.println("line_final=" +
            //    new String(line_final).replace("\000", "\052"));
            outbuffer = ByteBuffer.wrap(line_final, 0, outoffset);
            outc.write(outbuffer);
            inbuffer.clear();

        } // while loop
    } catch (IOException ex) {
        //ex.printStackTrace();
        throw new IOException("Failed to subset incoming fixed-field stream: " + ex.getMessage());
    }

}