Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 16, 2024
2 parents 2423599 + 055a2ad commit ebf832f
Show file tree
Hide file tree
Showing 28 changed files with 99 additions and 77 deletions.
4 changes: 2 additions & 2 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Some tests are only run when specific profiles are enabled, these
tests require a lot of disk space as they test behavior for very large
archives.

mvn test -Prun-tarit
mvn test -Prun-tar-it

runs tests for tar archives and requires more than 8GiB of disk space.

mvn test -Prun-zipit
mvn test -Prun-zip-it

runs tests for zip archives that require up to 20 GiB of disk
space. In addition the tests will run for a long time (more than ten
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj.
<profiles>
<!-- Add long running tests as **/*IT.java -->
<profile>
<id>run-zipit</id>
<id>run-zip-it</id>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -466,7 +466,7 @@ Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj.
</build>
</profile>
<profile>
<id>run-tarit</id>
<id>run-tar-it</id>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Abstracts classes that compress or archive an output stream.
Expand All @@ -51,6 +52,11 @@ private static byte[] write(final OutputStream os, final String data, final Char
return bytes;
}

/**
* Whether this instance was successfully closed.
*/
private final AtomicBoolean closed = new AtomicBoolean();

/**
* Constructs a new instance without a backing {@link OutputStream}.
* <p>
Expand All @@ -71,6 +77,35 @@ public CompressFilterOutputStream(final T out) {
super(out);
}

/**
* Check to make sure that this stream has not been closed
*
* @throws IOException if the stream is already closed
*/
protected void checkOpen() throws IOException {
if (isClosed()) {
throw new IOException("Stream closed");
}
}

@Override
public void close() throws IOException {
if (closed.compareAndSet(false, true)) {
// don't propagate more than once.
super.close();
}
}

/**
* Tests whether this instance was successfully closed.
*
* @return whether this instance was successfully closed.
* @since 1.27.0
*/
public boolean isClosed() {
return closed.get();
}

/**
* Gets the underlying output stream.
*
Expand Down Expand Up @@ -108,7 +143,7 @@ public long write(final Path path) throws IOException {
*
* @param data the data.
* @return the ASCII bytes.
* @exception IOException if an I/O error occurs.
* @throws IOException if an I/O error occurs.
* @see OutputStream#write(byte[])
*/
public byte[] writeUsAscii(final String data) throws IOException {
Expand All @@ -120,7 +155,7 @@ public byte[] writeUsAscii(final String data) throws IOException {
*
* @param data the data.
* @return the ASCII bytes.
* @exception IOException if an I/O error occurs.
* @throws IOException if an I/O error occurs.
* @see OutputStream#write(byte[])
*/
public byte[] writeUsAsciiRaw(final String data) throws IOException {
Expand All @@ -132,7 +167,7 @@ public byte[] writeUsAsciiRaw(final String data) throws IOException {
*
* @param data the data.
* @return the ASCII bytes.
* @exception IOException if an I/O error occurs.
* @throws IOException if an I/O error occurs.
* @see OutputStream#write(byte[])
*/
public byte[] writeUtf8(final String data) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public abstract class ArchiveOutputStream<E extends ArchiveEntry> extends Compre
/** Holds the number of bytes written to this stream. */
private long bytesWritten;

/**
* Whether this instance was successfully closed.
*/
private boolean closed;

/**
* Whether this instance was successfully finished.
*/
Expand Down Expand Up @@ -118,24 +113,6 @@ protected void checkFinished() throws IOException {
}
}

/**
* Check to make sure that this stream has not been closed
*
* @throws IOException if the stream is already closed
* @since 1.27.0
*/
protected void checkOpen() throws IOException {
if (isClosed()) {
throw new IOException("Stream closed");
}
}

@Override
public void close() throws IOException {
super.close();
closed = true;
}

/**
* Closes the archive entry, writing any trailer information that may be required.
*
Expand Down Expand Up @@ -229,16 +206,6 @@ public int getCount() {
return (int) bytesWritten;
}

/**
* Tests whether this instance was successfully closed.
*
* @return whether this instance was successfully closed.
* @since 1.27.0
*/
public boolean isClosed() {
return closed;
}

/**
* Tests whether this instance was successfully finished.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Arrays;

import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.io.IOUtils;

/**
* An output stream that compresses into the BZip2 format into another stream.
Expand Down Expand Up @@ -398,8 +397,6 @@ private static void hbMakeCodeLengths(final byte[] len, final int[] freq, final

private BlockSort blockSorter;

private volatile boolean closed;

/**
* Constructs a new {@code BZip2CompressorOutputStream} with a blocksize of 900k.
*
Expand Down Expand Up @@ -475,19 +472,13 @@ private void bsW(final int n, final int v) throws IOException {
this.bsLive = bsLiveShadow + n;
}

private void checkClosed() throws IOException {
if (closed) {
throw new IOException("Stream closed");
}
}

@Override
public void close() throws IOException {
if (!closed) {
if (!isClosed()) {
try {
finish();
} finally {
IOUtils.close(out);
super.close();
}
}
}
Expand Down Expand Up @@ -545,8 +536,7 @@ private void endCompression() throws IOException {
}

public void finish() throws IOException {
if (!closed) {
closed = true;
if (!isClosed()) {
try {
if (this.runLength > 0) {
writeRun();
Expand Down Expand Up @@ -1167,15 +1157,15 @@ public void write(final byte[] buf, int offs, final int len) throws IOException
if (offs + len > buf.length) {
throw new IndexOutOfBoundsException("offs(" + offs + ") + len(" + len + ") > buf.length(" + buf.length + ").");
}
checkClosed();
checkOpen();
for (final int hi = offs + len; offs < hi;) {
write0(buf[offs++]);
}
}

@Override
public void write(final int b) throws IOException {
checkClosed();
checkOpen();
write0(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public DeflateCompressorOutputStream(final OutputStream outputStream, final Defl
@Override
public void close() throws IOException {
try {
out.close();
super.close();
} finally {
deflater.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public class GzipCompressorOutputStream extends CompressorOutputStream<OutputStr
/** The buffer receiving the compressed data from the deflater */
private final byte[] deflateBuffer;

/** Indicates if the stream has been closed */
private boolean closed;

/** The checksum of the uncompressed data */
private final CRC32 crc = new CRC32();

Expand Down Expand Up @@ -87,13 +84,12 @@ public GzipCompressorOutputStream(final OutputStream out, final GzipParameters p

@Override
public void close() throws IOException {
if (!closed) {
if (!isClosed()) {
try {
finish();
} finally {
deflater.end();
out.close();
closed = true;
super.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void close() throws IOException {
try {
finish();
} finally {
out.close();
super.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void close() throws IOException {
try {
finish();
} finally {
out.close();
super.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void close() throws IOException {
try {
abstractStreamBridge.stop();
} finally {
out.close();
super.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void close() throws IOException {
try {
finish();
} finally {
out.close();
super.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void close() throws IOException {
try {
finish();
} finally {
out.close();
super.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.tukaani.xz.XZOutputStream;

/**
* XZ compressor.
* Compresses an output stream using the XZ and LZMA2 compression options.
*
* <em>Calling flush()</em>
* <p>
Expand All @@ -53,9 +53,11 @@ public XZCompressorOutputStream(final OutputStream outputStream) throws IOExcept
* Creates a new XZ compressor using the specified LZMA2 preset level.
* <p>
* The presets 0-3 are fast presets with medium compression. The presets 4-6 are fairly slow presets with high compression. The default preset is 6.
* </p>
* <p>
* The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the
* uncompressed size of the file exceeds 8&nbsp;MiB, 16&nbsp;MiB, or 32&nbsp;MiB, it is waste of memory to use the presets 7, 8, or 9, respectively.
* </p>
*
* @param outputStream the stream to wrap
* @param preset the preset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ArArchiveOutputStreamTest extends AbstractTest {

@Test
public void testLongFileNamesCauseExceptionByDefault() throws IOException {
ArArchiveOutputStream ref;
final ArArchiveOutputStream ref;
try (ArArchiveOutputStream outputStream = new ArArchiveOutputStream(new ByteArrayOutputStream())) {
ref = outputStream;
final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CpioArchiveOutputStreamTest extends AbstractTest {
public void testWriteOldBinary() throws Exception {
final File file = getFile("test1.xml");
final File output = newTempFile("test.cpio");
CpioArchiveOutputStream ref;
final CpioArchiveOutputStream ref;
try (CpioArchiveOutputStream outputStream = new CpioArchiveOutputStream(Files.newOutputStream(output.toPath()), CpioConstants.FORMAT_OLD_BINARY)) {
ref = outputStream;
outputStream.putArchiveEntry(new CpioArchiveEntry(CpioConstants.FORMAT_OLD_BINARY, file, "test1.xml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class TarArchiveOutputStreamTest extends AbstractTest {

private static byte[] createTarArchiveContainingOneDirectory(final String fileName, final Date modificationDate) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream ref;
final TarArchiveOutputStream ref;
try (TarArchiveOutputStream outputStream = new TarArchiveOutputStream(baos, 1024)) {
ref = outputStream;
outputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ZipArchiveOutputStreamTest extends AbstractTempDirTest {

@Test
public void testFileBasics() throws IOException {
ZipArchiveOutputStream ref;
final ZipArchiveOutputStream ref;
try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(createTempFile())) {
ref = outputStream;
assertTrue(outputStream.isSeekable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ public void testOutputStreamMethods() throws Exception {
final Map<String, String> m = new HashMap<>();
m.put("foo", "bar");
try (OutputStream out = Files.newOutputStream(output.toPath());
OutputStream os = new Pack200CompressorOutputStream(out, m)) {
Pack200CompressorOutputStream os = new Pack200CompressorOutputStream(out, m)) {
os.write(1);
os.write(new byte[] { 2, 3 });
os.close();
assertTrue(os.isClosed());
}
}

Expand Down
Loading

0 comments on commit ebf832f

Please sign in to comment.