📄 circularobjectbuffer.java
字号:
/* * Circular Object Buffer * Copyright (C) 2002 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * See COPYING.TXT for details. */package com.Ostermiller.util;/** * Implements the Circular Buffer producer/consumer model for Objects. * More information about this class is available from <a target="_top" href= * "http://ostermiller.org/utils/CircularObjectBuffer.html">ostermiller.org</a>. * <p> * This class is thread safe. * * @see CircularCharBuffer * @see CircularByteBuffer * * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities * @since ostermillerutils 1.00.00 */public class CircularObjectBuffer { /** * The default size for a circular object buffer. * * @since ostermillerutils 1.00.00 */ private final static int DEFAULT_SIZE = 1024; /** * A buffer that will grow as things are added. * * @since ostermillerutils 1.00.00 */ public final static int INFINITE_SIZE = -1; /** * The circular buffer. * <p> * The actual capacity of the buffer is one less than the actual length * of the buffer so that an empty and a full buffer can be * distinguished. An empty buffer will have the readPostion and the * writePosition equal to each other. A full buffer will have * the writePosition one less than the readPostion. * <p> * There are two important indexes into the buffer: * The readPosition, and the writePosition. The Objects * available to be read go from the readPosition to the writePosition, * wrapping around the end of the buffer. The space available for writing * goes from the write position to one less than the readPosition, * wrapping around the end of the buffer. * * @since ostermillerutils 1.00.00 */ protected Object[] buffer; /** * Index of the first Object available to be read. * * @since ostermillerutils 1.00.00 */ protected volatile int readPosition = 0; /** * Index of the first Object available to be written. * * @since ostermillerutils 1.00.00 */ protected volatile int writePosition = 0; /** * If this buffer is infinite (should resize itself when full) * * @since ostermillerutils 1.00.00 */ protected volatile boolean infinite = false; /** * True if a write to a full buffer should block until the buffer * has room, false if the write method should throw an IOException * * @since ostermillerutils 1.00.00 */ protected boolean blockingWrite = true; /** * True when no more input is coming into this buffer. At that * point reading from the buffer may return null if the buffer * is empty, otherwise a read will block until an Object is available. * * @since ostermillerutils 1.00.00 */ protected boolean inputDone = false; /** * Make this buffer ready for reuse. The contents of the buffer * will be cleared and the streams associated with this buffer * will be reopened if they had been closed. * * @since ostermillerutils 1.00.00 */ public void clear(){ synchronized (this){ readPosition = 0; writePosition = 0; inputDone = false; } } /** * Get number of Objects that are available to be read. * <p> * Note that the number of Objects available plus * the number of Objects free may not add up to the * capacity of this buffer, as the buffer may reserve some * space for other purposes. * * @return the size in Objects of this buffer * * @since ostermillerutils 1.00.00 */ public int getAvailable(){ synchronized (this){ return available(); } } /** * Get the number of Objects this buffer has free for * writing. * <p> * Note that the number of Objects available plus * the number of Objects free may not add up to the * capacity of this buffer, as the buffer may reserve some * space for other purposes. * * @return the available space in Objects of this buffer * * @since ostermillerutils 1.00.00 */ public int getSpaceLeft(){ synchronized (this){ return spaceLeft(); } } /** * Get the capacity of this buffer. * <p> * Note that the number of Objects available plus * the number of Objects free may not add up to the * capacity of this buffer, as the buffer may reserve some * space for other purposes. * * @return the size in Objects of this buffer * * @since ostermillerutils 1.00.00 */ public int getSize(){ synchronized (this){ return buffer.length; } } /** * double the size of the buffer * * @since ostermillerutils 1.00.00 */ private void resize(){ Object[] newBuffer = new Object[buffer.length * 2]; int available = available(); if (readPosition <= writePosition){ // any space between the read and // the first write needs to be saved. // In this case it is all in one piece. int length = writePosition - readPosition; System.arraycopy(buffer, readPosition, newBuffer, 0, length); } else { int length1 = buffer.length - readPosition; System.arraycopy(buffer, readPosition, newBuffer, 0, length1); int length2 = writePosition; System.arraycopy(buffer, 0, newBuffer, length1, length2); } buffer = newBuffer; readPosition = 0; writePosition = available; } /** * Space available in the buffer which can be written. * * @since ostermillerutils 1.00.00 */ private int spaceLeft(){ if (writePosition < readPosition){ // any space between the first write and // the read except one Object is available. // In this case it is all in one piece. return (readPosition - writePosition - 1); } else { // space at the beginning and end. return ((buffer.length - 1) - (writePosition - readPosition)); } } /** * Objects available for reading. * * @since ostermillerutils 1.00.00 */ private int available(){ if (readPosition <= writePosition){ // any space between the first read and // the first write is available. In this case i // is all in one piece. return (writePosition - readPosition); } else { // space at the beginning and end. return (buffer.length - (readPosition - writePosition)); } } /** * Create a new buffer with a default capacity. * Writing to a full buffer will block until space * is available rather than throw an exception. * * @since ostermillerutils 1.00.00 */ public CircularObjectBuffer(){ this (DEFAULT_SIZE, true); } /** * Create a new buffer with given capacity. * Writing to a full buffer will block until space * is available rather than throw an exception. * <p> * Note that the buffer may reserve some Objects for * special purposes and capacity number of Objects may * not be able to be written to the buffer. * <p> * Note that if the buffer is of INFINITE_SIZE it will * neither block or throw exceptions, but rather grow * without bound. * * @param size desired capacity of the buffer in Objects or CircularObjectBuffer.INFINITE_SIZE. * * @since ostermillerutils 1.00.00 */ public CircularObjectBuffer(int size){ this (size, true); } /** * Create a new buffer with a default capacity and * given blocking behavior. * * @param blockingWrite true writing to a full buffer should block * until space is available, false if an exception should * be thrown instead. * * @since ostermillerutils 1.00.00 */ public CircularObjectBuffer(boolean blockingWrite){ this (DEFAULT_SIZE, blockingWrite); } /** * Create a new buffer with the given capacity and * blocking behavior. * <p> * Note that the buffer may reserve some Objects for * special purposes and capacity number of Objects may
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -