|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ExpandingBuffer.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.io; | |
| 36 | ||
| 37 | import java.io.Serializable; | |
| 38 | import java.util.LinkedList; | |
| 39 | ||
| 40 | /** | |
| 41 | * Abstraction of {@link ExpandingByteBuffer} and {@link ExpandingCharBuffer} to manage | |
| 42 | * indices and bookeeping for these buffers from a single control point. In general, this | |
| 43 | * class represents an expandable and thread safe buffer of elements of some type. {@code T} is the | |
| 44 | * type of a sequence of these elements of fixed length {@link #BUFFER_SIZE}. Subclasses are responsible | |
| 45 | * for managing reading and writing, but need not interact directly with the expanding queue | |
| 46 | * of {@code T}s, nor with the indices used to manage this queue. Instead, the methods in | |
| 47 | * this class provide the necessary tools. Synchronization should occur on the ExpandingBuffer | |
| 48 | * object to prevent conflicts between threads before invoking any of this class's helper methods. | |
| 49 | */ | |
| 50 | public abstract class ExpandingBuffer<T> implements Serializable { | |
| 51 | ||
| 52 | protected static final int BUFFER_SIZE = 1024; | |
| 53 | ||
| 54 | private final LinkedList<T> _buffers; | |
| 55 | ||
| 56 | /* | |
| 57 | * The indices below are assumed to be positive longs. This assertion is not checked, and they | |
| 58 | * will eventually wrap around, but this assumption holds for over 8 exabytes (8 million terabytes) | |
| 59 | * of data (assuming 1 byte per element). | |
| 60 | */ | |
| 61 | ||
| 62 | /** | |
| 63 | * The virtual index of {@code _buffers.getFirst()[0]} (or {@code _nextBuffer} if {@code _buffers} | |
| 64 | * is empty) | |
| 65 | */ | |
| 66 | private long _base; | |
| 67 | ||
| 68 | /** | |
| 69 | * The virtual index of the beginning of the next buffer to be allocated -- {@code BUFFER_SIZE} after | |
| 70 | * {@code _buffers.getLast()} ({@code _nextBuffer >= _base}) | |
| 71 | */ | |
| 72 | private long _nextBuffer; | |
| 73 | ||
| 74 | /** | |
| 75 | * The virtual index of the first character in the virtual buffer ({@code _first >= _base}, | |
| 76 | * {@code _first <= _last}) | |
| 77 | */ | |
| 78 | private long _first; | |
| 79 | ||
| 80 | /** | |
| 81 | * The virtual index *after* the last character in the virtual buffer ({@code _last >= _first}, | |
| 82 | * {@code _last <= _nextBuffer}) | |
| 83 | */ | |
| 84 | private long _last; | |
| 85 | ||
| 86 | 0 | public ExpandingBuffer() { |
| 87 | 0 | _buffers = new LinkedList<T>(); |
| 88 | 0 | _base = 0l; |
| 89 | 0 | _nextBuffer = 0l; |
| 90 | 0 | _first = 0l; |
| 91 | 0 | _last = 0l; |
| 92 | } | |
| 93 | ||
| 94 | /** Create a fixed-size sub-buffer */ | |
| 95 | protected abstract T allocateBuffer(int size); | |
| 96 | ||
| 97 | /** | |
| 98 | * @return the size of the buffer | |
| 99 | */ | |
| 100 | 0 | public synchronized long size() { |
| 101 | 0 | return _last - _first; |
| 102 | } | |
| 103 | ||
| 104 | 0 | public synchronized boolean isEmpty() { |
| 105 | 0 | return _first == _last; |
| 106 | } | |
| 107 | ||
| 108 | ||
| 109 | /** | |
| 110 | * Allocate space in the buffer if none is available. Ensures that there is room for at least | |
| 111 | * one new element (and that {@code _buffers} is nonempty). Should be called <em>before</em> | |
| 112 | * a write is attempted. | |
| 113 | * @return The amount of space now available at the end of the buffer ({@code > 0}) | |
| 114 | */ | |
| 115 | 0 | protected int allocate() { |
| 116 | 0 | if (_last == _nextBuffer) { |
| 117 | 0 | _buffers.addLast(allocateBuffer(BUFFER_SIZE)); |
| 118 | 0 | _nextBuffer += BUFFER_SIZE; |
| 119 | 0 | return BUFFER_SIZE; |
| 120 | } | |
| 121 | 0 | else { return (int) (_nextBuffer - _last); } |
| 122 | } | |
| 123 | ||
| 124 | /** Determine the number of buffered elements located in {@code _buffers.getFirst()} */ | |
| 125 | 0 | protected int elementsInFirstBuffer() { |
| 126 | 0 | long secondBuffer = _base + BUFFER_SIZE; |
| 127 | 0 | return (int) (((secondBuffer > _last) ? _last : secondBuffer) - _base); |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Deallocate the first buffer if it is no longer needed. Return true if deallocation took place. | |
| 132 | * Should be called <em>after</em> a read occurs. | |
| 133 | */ | |
| 134 | 0 | protected boolean deallocate() { |
| 135 | 0 | long secondBuffer = _base + BUFFER_SIZE; |
| 136 | 0 | if (_first >= secondBuffer) { |
| 137 | 0 | _buffers.removeFirst(); |
| 138 | 0 | _base = secondBuffer; |
| 139 | 0 | return true; |
| 140 | } | |
| 141 | 0 | else { return false; } |
| 142 | } | |
| 143 | ||
| 144 | /** Access the first buffer (assuming it exists) */ | |
| 145 | 0 | protected T firstBuffer() { return _buffers.getFirst(); } |
| 146 | ||
| 147 | /** Calculate the first valid index in {@code firstBuffer()} (assuming it exists) */ | |
| 148 | 0 | protected int firstIndex() { return (int) (_first - _base); } |
| 149 | ||
| 150 | /** Access the last buffer (assuming it exists) */ | |
| 151 | 0 | protected T lastBuffer() { return _buffers.getLast(); } |
| 152 | ||
| 153 | /** Calculate the index after the last valid element in {@code lastBuffer()} (assuming it exists) */ | |
| 154 | 0 | protected int lastIndex() { return (int) (_last - (_nextBuffer - BUFFER_SIZE)); } |
| 155 | ||
| 156 | /** Adjust the indices after writing the given number of elements. */ | |
| 157 | 0 | protected void recordWrite(long written) { _last += written; } |
| 158 | ||
| 159 | /** Adjust the indices after reading the given number of elements. */ | |
| 160 | 0 | protected void recordRead(long read) { _first += read; } |
| 161 | } |
|
||||||||||