blob: 49f9d873259a106fdc8ec49095b01930d4a67cb0 [file] [log] [blame]
Torsten Curdtca165392008-07-10 10:17:44 +00001/*
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 */
19package org.apache.commons.compress.archivers.tar;
20
21import java.io.FilterOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25
26/**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000027 * The TarOutputStream writes a UNIX tar archive as an OutputStream.
28 * Methods are provided to put entries, and then write their contents
29 * by writing to this stream using write().
30 *
Torsten Curdtca165392008-07-10 10:17:44 +000031 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +000032public class TarOutputStream extends FilterOutputStream {
33 /** Fail if a long file name is required in the archive. */
Torsten Curdtca165392008-07-10 10:17:44 +000034 public static final int LONGFILE_ERROR = 0;
35
Torsten Curdt46ad24d2009-01-08 11:09:25 +000036 /** Long paths will be truncated in the archive. */
Torsten Curdtca165392008-07-10 10:17:44 +000037 public static final int LONGFILE_TRUNCATE = 1;
38
Torsten Curdt46ad24d2009-01-08 11:09:25 +000039 /** GNU tar extensions are used to store long file names in the archive. */
Torsten Curdtca165392008-07-10 10:17:44 +000040 public static final int LONGFILE_GNU = 2;
41
Torsten Curdt46ad24d2009-01-08 11:09:25 +000042 // CheckStyle:VisibilityModifier OFF - bc
43 protected boolean debug;
44 protected long currSize;
45 protected String currName;
46 protected long currBytes;
47 protected byte[] oneBuf;
48 protected byte[] recordBuf;
49 protected int assemLen;
50 protected byte[] assemBuf;
51 protected TarBuffer buffer;
52 protected int longFileMode = LONGFILE_ERROR;
53 // CheckStyle:VisibilityModifier ON
Torsten Curdtca165392008-07-10 10:17:44 +000054
Torsten Curdt46ad24d2009-01-08 11:09:25 +000055 private boolean closed = false;
Torsten Curdtca165392008-07-10 10:17:44 +000056
57 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000058 * Constructor for TarInputStream.
59 * @param os the output stream to use
Torsten Curdtca165392008-07-10 10:17:44 +000060 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +000061 public TarOutputStream(OutputStream os) {
62 this(os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
Torsten Curdtca165392008-07-10 10:17:44 +000063 }
64
65 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000066 * Constructor for TarInputStream.
67 * @param os the output stream to use
68 * @param blockSize the block size to use
Torsten Curdtca165392008-07-10 10:17:44 +000069 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +000070 public TarOutputStream(OutputStream os, int blockSize) {
71 this(os, blockSize, TarBuffer.DEFAULT_RCDSIZE);
Torsten Curdtca165392008-07-10 10:17:44 +000072 }
73
74 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000075 * Constructor for TarInputStream.
76 * @param os the output stream to use
77 * @param blockSize the block size to use
78 * @param recordSize the record size to use
Torsten Curdtca165392008-07-10 10:17:44 +000079 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +000080 public TarOutputStream(OutputStream os, int blockSize, int recordSize) {
81 super(os);
Torsten Curdtca165392008-07-10 10:17:44 +000082
Torsten Curdt46ad24d2009-01-08 11:09:25 +000083 this.buffer = new TarBuffer(os, blockSize, recordSize);
84 this.debug = false;
85 this.assemLen = 0;
86 this.assemBuf = new byte[recordSize];
87 this.recordBuf = new byte[recordSize];
88 this.oneBuf = new byte[1];
89 }
90
91 /**
92 * Set the long file mode.
93 * This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or LONGFILE_GNU(2).
94 * This specifies the treatment of long file names (names >= TarConstants.NAMELEN).
95 * Default is LONGFILE_ERROR.
96 * @param longFileMode the mode to use
97 */
98 public void setLongFileMode(int longFileMode) {
99 this.longFileMode = longFileMode;
100 }
101
102
103 /**
104 * Sets the debugging flag.
105 *
106 * @param debugF True to turn on debugging.
107 */
108 public void setDebug(boolean debugF) {
109 this.debug = debugF;
Torsten Curdtca165392008-07-10 10:17:44 +0000110 }
111
112 /**
113 * Sets the debugging flag in this stream's TarBuffer.
114 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000115 * @param debug True to turn on debugging.
Torsten Curdtca165392008-07-10 10:17:44 +0000116 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000117 public void setBufferDebug(boolean debug) {
118 buffer.setDebug(debug);
Torsten Curdtca165392008-07-10 10:17:44 +0000119 }
120
121 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000122 * Ends the TAR archive without closing the underlying OutputStream.
123 * The result is that the two EOF records of nulls are written.
124 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000125 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000126 public void finish() throws IOException {
127 // See Bugzilla 28776 for a discussion on this
128 // http://issues.apache.org/bugzilla/show_bug.cgi?id=28776
129 writeEOFRecord();
130 writeEOFRecord();
131 }
132
133 /**
134 * Ends the TAR archive and closes the underlying OutputStream.
135 * This means that finish() is called followed by calling the
136 * TarBuffer's close().
137 * @throws IOException on error
138 */
139 public void close() throws IOException {
140 if (!closed) {
141 finish();
142 buffer.close();
143 out.close();
144 closed = true;
Torsten Curdtca165392008-07-10 10:17:44 +0000145 }
Torsten Curdtca165392008-07-10 10:17:44 +0000146 }
147
148 /**
149 * Get the record size being used by this stream's TarBuffer.
150 *
151 * @return The TarBuffer record size.
152 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000153 public int getRecordSize() {
154 return buffer.getRecordSize();
Torsten Curdtca165392008-07-10 10:17:44 +0000155 }
156
157 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000158 * Put an entry on the output stream. This writes the entry's
159 * header record and positions the output stream for writing
160 * the contents of the entry. Once this method is called, the
161 * stream is ready for calls to write() to write the entry's
162 * contents. Once the contents are written, closeEntry()
163 * <B>MUST</B> be called to ensure that all buffered data
164 * is completely written to the output stream.
Torsten Curdtca165392008-07-10 10:17:44 +0000165 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000166 * @param entry The TarEntry to be written to the archive.
167 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000168 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000169 public void putNextEntry(TarArchiveEntry entry) throws IOException {
170 if (entry.getName().length() >= TarConstants.NAMELEN) {
Torsten Curdtca165392008-07-10 10:17:44 +0000171
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000172 if (longFileMode == LONGFILE_GNU) {
173 // create a TarEntry for the LongLink, the contents
Torsten Curdtca165392008-07-10 10:17:44 +0000174 // of which are the entry's name
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000175 TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK,
176 TarConstants.LF_GNUTYPE_LONGNAME);
Torsten Curdtca165392008-07-10 10:17:44 +0000177
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000178 longLinkEntry.setSize(entry.getName().length() + 1);
179 putNextEntry(longLinkEntry);
180 write(entry.getName().getBytes());
181 write(0);
Torsten Curdtca165392008-07-10 10:17:44 +0000182 closeEntry();
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000183 } else if (longFileMode != LONGFILE_TRUNCATE) {
184 throw new RuntimeException("file name '" + entry.getName()
185 + "' is too long ( > "
186 + TarConstants.NAMELEN + " bytes)");
Torsten Curdtca165392008-07-10 10:17:44 +0000187 }
188 }
189
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000190 entry.writeEntryHeader(recordBuf);
191 buffer.writeRecord(recordBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000192
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000193 currBytes = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000194
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000195 if (entry.isDirectory()) {
196 currSize = 0;
197 } else {
198 currSize = entry.getSize();
Torsten Curdtca165392008-07-10 10:17:44 +0000199 }
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000200 currName = entry.getName();
Torsten Curdtca165392008-07-10 10:17:44 +0000201 }
202
203 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000204 * Close an entry. This method MUST be called for all file
205 * entries that contain data. The reason is that we must
206 * buffer data written to the stream in order to satisfy
207 * the buffer's record based writes. Thus, there may be
208 * data fragments still being assembled that must be written
209 * to the output stream before this entry is closed and the
210 * next entry written.
211 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000212 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000213 public void closeEntry() throws IOException {
214 if (assemLen > 0) {
215 for (int i = assemLen; i < assemBuf.length; ++i) {
216 assemBuf[i] = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000217 }
218
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000219 buffer.writeRecord(assemBuf);
220
221 currBytes += assemLen;
222 assemLen = 0;
223 }
224
225 if (currBytes < currSize) {
226 throw new IOException("entry '" + currName + "' closed at '"
227 + currBytes
228 + "' before the '" + currSize
229 + "' bytes specified in the header were written");
Torsten Curdtca165392008-07-10 10:17:44 +0000230 }
231 }
232
233 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000234 * Writes a byte to the current tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000235 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000236 * This method simply calls read( byte[], int, int ).
237 *
238 * @param b The byte written.
239 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000240 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000241 public void write(int b) throws IOException {
242 oneBuf[0] = (byte) b;
Torsten Curdtca165392008-07-10 10:17:44 +0000243
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000244 write(oneBuf, 0, 1);
Torsten Curdtca165392008-07-10 10:17:44 +0000245 }
246
247 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000248 * Writes bytes to the current tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000249 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000250 * This method simply calls write( byte[], int, int ).
251 *
252 * @param wBuf The buffer to write to the archive.
253 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000254 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000255 public void write(byte[] wBuf) throws IOException {
256 write(wBuf, 0, wBuf.length);
Torsten Curdtca165392008-07-10 10:17:44 +0000257 }
258
259 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000260 * Writes bytes to the current tar archive entry. This method
261 * is aware of the current entry and will throw an exception if
262 * you attempt to write bytes past the length specified for the
263 * current entry. The method is also (painfully) aware of the
264 * record buffering required by TarBuffer, and manages buffers
265 * that are not a multiple of recordsize in length, including
266 * assembling records from small buffers.
Torsten Curdtca165392008-07-10 10:17:44 +0000267 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000268 * @param wBuf The buffer to write to the archive.
269 * @param wOffset The offset in the buffer from which to get bytes.
270 * @param numToWrite The number of bytes to write.
271 * @throws IOException on error
Torsten Curdtca165392008-07-10 10:17:44 +0000272 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000273 public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException {
274 if ((currBytes + numToWrite) > currSize) {
275 throw new IOException("request to write '" + numToWrite
276 + "' bytes exceeds size in header of '"
277 + currSize + "' bytes for entry '"
278 + currName + "'");
279
Torsten Curdtca165392008-07-10 10:17:44 +0000280 //
281 // We have to deal with assembly!!!
282 // The programmer can be writing little 32 byte chunks for all
283 // we know, and we must assemble complete records for writing.
284 // REVIEW Maybe this should be in TarBuffer? Could that help to
285 // eliminate some of the buffer copying.
286 //
287 }
288
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000289 if (assemLen > 0) {
290 if ((assemLen + numToWrite) >= recordBuf.length) {
291 int aLen = recordBuf.length - assemLen;
Torsten Curdtca165392008-07-10 10:17:44 +0000292
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000293 System.arraycopy(assemBuf, 0, recordBuf, 0,
294 assemLen);
295 System.arraycopy(wBuf, wOffset, recordBuf,
296 assemLen, aLen);
297 buffer.writeRecord(recordBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000298
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000299 currBytes += recordBuf.length;
300 wOffset += aLen;
301 numToWrite -= aLen;
302 assemLen = 0;
303 } else {
304 System.arraycopy(wBuf, wOffset, assemBuf, assemLen,
305 numToWrite);
Torsten Curdtca165392008-07-10 10:17:44 +0000306
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000307 wOffset += numToWrite;
308 assemLen += numToWrite;
Torsten Curdtca165392008-07-10 10:17:44 +0000309 numToWrite -= numToWrite;
310 }
311 }
312
313 //
314 // When we get here we have EITHER:
315 // o An empty "assemble" buffer.
316 // o No bytes to write (numToWrite == 0)
317 //
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000318 while (numToWrite > 0) {
319 if (numToWrite < recordBuf.length) {
320 System.arraycopy(wBuf, wOffset, assemBuf, assemLen,
321 numToWrite);
Torsten Curdtca165392008-07-10 10:17:44 +0000322
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000323 assemLen += numToWrite;
Torsten Curdtca165392008-07-10 10:17:44 +0000324
325 break;
326 }
327
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000328 buffer.writeRecord(wBuf, wOffset);
Torsten Curdtca165392008-07-10 10:17:44 +0000329
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000330 int num = recordBuf.length;
Torsten Curdtca165392008-07-10 10:17:44 +0000331
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000332 currBytes += num;
Torsten Curdtca165392008-07-10 10:17:44 +0000333 numToWrite -= num;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000334 wOffset += num;
Torsten Curdtca165392008-07-10 10:17:44 +0000335 }
336 }
337
338 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000339 * Write an EOF (end of archive) record to the tar archive.
340 * An EOF record consists of a record of all zeros.
Torsten Curdtca165392008-07-10 10:17:44 +0000341 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000342 private void writeEOFRecord() throws IOException {
343 for (int i = 0; i < recordBuf.length; ++i) {
344 recordBuf[i] = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000345 }
346
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000347 buffer.writeRecord(recordBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000348 }
349}