API for compressors
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/822bc577 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/822bc577 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/822bc577 Branch: refs/heads/compress-2.0 Commit: 822bc577296236275fb0dec76eebc6a11fbb2bef Parents: b691d9a Author: Stefan Bodewig <bode...@apache.org> Authored: Sat Apr 29 18:02:48 2017 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sat Apr 29 18:02:48 2017 +0200 ---------------------------------------------------------------------- .../compress2/compressors/CompressedInput.java | 41 +++++++++++ .../compress2/compressors/CompressedOutput.java | 51 ++++++++++++++ .../compressors/CompressionFormat.java | 71 ++++++++++++++++++++ .../spi/AbstractCompressedInput.java | 56 +++++++++++++++ .../spi/AbstractCompressedOutput.java | 45 +++++++++++++ .../spi/AbstractCompressionFormat.java | 71 ++++++++++++++++++++ 6 files changed, 335 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/CompressedInput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/CompressedInput.java b/src/main/java/org/apache/commons/compress2/compressors/CompressedInput.java new file mode 100644 index 0000000..00a87e2 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/CompressedInput.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors; + +import java.io.IOException; +import java.nio.channels.ReadableByteChannel; + +/** + * Reads compressed channels. + * @NotThreadSafe + */ +public interface CompressedInput extends AutoCloseable { + + /** + * Obtains the next compressed channel. + * @return the next channel or null if the end of the input has been reached. + */ + ReadableByteChannel next() throws IOException; + + /** + * Returns the current number of bytes read from this input. + * @return the number of read bytes + */ + long getBytesRead(); +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/CompressedOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/CompressedOutput.java b/src/main/java/org/apache/commons/compress2/compressors/CompressedOutput.java new file mode 100644 index 0000000..98430e6 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/CompressedOutput.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors; + +import java.io.IOException; +import java.nio.channels.WritableByteChannel; + +public interface CompressedOutput extends AutoCloseable { + + /** + * Prepares for writing a new compressed channel. + * + * <p>The caller must then write the content to the channel and + * close it to complete the process.</p> + * + * @return a channel to write the content to be compressed to + * @throws IOException + */ + WritableByteChannel startCompressing() throws IOException; + + /** + * Finishes the addition of compressed channels to this output, without closing it. + * + * <p>Additional data can be written, if the format supports it.<p> + * + * @throws IOException + */ + default void finish() throws IOException { } + + /** + * Returns the current number of bytes written to this output. + * @return the number of written bytes + */ + long getBytesWritten(); +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/CompressionFormat.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/CompressionFormat.java b/src/main/java/org/apache/commons/compress2/compressors/CompressionFormat.java new file mode 100644 index 0000000..ce0e0e6 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/CompressionFormat.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors; + +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.io.IOException; +import org.apache.commons.compress2.Format; + +/** + * Describes a given compression format and works as factory and content-probe at the same time. + * @Immutable + */ +public interface CompressionFormat extends Format { + + /** + * Reads a compressed channel. + * @param channel the channel to read from + * @throws IOException + */ + CompressedInput readFrom(ReadableByteChannel channel) throws IOException; + + /** + * Reads a compressed file. + * @param file the file to read from + * @throws IOException + */ + default CompressedInput readFrom(Path path) throws IOException { + return readFrom(FileChannel.open(path, StandardOpenOption.READ)); + } + + /** + * Compresses data to a given channel. + * @param channel the channel to write to + * @throws IOException + * @throws UnsupportedOperationException if this format doesn't + * support writing. + */ + CompressedOutput writeTo(WritableByteChannel channel) + throws IOException, UnsupportedOperationException; + + /** + * Compresses data to a given file. + * @param file the file to write to + * @throws IOException + * @throws UnsupportedOperationException if this format doesn't support writing + */ + default CompressedOutput writeTo(Path path) throws IOException, UnsupportedOperationException { + return writeTo(FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING)); + } +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedInput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedInput.java b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedInput.java new file mode 100644 index 0000000..667f23e --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedInput.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors.spi; + +import org.apache.commons.compress2.compressors.CompressedInput; + +/** + * Base class implementations may use. + * @NotThreadSafe + */ +public abstract class AbstractCompressedInput implements CompressedInput { + /** holds the number of bytes read from this channel */ + private long bytesRead = 0; + + @Override + public long getBytesRead() { + return bytesRead; + } + + /** + * Increments the counter of already read bytes. + * Doesn't increment if the EOF has been hit (read == -1) + * + * @param read the number of bytes read + */ + protected void count(long read) { + if (read != -1) { + bytesRead = bytesRead + read; + } + } + + /** + * Decrements the counter of already read bytes. + * + * @param pushedBack the number of bytes pushed back. + */ + protected void pushedBackBytes(long pushedBack) { + bytesRead -= pushedBack; + } +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedOutput.java b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedOutput.java new file mode 100644 index 0000000..6376402 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressedOutput.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors.spi; + +import org.apache.commons.compress2.compressors.CompressedOutput; + +/** + * Base class implementations may use. + * @NotThreadSafe + */ +public abstract class AbstractCompressedOutput implements CompressedOutput { + /** holds the number of bytes written to this channel */ + private long bytesWritten = 0; + + @Override + public long getBytesWritten() { + return bytesWritten; + } + + /** + * Increments the counter of written bytes. + * + * @param written the number of bytes written + */ + protected void count(long written) { + bytesWritten += written; + } + +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/822bc577/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressionFormat.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressionFormat.java b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressionFormat.java new file mode 100644 index 0000000..64526c8 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/compressors/spi/AbstractCompressionFormat.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress2.compressors.spi; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; +import org.apache.commons.compress2.compressors.CompressionFormat; +import org.apache.commons.compress2.compressors.CompressedOutput; + +/** + * Base class implementations may use. + * @Immutable + */ +public abstract class AbstractCompressionFormat implements CompressionFormat { + + /** + * {@inheritDoc} + * <p>This implementation always returns false.</p> + */ + @Override + public boolean supportsWriting() { return false; } + + /** + * {@inheritDoc} + * <p>This implementation always returns false.</p> + */ + @Override + public boolean supportsAutoDetection() { return false; } + /** + * {@inheritDoc} + * <p>This implementation always throws an UnsupportedOperationException.</p> + */ + @Override + public int getNumberOfBytesRequiredForAutodetection() throws UnsupportedOperationException { + throw new UnsupportedOperationException("this format doesn't support content-based detection"); + } + /** + * {@inheritDoc} + * <p>This implementation always throws an UnsupportedOperationException.</p> + */ + @Override + public boolean matches(ByteBuffer probe) throws UnsupportedOperationException { + throw new UnsupportedOperationException("this format doesn't support content-based detection"); + } + /** + * {@inheritDoc} + * <p>This implementation always throws an UnsupportedOperationException.</p> + */ + @Override + public CompressedOutput writeTo(WritableByteChannel channel) + throws IOException, UnsupportedOperationException { + throw new UnsupportedOperationException("this format is read-only"); + } +}