001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.archivers; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.OutputStream; 024import java.nio.file.LinkOption; 025import java.nio.file.Path; 026 027/** 028 * Archive output stream implementations are expected to override the 029 * {@link #write(byte[], int, int)} method to improve performance. 030 * They should also override {@link #close()} to ensure that any necessary 031 * trailers are added. 032 * 033 * <p>The normal sequence of calls when working with ArchiveOutputStreams is:</p> 034 * <ul> 035 * <li>Create ArchiveOutputStream object,</li> 036 * <li>optionally write SFX header (Zip only),</li> 037 * <li>repeat as needed: 038 * <ul> 039 * <li>{@link #putArchiveEntry(ArchiveEntry)} (writes entry header), 040 * <li>{@link #write(byte[])} (writes entry data, as often as needed), 041 * <li>{@link #closeArchiveEntry()} (closes entry), 042 * </ul> 043 * </li> 044 * <li> {@link #finish()} (ends the addition of entries),</li> 045 * <li> optionally write additional data, provided format supports it,</li> 046 * <li>{@link #close()}.</li> 047 * </ul> 048 */ 049public abstract class ArchiveOutputStream extends OutputStream { 050 051 static final int BYTE_MASK = 0xFF; 052 /** Temporary buffer used for the {@link #write(int)} method */ 053 private final byte[] oneByte = new byte[1]; 054 055 /** holds the number of bytes written to this stream */ 056 private long bytesWritten; 057 // Methods specific to ArchiveOutputStream 058 059 /** 060 * Whether this stream is able to write the given entry. 061 * 062 * <p>Some archive formats support variants or details that are 063 * not supported (yet).</p> 064 * 065 * @param archiveEntry 066 * the entry to test 067 * @return This implementation always returns true. 068 * @since 1.1 069 */ 070 public boolean canWriteEntryData(final ArchiveEntry archiveEntry) { 071 return true; 072 } 073 074 /** 075 * Closes the archive entry, writing any trailer information that may 076 * be required. 077 * @throws IOException if an I/O error occurs 078 */ 079 public abstract void closeArchiveEntry() throws IOException; 080 081 /** 082 * Increments the counter of already written bytes. 083 * Doesn't increment if EOF has been hit ({@code written == -1}). 084 * 085 * @param written the number of bytes written 086 */ 087 protected void count(final int written) { 088 count((long) written); 089 } 090 091 /** 092 * Increments the counter of already written bytes. 093 * Doesn't increment if EOF has been hit ({@code written == -1}). 094 * 095 * @param written the number of bytes written 096 * @since 1.1 097 */ 098 protected void count(final long written) { 099 if (written != -1) { 100 bytesWritten = bytesWritten + written; 101 } 102 } 103 104 /** 105 * Create an archive entry using the inputFile and entryName provided. 106 * 107 * @param inputFile the file to create the entry from 108 * @param entryName name to use for the entry 109 * @return the ArchiveEntry set up with details from the file 110 * 111 * @throws IOException if an I/O error occurs 112 */ 113 public abstract ArchiveEntry createArchiveEntry(File inputFile, String entryName) throws IOException; 114 115 // Generic implementations of OutputStream methods that may be useful to sub-classes 116 117 /** 118 * Create an archive entry using the inputPath and entryName provided. 119 * 120 * The default implementation calls simply delegates as: 121 * <pre>return createArchiveEntry(inputFile.toFile(), entryName);</pre> 122 * 123 * Subclasses should override this method. 124 * 125 * @param inputPath the file to create the entry from 126 * @param entryName name to use for the entry 127 * @param options options indicating how symbolic links are handled. 128 * @return the ArchiveEntry set up with details from the file 129 * 130 * @throws IOException if an I/O error occurs 131 * @since 1.21 132 */ 133 public ArchiveEntry createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException { 134 return createArchiveEntry(inputPath.toFile(), entryName); 135 } 136 137 /** 138 * Finishes the addition of entries to this stream, without closing it. 139 * Additional data can be written, if the format supports it. 140 * 141 * @throws IOException if the user forgets to close the entry. 142 */ 143 public abstract void finish() throws IOException; 144 145 /** 146 * Returns the current number of bytes written to this stream. 147 * @return the number of written bytes 148 * @since 1.1 149 */ 150 public long getBytesWritten() { 151 return bytesWritten; 152 } 153 154 /** 155 * Returns the current number of bytes written to this stream. 156 * @return the number of written bytes 157 * @deprecated this method may yield wrong results for large 158 * archives, use #getBytesWritten instead 159 */ 160 @Deprecated 161 public int getCount() { 162 return (int) bytesWritten; 163 } 164 165 /** 166 * Writes the headers for an archive entry to the output stream. 167 * The caller must then write the content to the stream and call 168 * {@link #closeArchiveEntry()} to complete the process. 169 * 170 * @param entry describes the entry 171 * @throws IOException if an I/O error occurs 172 */ 173 public abstract void putArchiveEntry(ArchiveEntry entry) throws IOException; 174 175 /** 176 * Writes a byte to the current archive entry. 177 * 178 * <p>This method simply calls {@code write( byte[], 0, 1 )}. 179 * 180 * <p>MUST be overridden if the {@link #write(byte[], int, int)} method 181 * is not overridden; may be overridden otherwise. 182 * 183 * @param b The byte to be written. 184 * @throws IOException on error 185 */ 186 @Override 187 public void write(final int b) throws IOException { 188 oneByte[0] = (byte) (b & BYTE_MASK); 189 write(oneByte, 0, 1); 190 } 191}