Example usage for java.nio BufferUnderflowException BufferUnderflowException

List of usage examples for java.nio BufferUnderflowException BufferUnderflowException

Introduction

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

Prototype

public BufferUnderflowException() 

Source Link

Document

Constructs a BufferUnderflowException.

Usage

From source file:Main.java

/**
 * Relative <em>get</em> method for reading {@code size} number of bytes from the current
 * position of this buffer./*from   w  w w.  java2  s  .com*/
 * <p>
 * <p>This method reads the next {@code size} bytes at this buffer's current position,
 * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
 * {@code size}, byte order set to this buffer's byte order; and then increments the position by
 * {@code size}.
 */
private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
        throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    final int originalLimit = source.limit();
    final int position = source.position();
    final int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        final ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}

From source file:com.slytechs.capture.file.editor.AbstractRawIterator.java

/**
 * Gets the buffer at current position, does not advance and does not apply
 * the recordFilter.//w  ww  .  j av a  2  s .  c  o  m
 * 
 * @param headerReader
 * @return
 * @throws IOException
 */
public final ByteBuffer getNoFilter(final HeaderReader lengthGetter) throws IOException {

    final long regional = this.segment.mapGlobalToRegional(this.global);
    final int min = lengthGetter.getMinLength();

    if (this.blockBuffer.checkBoundsRegional(regional, min) == false) {
        this.blockBuffer = this.loader.fetchBlock(regional, min);
    } else {
        this.blockBuffer.reposition(regional, min);
    }

    final ByteBuffer buffer = this.blockBuffer.getByteBuffer();

    final int length = (int) this.getRecordLength(buffer, lengthGetter);
    final int allocation = this.loader.getBufferAllocation(length);

    if (this.blockBuffer.checkBoundsRegional(regional, length) == false) {

        if (length > allocation) {
            AbstractRawIterator.logger
                    .error("Record's length (" + length + ") is greater then prefetch buffer size ("
                            + allocation + ") at record position (" + this.global + ")");
            throw new BufferUnderflowException();
        }

        this.blockBuffer = this.loader.fetchBlock(regional, length);
    }

    try {
        this.blockBuffer.reposition(regional, length);
    } catch (final IllegalArgumentException e) {
        AbstractRawIterator.logger.error("Unable to set limit and position ByteBuffer properties "
                + "at position (" + this.global + "). Record's length (" + length + ").");
        throw e;
    }

    return buffer;
}

From source file:com.slytechs.capture.file.editor.AbstractRawIterator.java

private long getRecordLength(final ByteBuffer buffer, final HeaderReader lengthGetter) throws IOException {

    final long length = lengthGetter.readLength(buffer);

    if (length < 0) {
        AbstractRawIterator.logger/*w w  w.  j  a  v a2 s.  c o  m*/
                .error("Invalid record length value (" + length + ") at record position (" + this.global + ")");
        throw new BufferUnderflowException();
    }

    return length;
}

From source file:com.slytechs.capture.file.editor.AbstractRawIterator.java

private long getRecordLength(final long regional, final HeaderReader headerReader) throws IOException {

    final PartialBuffer bblock;
    try {// w ww . j a  va 2s  . c o m
        bblock = this.loader.fetchBlock(regional, headerReader.getMinLength());

    } catch (BufferFetchException e) {
        e.setFlexRegion(edits);
        e.setMessage("Unable to read length from header");
        e.setHeaderReader(headerReader);
        throw e;
    }

    final ByteBuffer buffer = bblock.getByteBuffer();
    final long length = headerReader.readLength(buffer);

    if ((length < 0) || (length > 100000)) {
        AbstractRawIterator.logger
                .error("Invalid record length value (" + length + ") at record position (" + this.global + ")");
        throw new BufferUnderflowException();
    }

    return length;
}

From source file:com.slytechs.capture.file.editor.AbstractRawIterator.java

/**
 * Does all the work and does not apply the recordFilter.
 * //from ww  w. j a va2  s  . com
 * @param headerReader
 * @return
 * @throws IOException
 */
private ByteBuffer nextNoFilter(final HeaderReader lengthGetter) throws IOException {

    final long regional = this.segment.mapGlobalToRegional(this.global);
    final int min = lengthGetter.getMinLength();

    if (this.blockBuffer.checkBoundsRegional(regional, min) == false) {
        this.blockBuffer = this.loader.fetchBlock(regional, min);
    } else {
        this.blockBuffer.reposition(regional, min);
    }

    final ByteBuffer buffer = this.blockBuffer.getByteBuffer();

    final int length = (int) this.getRecordLength(buffer, lengthGetter);
    final int allocation = this.loader.getBufferAllocation(length);

    if (this.blockBuffer.checkBoundsRegional(regional, length) == false) {

        if (length > SEARCH_LENGTH) {
            AbstractRawIterator.logger
                    .warn("Record's length (" + length + ") is greater then prefetch buffer size (" + allocation
                            + ") at record position (" + this.global + ")");
            if (pattern.match(buffer)) {
                logger.info("Erroneous record passes the search pattern test");
            }
            throw new BufferUnderflowException();
        }

        this.blockBuffer = this.loader.fetchBlock(regional, length);
    }

    try {
        this.blockBuffer.reposition(regional, length);
    } catch (final IllegalArgumentException e) {
        AbstractRawIterator.logger.error("Unable to set limit and position ByteBuffer properties "
                + "at position (" + this.global + "). Record's length (" + length + ").");
        throw e;
    }

    this.previousPosition = this.global;
    this.setPosition(this.global + length);

    return buffer;
}

From source file:ome.io.nio.RomioPixelBuffer.java

