Skip to content

Commit 9cb0420

Browse files
committed
Make CircularBuffer part of the public API by moving it to the utils package
1 parent d4bdb6c commit 9cb0420

4 files changed

Lines changed: 336 additions & 89 deletions

File tree

src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424

2525
import org.apache.commons.compress.archivers.ArchiveException;
26+
import org.apache.commons.compress.utils.CircularBuffer;
2627
import org.apache.commons.compress.utils.InputStreamStatistics;
2728
import org.apache.commons.io.input.BoundedInputStream;
2829
import org.apache.commons.io.input.CloseShieldInputStream;

src/main/java/org/apache/commons/compress/archivers/zip/CircularBuffer.java renamed to src/main/java/org/apache/commons/compress/utils/CircularBuffer.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
* under the License.
1818
*/
1919

20-
package org.apache.commons.compress.archivers.zip;
20+
package org.apache.commons.compress.utils;
2121

2222
/**
2323
* Circular byte buffer.
2424
*
25-
* @since 1.7
25+
* @since 1.29
2626
*/
27-
final class CircularBuffer {
27+
public class CircularBuffer {
2828

2929
/** Size of the buffer */
3030
private final int size;
@@ -38,9 +38,12 @@ final class CircularBuffer {
3838
/** Index of the next data written in the buffer */
3939
private int writeIndex;
4040

41-
CircularBuffer(final int size) {
41+
private int bytesAvailable;
42+
43+
public CircularBuffer(final int size) {
4244
this.size = size;
4345
buffer = new byte[size];
46+
bytesAvailable = 0;
4447
}
4548

4649
/**
@@ -49,7 +52,7 @@ final class CircularBuffer {
4952
* @return Whether a new byte can be read from the buffer.
5053
*/
5154
public boolean available() {
52-
return readIndex != writeIndex;
55+
return bytesAvailable > 0;
5356
}
5457

5558
/**
@@ -59,11 +62,18 @@ public boolean available() {
5962
* @param length the number of bytes to copy
6063
*/
6164
public void copy(final int distance, final int length) {
65+
if (distance < 1) {
66+
throw new IllegalArgumentException("Distance must be at least 1");
67+
}
68+
69+
if (distance > size) {
70+
throw new IllegalArgumentException("Distance exceeds buffer size");
71+
}
72+
6273
final int pos1 = writeIndex - distance;
6374
final int pos2 = pos1 + length;
6475
for (int i = pos1; i < pos2; i++) {
65-
buffer[writeIndex] = buffer[(i + size) % size];
66-
writeIndex = (writeIndex + 1) % size;
76+
put(buffer[(i + size) % size]);
6777
}
6878
}
6979

@@ -76,6 +86,7 @@ public int get() {
7686
if (available()) {
7787
final int value = buffer[readIndex];
7888
readIndex = (readIndex + 1) % size;
89+
bytesAvailable--;
7990
return value & 0xFF;
8091
}
8192
return -1;
@@ -87,7 +98,12 @@ public int get() {
8798
* @param value the value to put.
8899
*/
89100
public void put(final int value) {
101+
if(bytesAvailable == size) {
102+
throw new IllegalStateException("Buffer overflow: Cannot write to a full buffer");
103+
}
104+
90105
buffer[writeIndex] = (byte) value;
91106
writeIndex = (writeIndex + 1) % size;
107+
bytesAvailable++;
92108
}
93109
}

src/test/java/org/apache/commons/compress/archivers/zip/CircularBufferTest.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)