org.apache.commons.collections.BufferOverflowException Java Examples

The following examples show how to use org.apache.commons.collections.BufferOverflowException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoundedFifoBuffer.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the given element to this buffer.
 *
 * @param element  the element to add
 * @return true, always
 * @throws NullPointerException  if the given element is null
 * @throws BufferOverflowException  if this buffer is full
 */
public boolean add(Object element) {
    if (null == element) {
        throw new NullPointerException("Attempted to add null object to buffer");
    }

    if (full) {
        throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects.");
    }

    elements[end++] = element;

    if (end >= maxElements) {
        end = 0;
    }

    if (end == start) {
        full = true;
    }

    return true;
}
 
Example #2
Source File: CircularByteBuffer.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write a portion of an array of bytes.
 * If the buffer allows blocking writes, this method will block until
 * all the data has been written rather than throw an IOException.
 *
 * @param cbuf Array of bytes
 * @param off Offset from which to start writing bytes
 * @param len - Number of bytes to write
 * @throws BufferOverflowException if buffer does not allow blocking writes
 *   and the buffer is full.  If the exception is thrown, no data
 *   will have been written since the buffer was set to be non-blocking.
 * @throws IOException if the stream is closed, or the write is interrupted.
 *
 * @since ostermillerutils 1.00.00
 */
@Override public void write(@NotNull byte[] cbuf, int off, int len) throws IOException {
	while (len > 0){
		synchronized (CircularByteBuffer.this){
			if (outputStreamClosed) throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream.");
			if (inputStreamClosed) throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer.");
			int spaceLeft = spaceLeft();
			while (infinite && spaceLeft < len){
				resize();
				spaceLeft = spaceLeft();
			}
			if (!blockingWrite && spaceLeft < len) throw new BufferOverflowException("CircularByteBuffer is full; cannot write " + len + " bytes");
			int realLen = Math.min(len, spaceLeft);
			int firstLen = Math.min(realLen, buffer.length - writePosition);
			int secondLen = Math.min(realLen - firstLen, buffer.length - markPosition - 1);
			int written = firstLen + secondLen;
			if (firstLen > 0){
				System.arraycopy(cbuf, off, buffer, writePosition, firstLen);
			}
			if (secondLen > 0){
				System.arraycopy(cbuf, off+firstLen, buffer, 0, secondLen);
				writePosition = secondLen;
			} else {
				writePosition += written;
			}
			if (writePosition == buffer.length) {
				writePosition = 0;
			}
			off += written;
			len -= written;
		}
		if (len > 0){
			try {
				Thread.sleep(100);
			} catch(Exception x){
				throw new IOException("Waiting for available space in buffer interrupted.");
			}
		}
	}
}
 
Example #3
Source File: CircularByteBuffer.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write a single byte.
 * The byte to be written is contained in the 8 low-order bits of the
 * given integer value; the 24 high-order bits are ignored.
 * If the buffer allows blocking writes, this method will block until
 * all the data has been written rather than throw an IOException.
 *
 * @param c number of bytes to be written
 * @throws BufferOverflowException if buffer does not allow blocking writes
 *   and the buffer is full.
 * @throws IOException if the stream is closed, or the write is interrupted.
 *
 * @since ostermillerutils 1.00.00
 */
@Override public void write(int c) throws IOException {
	boolean written = false;
	while (!written){
		synchronized (CircularByteBuffer.this){
			if (outputStreamClosed) throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream.");
			if (inputStreamClosed) throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer.");
			int spaceLeft = spaceLeft();
			while (infinite && spaceLeft < 1){
				resize();
				spaceLeft = spaceLeft();
			}
			if (!blockingWrite && spaceLeft < 1) throw new BufferOverflowException("CircularByteBuffer is full; cannot write 1 byte");
			if (spaceLeft > 0){
				buffer[writePosition] = (byte)(c & 0xff);
				writePosition++;
				if (writePosition == buffer.length) {
					writePosition = 0;
				}
				written = true;
			}
		}
		if (!written){
			try {
				Thread.sleep(100);
			} catch(Exception x){
				throw new IOException("Waiting for available space in buffer interrupted.");
			}
		}
	}
}
 