/**
 * Implemented as specified by {@link PixelBuffer} I/F.
 * @see PixelBuffer#setPlane(ByteBuffer, Integer, Integer, Integer)
*///from  w  ww .  ja va2 s .co m
public void setPlane(ByteBuffer buffer, Integer z, Integer c, Integer t)
        throws IOException, DimensionsOutOfBoundsException {
    throwIfReadOnly();
    Long offset = getPlaneOffset(z, c, t);
    Integer size = getPlaneSize();
    if (buffer.limit() != size) {
        // Handle the size mismatch.
        if (buffer.limit() < size)
            throw new BufferUnderflowException();
        throw new BufferOverflowException();
    }

    setRegion(size, offset, buffer);
}

From source file:ome.io.nio.RomioPixelBuffer.java

/**
 * Implemented as specified by {@link PixelBuffer} I/F.
 * @see PixelBuffer#setStack(ByteBuffer, Integer, Integer, Integer)
*///from w w w. j a v  a 2s .  co  m
public void setStack(ByteBuffer buffer, Integer z, Integer c, Integer t)
        throws IOException, DimensionsOutOfBoundsException {
    throwIfReadOnly();
    Long offset = getStackOffset(c, t);
    Integer size = getStackSize();
    if (buffer.limit() != size) {
        // Handle the size mismatch.
        if (buffer.limit() < size)
            throw new BufferUnderflowException();
        throw new BufferOverflowException();
    }

    setRegion(size, offset, buffer);
}

From source file:ome.io.nio.RomioPixelBuffer.java

/**
 * Implemented as specified by {@link PixelBuffer} I/F.
 * @see PixelBuffer#setTimepoint(ByteBuffer, Integer)
*/// w w w. j av a  2s.c o  m
public void setTimepoint(ByteBuffer buffer, Integer t) throws IOException, DimensionsOutOfBoundsException {
    throwIfReadOnly();
    Long offset = getTimepointOffset(t);
    Integer size = getTimepointSize();
    if (buffer.limit() != size) {
        // Handle the size mismatch.
        if (buffer.limit() < size)
            throw new BufferUnderflowException();
        throw new BufferOverflowException();
    }

    setRegion(size, offset, buffer);
}

From source file:uk.co.modularaudio.util.audio.buffer.LocklessPreallocatingGenericRingBuffer.java

public int read(A target[], int pos, int length) {
    boolean success = false;
    // Keep trying until we are the one to successfully update
    // the read position with atomic CAS
    while (!success) {
        int curReadPosition = readPosition.get();
        int curWritePosition = writePosition.get();
        int numReadable = calcNumReadable(curReadPosition, curWritePosition);
        int newPosition = curReadPosition;

        if (numReadable < length) {
            return 0;
        } else {/* w  ww .j a  v a  2  s. c  o  m*/
            // Treat the cases
            if (curWritePosition > curReadPosition) {
                // Copy from the ring buffer directly into the output and update the read position
                copyFromToLength(buffer, curReadPosition, target, pos, length);
            } else if (curReadPosition > curWritePosition) {
                // Case where the read position might loop over the end of the buffer
                if (curReadPosition + length > bufferLength) {
                    int numToReadFromEnd = bufferLength - curReadPosition;
                    int numToReadFromStart = length - numToReadFromEnd;
                    copyFromToLength(buffer, curReadPosition, target, pos, numToReadFromEnd);
                    copyFromToLength(buffer, 0, target, pos + numToReadFromEnd, numToReadFromStart);
                } else {
                    // Fits before the end of the ring
                    copyFromToLength(buffer, curReadPosition, target, pos, length);
                }
            } else {
                log.error("Case analysis error in ring read");
                throw new BufferUnderflowException();
            }
        }

        newPosition += length;

        if (newPosition >= bufferLength) {
            newPosition -= bufferLength;
        }
        success = readPosition.compareAndSet(curReadPosition, newPosition);
    }
    return length;
}

From source file:uk.co.modularaudio.util.audio.buffer.LocklessPreallocatingGenericRingBuffer.java

public int readUpToMaxNum(A target[], int pos, int maxNum) throws BufferUnderflowException {
    boolean success = false;
    int numRead = 0;
    while (!success) {
        int curReadPosition = readPosition.get();
        int curWritePosition = writePosition.get();
        int numReadable = calcNumReadable(curReadPosition, curWritePosition);
        if (numReadable == 0) {
            return 0;
        }//from  w  w  w  . j a v  a2s  .c o  m
        int newPosition = curReadPosition;

        int length = (numReadable < maxNum ? numReadable : maxNum);

        if (curWritePosition > curReadPosition) {
            // Copy from the ring buffer directly into the output and update the read position
            copyFromToLength(buffer, curReadPosition, target, pos, length);
        } else if (curReadPosition > curWritePosition) {
            // Case where the read position might loop over the end of the buffer
            if (curReadPosition + length > bufferLength) {
                int numToReadFromEnd = bufferLength - curReadPosition;
                int numToReadFromStart = length - numToReadFromEnd;
                copyFromToLength(buffer, curReadPosition, target, pos, numToReadFromEnd);
                copyFromToLength(buffer, 0, target, pos + numToReadFromEnd, numToReadFromStart);
            } else {
                // Fits before the end of the ring
                copyFromToLength(buffer, curReadPosition, target, pos, length);
            }

            numRead = length;
        } else {
            log.error("Case analysis error in ring read");
            throw new BufferUnderflowException();
        }

        newPosition += length;

        if (newPosition >= bufferLength) {
            newPosition -= bufferLength;
        }
        success = readPosition.compareAndSet(curReadPosition, newPosition);
    }
    return numRead;
}