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:uk.co.modularaudio.util.audio.buffer.LocklessPreallocatingGenericRingBuffer.java

public void readOneCopyToDest(A dest) throws BufferUnderflowException {
    boolean success = false;
    while (!success) {
        int curReadPosition = readPosition.get();
        int curWritePosition = writePosition.get();
        int numReadable = calcNumReadable(curReadPosition, curWritePosition);
        int newPosition = curReadPosition;

        if (numReadable < 1) {
            throw new BufferUnderflowException();
        } else {/*from ww  w.  j a  v a 2  s. com*/
            copier.copyValues(buffer[curReadPosition], dest);
        }

        newPosition += 1;

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

From source file:uk.co.modularaudio.util.audio.buffer.UnsafePreallocatingGenericRingBuffer.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 numReadable = calcNumReadable(readPosition, writePosition);
        int newPosition;

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

        if (newPosition >= bufferLength) {
            newPosition -= bufferLength;
        }
        readPosition = newPosition;
    }
    return length;
}

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

public int readUpToMaxNum(A target[], int pos, int maxNum) throws BufferUnderflowException {
    boolean success = false;
    int numRead = 0;
    while (!success) {
        int numReadable = calcNumReadable(readPosition, writePosition);
        if (numReadable == 0) {
            return 0;
        }//  w w w. ja va 2s  .c om
        int newPosition = readPosition;

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

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

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

        newPosition += length;

        if (newPosition >= bufferLength) {
            newPosition -= bufferLength;
        }
        readPosition = newPosition;
    }
    return numRead;
}

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

public void readOneCopyToDest(A dest) throws BufferUnderflowException {
    boolean success = false;
    while (!success) {
        int numReadable = calcNumReadable(readPosition, writePosition);
        int newPosition = readPosition;

        if (numReadable < 1) {
            throw new BufferUnderflowException();
        } else {//w w  w  .j a  va  2s  . com
            copier.copyValues(buffer[readPosition], dest);
        }

        newPosition += 1;

        if (newPosition >= bufferLength) {
            newPosition -= bufferLength;
        }
        readPosition = newPosition;
    }
}

From source file:uk.co.modularaudio.util.audio.mad.hardwareio.LocklessHardwareMidiNoteRingBuffer.java

public int readUpToMaxNumAndFrameTime(final HardwareMidiNoteEvent[] target, final int pos, final int maxNum,
        final long endFrameTime) {
    boolean success = false;
    int numRead = 0;
    while (!success) {
        final int curReadPosition = readPosition.get();
        final int curWritePosition = writePosition.get();
        final int numReadable = calcNumReadable(curReadPosition, curWritePosition);
        if (numReadable == 0) {
            return 0;
        }//from  w ww. jav a2 s  .c o m
        int length = (numReadable < maxNum ? numReadable : maxNum);

        // Now check the timestamp to work out how many we will actually read out
        boolean done = false;
        int numToRead = 0;
        for (int c = 0; !done && c < length; c++) {
            int posToCheck = curReadPosition + c;
            if (posToCheck > (bufferLength - 1)) {
                posToCheck -= bufferLength;
            }
            ;
            final long bufferEventFrameTime = buffer[posToCheck].eventFrameTime;
            if (bufferEventFrameTime > endFrameTime) {
                if (c == 0) {
                    return 0;
                } else {
                    done = true;
                }
            } else {
                numToRead++;
            }
        }

        if (numToRead > 0) {
            length = numToRead;
        }

        if (curWritePosition > curReadPosition) {
            // Copy from the ring buffer directly into the output and update the read position
            copyFromToLength(buffer, curReadPosition, target, pos, length);
            numRead = length;
        } else if (curReadPosition > curWritePosition) {
            // Case where the read position might loop over the end of the buffer
            if (curReadPosition + length > bufferLength) {
                final int numToReadFromEnd = bufferLength - curReadPosition;
                final 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();
        }

        int newPosition = curReadPosition + length;

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