Example #4
Source File: CircularByteBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write a single byte.
 * The byte to be written is contained in the 8 low-order bits of the
 * given integer value; the 24 high-order bits are ignored.
 * If the buffer allows blocking writes, this method will block until
 * all the data has been written rather than throw an IOException.
 *
 * @param c
 *            number of bytes to be written
 * @throws BufferOverflowException
 *             if buffer does not allow blocking writes
 *             and the buffer is full.
 * @throws IOException
 *             if the stream is closed, or the write is interrupted.
 *
 * @since ostermillerutils 1.00.00
 */
@Override
public void write(int c) throws IOException {
    boolean written = false;
    while (!written) {
	synchronized (CircularByteBuffer.this) {
	    if (outputStreamClosed) {
		throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream.");
	    }
	    if (inputStreamClosed) {
		throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer.");
	    }
	    int spaceLeft = spaceLeft();
	    while (infinite && spaceLeft < 1) {
		resize();
		spaceLeft = spaceLeft();
	    }
	    if (!blockingWrite && spaceLeft < 1) {
		throw new BufferOverflowException("CircularByteBuffer is full; cannot write 1 byte");
	    }
	    if (spaceLeft > 0) {
		buffer[writePosition] = (byte) (c & 0xff);
		writePosition++;
		if (writePosition == buffer.length) {
		    writePosition = 0;
		}
		written = true;
	    }
	}
	if (!written) {
	    try {
		Thread.sleep(100);
	    } catch (Exception x) {
		throw new IOException("Waiting for available space in buffer interrupted.");
	    }
	}
    }
}
 
Example #5
Source File: CircularByteBuffer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Write a portion of an array of bytes.
 * If the buffer allows blocking writes, this method will block until
 * all the data has been written rather than throw an IOException.
 *
 * @param cbuf
 *            Array of bytes
 * @param off
 *            Offset from which to start writing bytes
 * @param len
 *            - Number of bytes to write
 * @throws BufferOverflowException
 *             if buffer does not allow blocking writes
 *             and the buffer is full. If the exception is thrown, no data
 *             will have been written since the buffer was set to be non-blocking.
 * @throws IOException
 *             if the stream is closed, or the write is interrupted.
 *
 * @since ostermillerutils 1.00.00
 */
@Override
public void write(byte[] cbuf, int off, int len) throws IOException {
    while (len > 0) {
	synchronized (CircularByteBuffer.this) {
	    if (outputStreamClosed) {
		throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream.");
	    }
	    if (inputStreamClosed) {
		throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer.");
	    }
	    int spaceLeft = spaceLeft();
	    while (infinite && spaceLeft < len) {
		resize();
		spaceLeft = spaceLeft();
	    }
	    if (!blockingWrite && spaceLeft < len) {
		throw new BufferOverflowException("CircularByteBuffer is full; cannot write " + len + " bytes");
	    }
	    int realLen = Math.min(len, spaceLeft);
	    int firstLen = Math.min(realLen, buffer.length - writePosition);
	    int secondLen = Math.min(realLen - firstLen, buffer.length - markPosition - 1);
	    int written = firstLen + secondLen;
	    if (firstLen > 0) {
		System.arraycopy(cbuf, off, buffer, writePosition, firstLen);
	    }
	    if (secondLen > 0) {
		System.arraycopy(cbuf, off + firstLen, buffer, 0, secondLen);
		writePosition = secondLen;
	    } else {
		writePosition += written;
	    }
	    if (writePosition == buffer.length) {
		writePosition = 0;
	    }
	    off += written;
	    len -= written;
	}
	if (len > 0) {
	    try {
		Thread.sleep(100);
	    } catch (Exception x) {
		throw new IOException("Waiting for available space in buffer interrupted.");
	    }
	}
    }
}