ArchiveOutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers;

  20. import java.io.File;
  21. import java.io.FilterOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.nio.file.LinkOption;
  25. import java.nio.file.Path;

  26. /**
  27.  * Archive output stream implementations are expected to override the {@link #write(byte[], int, int)} method to improve performance. They should also override
  28.  * {@link #close()} to ensure that any necessary trailers are added.
  29.  *
  30.  * <p>
  31.  * The normal sequence of calls when working with ArchiveOutputStreams is:
  32.  * </p>
  33.  * <ul>
  34.  * <li>Create ArchiveOutputStream object,</li>
  35.  * <li>optionally write SFX header (Zip only),</li>
  36.  * <li>repeat as needed:
  37.  * <ul>
  38.  * <li>{@link #putArchiveEntry(ArchiveEntry)} (writes entry header),
  39.  * <li>{@link #write(byte[])} (writes entry data, as often as needed),
  40.  * <li>{@link #closeArchiveEntry()} (closes entry),
  41.  * </ul>
  42.  * </li>
  43.  * <li>{@link #finish()} (ends the addition of entries),</li>
  44.  * <li>optionally write additional data, provided format supports it,</li>
  45.  * <li>{@link #close()}.</li>
  46.  * </ul>
  47.  *
  48.  * @param <E> The type of {@link ArchiveEntry} consumed.
  49.  */
  50. public abstract class ArchiveOutputStream<E extends ArchiveEntry> extends FilterOutputStream {

  51.     static final int BYTE_MASK = 0xFF;

  52.     /** Temporary buffer used for the {@link #write(int)} method. */
  53.     private final byte[] oneByte = new byte[1];

  54.     /** Holds the number of bytes written to this stream. */
  55.     private long bytesWritten;

  56.     /**
  57.      * Whether this instance was successfully closed.
  58.      */
  59.     private boolean closed;

  60.     /**
  61.      * Whether this instance was successfully finished.
  62.      */
  63.     private boolean finished;

  64.     /**
  65.      * Constructs a new instance without a backing OutputStream.
  66.      * <p>
  67.      * You must initialize {@code this.out} after construction.
  68.      * </p>
  69.      */
  70.     public ArchiveOutputStream() {
  71.         super(null);
  72.     }

  73.     /**
  74.      * Constructs a new instance with the given backing OutputStream.
  75.      *
  76.      * @param out the underlying output stream to be assigned to the field {@code this.out} for later use, or {@code null} if this instance is to be created
  77.      *            without an underlying stream.
  78.      * @since 1.27.0.
  79.      */
  80.     public ArchiveOutputStream(final OutputStream out) {
  81.         super(out);
  82.     }

  83.     /**
  84.      * Whether this stream is able to write the given entry.
  85.      *
  86.      * <p>
  87.      * Some archive formats support variants or details that are not supported (yet).
  88.      * </p>
  89.      *
  90.      * @param archiveEntry the entry to test
  91.      * @return This implementation always returns true.
  92.      * @since 1.1
  93.      */
  94.     public boolean canWriteEntryData(final ArchiveEntry archiveEntry) {
  95.         return true;
  96.     }

  97.     /**
  98.      * Throws an {@link IOException} if this instance is already finished.
  99.      *
  100.      * @throws IOException if this instance is already finished.
  101.      * @since 1.27.0
  102.      */
  103.     protected void checkFinished() throws IOException {
  104.         if (isFinished()) {
  105.             throw new IOException("Stream has already been finished.");
  106.         }
  107.     }

  108.     @Override
  109.     public void close() throws IOException {
  110.         super.close();
  111.         closed = true;
  112.     }

  113.     /**
  114.      * Closes the archive entry, writing any trailer information that may be required.
  115.      *
  116.      * @throws IOException if an I/O error occurs
  117.      */
  118.     public abstract void closeArchiveEntry() throws IOException;

  119.     /**
  120.      * Increments the counter of already written bytes. Doesn't increment if EOF has been hit ({@code written == -1}).
  121.      *
  122.      * @param written the number of bytes written
  123.      */
  124.     protected void count(final int written) {
  125.         count((long) written);
  126.     }

  127.     /**
  128.      * Increments the counter of already written bytes. Doesn't increment if EOF has been hit ({@code written == -1}).
  129.      *
  130.      * @param written the number of bytes written
  131.      * @since 1.1
  132.      */
  133.     protected void count(final long written) {
  134.         if (written != -1) {
  135.             bytesWritten += written;
  136.         }
  137.     }

  138.     /**
  139.      * Creates an archive entry using the inputFile and entryName provided.
  140.      *
  141.      * @param inputFile the file to create the entry from
  142.      * @param entryName name to use for the entry
  143.      * @return the ArchiveEntry set up with details from the file
  144.      *
  145.      * @throws IOException if an I/O error occurs
  146.      */
  147.     public abstract E createArchiveEntry(File inputFile, String entryName) throws IOException;

  148.     /**
  149.      * Creates an archive entry using the inputPath and entryName provided.
  150.      * <p>
  151.      * The default implementation calls simply delegates as:
  152.      * </p>
  153.      *
  154.      * <pre>
  155.      * return createArchiveEntry(inputFile.toFile(), entryName);
  156.      * </pre>
  157.      * <p>
  158.      * Subclasses should override this method.
  159.      * </p>
  160.      *
  161.      * @param inputPath the file to create the entry from
  162.      * @param entryName name to use for the entry
  163.      * @param options   options indicating how symbolic links are handled.
  164.      * @return the ArchiveEntry set up with details from the file
  165.      *
  166.      * @throws IOException if an I/O error occurs
  167.      * @since 1.21
  168.      */
  169.     public E createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
  170.         return createArchiveEntry(inputPath.toFile(), entryName);
  171.     }

  172.     /**
  173.      * Finishes the addition of entries to this stream, without closing it. Additional data can be written, if the format supports it.
  174.      *
  175.      * @throws IOException Maybe thrown by subclasses if the user forgets to close the entry.
  176.      */
  177.     public void finish() throws IOException {
  178.         finished = true;
  179.     }

  180.     /**
  181.      * Gets the current number of bytes written to this stream.
  182.      *
  183.      * @return the number of written bytes
  184.      * @since 1.1
  185.      */
  186.     public long getBytesWritten() {
  187.         return bytesWritten;
  188.     }

  189.     /**
  190.      * Gets the current number of bytes written to this stream.
  191.      *
  192.      * @return the number of written bytes
  193.      * @deprecated this method may yield wrong results for large archives, use #getBytesWritten instead
  194.      */
  195.     @Deprecated
  196.     public int getCount() {
  197.         return (int) bytesWritten;
  198.     }

  199.     /**
  200.      * Tests whether this instance was successfully closed.
  201.      *
  202.      * @return whether this instance was successfully closed.
  203.      * @since 1.27.0
  204.      */
  205.     protected boolean isClosed() {
  206.         return closed;
  207.     }

  208.     /**
  209.      * Tests whether this instance was successfully finished.
  210.      *
  211.      * @return whether this instance was successfully finished.
  212.      * @since 1.27.0
  213.      */
  214.     protected boolean isFinished() {
  215.         return finished;
  216.     }

  217.     /**
  218.      * Writes the headers for an archive entry to the output stream. The caller must then write the content to the stream and call {@link #closeArchiveEntry()}
  219.      * to complete the process.
  220.      *
  221.      * @param entry describes the entry
  222.      * @throws IOException if an I/O error occurs
  223.      */
  224.     public abstract void putArchiveEntry(E entry) throws IOException;

  225.     /**
  226.      * Writes a byte to the current archive entry.
  227.      *
  228.      * <p>
  229.      * This method simply calls {@code write( byte[], 0, 1 )}.
  230.      *
  231.      * <p>
  232.      * MUST be overridden if the {@link #write(byte[], int, int)} method is not overridden; may be overridden otherwise.
  233.      *
  234.      * @param b The byte to be written.
  235.      * @throws IOException on error
  236.      */
  237.     @Override
  238.     public void write(final int b) throws IOException {
  239.         oneByte[0] = (byte) (b & BYTE_MASK);
  240.         write(oneByte, 0, 1);
  241.     }

  242.     /**
  243.      * Check to make sure that this stream has not been closed
  244.      *
  245.      * @throws IOException if the stream is already closed
  246.      * @since 1.27.0
  247.      */
  248.     protected void checkOpen() throws IOException {
  249.         if (isClosed()) {
  250.             throw new IOException("Stream closed");
  251.         }
  252.     }
  253. }