File: TarOutputStream.java

package info (click to toggle)
jedit 4.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,252 kB
  • sloc: java: 90,581; xml: 88,372; makefile: 55; sh: 25
file content (330 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
** Authored by Timothy Gerard Endres
** <mailto:time@gjt.org>  <http://www.trustice.com>
** 
** This work has been placed into the public domain.
** You may use this work in any way and for any purpose you wish.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE. 
** 
*/

package installer;

import java.io.*;


/**
 * The TarOutputStream writes a UNIX tar archive as an OutputStream.
 * Methods are provided to put entries, and then write their contents
 * by writing to this stream using write().
 *
 *
 * @version $Revision: 12504 $
 * @author Timothy Gerard Endres,
 *  <a href="mailto:time@gjt.org">time@trustice.com</a>.
 * @see TarBuffer
 * @see TarHeader
 * @see TarEntry
 */


public
class		TarOutputStream
extends		FilterOutputStream
	{
	protected boolean			debug;
	protected int				currSize;
	protected int				currBytes;
	protected byte[]			oneBuf;
	protected byte[]			recordBuf;
	protected int				assemLen;
	protected byte[]			assemBuf;
	protected TarBuffer			buffer;


	public
	TarOutputStream( OutputStream os )
		{
		this( os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE );
		}

	public
	TarOutputStream( OutputStream os, int blockSize )
		{
		this( os, blockSize, TarBuffer.DEFAULT_RCDSIZE );
		}

	public
	TarOutputStream( OutputStream os, int blockSize, int recordSize )
		{
		super( os );

		this.buffer = new TarBuffer( os, blockSize, recordSize );
		
		this.debug = false;
		this.assemLen = 0;
		this.assemBuf = new byte[ recordSize ];
		this.recordBuf = new byte[ recordSize ];
		this.oneBuf = new byte[1];
		}

	/**
	 * Sets the debugging flag.
	 *
	 * @param debugF True to turn on debugging.
	 */
	public void
	setDebug( boolean debugF )
		{
		this.debug = debugF;
		}

	/**
	 * Sets the debugging flag in this stream's TarBuffer.
	 *
	 * @param debugF True to turn on debugging.
	 */
	public void
	setBufferDebug( boolean debug )
		{
		this.buffer.setDebug( debug );
		}

	/**
	 * Ends the TAR archive without closing the underlying OutputStream.
	 * The result is that the EOF record of nulls is written.
	 */

	public void
	finish()
		throws IOException
		{
		this.writeEOFRecord();
		}

	/**
	 * Ends the TAR archive and closes the underlying OutputStream.
	 * This means that finish() is called followed by calling the
	 * TarBuffer's close().
	 */

	public void
	close()
		throws IOException
		{
		this.finish();
		this.buffer.close();
		}

	/**
	 * Get the record size being used by this stream's TarBuffer.
	 *
	 * @return The TarBuffer record size.
	 */
	public int
	getRecordSize()
		{
		return this.buffer.getRecordSize();
		}

	/**
	 * Put an entry on the output stream. This writes the entry's
	 * header record and positions the output stream for writing
	 * the contents of the entry. Once this method is called, the
	 * stream is ready for calls to write() to write the entry's
	 * contents. Once the contents are written, closeEntry()
	 * <B>MUST</B> be called to ensure that all buffered data
	 * is completely written to the output stream.
	 *
	 * @param entry The TarEntry to be written to the archive.
	 */
	public void
	putNextEntry( TarEntry entry )
		throws IOException
		{
		if ( entry.getHeader().name.length() > TarHeader.NAMELEN )
			throw new InvalidHeaderException
				( "file name '" + entry.getHeader().name
					+ "' is too long ( > "
					+ TarHeader.NAMELEN + " bytes )" );

		entry.writeEntryHeader( this.recordBuf );
		this.buffer.writeRecord( this.recordBuf );

		this.currBytes = 0;

		if ( entry.isDirectory() )
			this.currSize = 0;
		else
			this.currSize = (int)entry.getSize();
		}

	/**
	 * Close an entry. This method MUST be called for all file
	 * entries that contain data. The reason is that we must
	 * buffer data written to the stream in order to satisfy
	 * the buffer's record based writes. Thus, there may be
	 * data fragments still being assembled that must be written
	 * to the output stream before this entry is closed and the
	 * next entry written.
	 */
	public void
	closeEntry()
		throws IOException
		{
		if ( this.assemLen > 0 )
			{
			for ( int i = this.assemLen ; i < this.assemBuf.length ; ++i )
				this.assemBuf[i] = 0;

			this.buffer.writeRecord( this.assemBuf );

			this.currBytes += this.assemLen;
			this.assemLen = 0;
			}

		if ( this.currBytes < this.currSize )
			throw new IOException
				( "entry closed at '" + this.currBytes
					+ "' before the '" + this.currSize
					+ "' bytes specified in the header were written" );
		}

	/**
	 * Writes a byte to the current tar archive entry.
	 *
	 * This method simply calls read( byte[], int, int ).
	 *
	 * @param b The byte written.
	 */
	public void
	write( int b )
		throws IOException
		{
		this.oneBuf[0] = (byte) b;
		this.write( this.oneBuf, 0, 1 );
		}

	/**
	 * Writes bytes to the current tar archive entry.
	 *
	 * This method simply calls read( byte[], int, int ).
	 *
	 * @param wBuf The buffer to write to the archive.
	 * @return The number of bytes read, or -1 at EOF.
	 */
	public void
	write( byte[] wBuf )
		throws IOException
		{
		this.write( wBuf, 0, wBuf.length );
		}

	/**
	 * Writes bytes to the current tar archive entry. This method
	 * is aware of the current entry and will throw an exception if
	 * you attempt to write bytes past the length specified for the
	 * current entry. The method is also (painfully) aware of the
	 * record buffering required by TarBuffer, and manages buffers
	 * that are not a multiple of recordsize in length, including
	 * assembling records from small buffers.
	 *
	 * This method simply calls read( byte[], int, int ).
	 *
	 * @param wBuf The buffer to write to the archive.
	 * @param wOffset The offset in the buffer from which to get bytes.
	 * @param numToWrite The number of bytes to write.
	 */
	public void
	write( byte[] wBuf, int wOffset, int numToWrite )
		throws IOException
		{
		if ( (this.currBytes + numToWrite) > this.currSize )
			throw new IOException
				( "request to write '" + numToWrite
					+ "' bytes exceeds size in header of '"
					+ this.currSize + "' bytes" );

		//
		// We have to deal with assembly!!!
		// The programmer can be writing little 32 byte chunks for all
		// we know, and we must assemble complete records for writing.
		// REVIEW Maybe this should be in TarBuffer? Could that help to
		//        eliminate some of the buffer copying.
		//
		if ( this.assemLen > 0 )
			{
			if ( (this.assemLen + numToWrite ) >= this.recordBuf.length )
				{
				int aLen = this.recordBuf.length - this.assemLen;

				System.arraycopy
					( this.assemBuf, 0, this.recordBuf, 0, this.assemLen );

				System.arraycopy
					( wBuf, wOffset, this.recordBuf, this.assemLen, aLen );

				this.buffer.writeRecord( this.recordBuf );

				this.currBytes += this.recordBuf.length;

				wOffset += aLen;
				numToWrite -= aLen;
				this.assemLen = 0;
				}
			else // ( (this.assemLen + numToWrite ) < this.recordBuf.length )
				{
				System.arraycopy
					( wBuf, wOffset, this.assemBuf,
						this.assemLen, numToWrite );
				wOffset += numToWrite;
				this.assemLen += numToWrite; 
				numToWrite -= numToWrite;
				}
			}

		//
		// When we get here we have EITHER:
		//   o An empty "assemble" buffer.
		//   o No bytes to write (numToWrite == 0)
		//

		for ( ; numToWrite > 0 ; )
			{
			if ( numToWrite < this.recordBuf.length )
				{
				System.arraycopy
					( wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite );
				this.assemLen += numToWrite;
				break;
				}

			this.buffer.writeRecord( wBuf, wOffset );

			int num = this.recordBuf.length;
			this.currBytes += num;
			numToWrite -= num;
			wOffset += num;
			}
		}

	/**
	 * Write an EOF (end of archive) record to the tar archive.
	 * An EOF record consists of a record of all zeros.
	 */
	private void
	writeEOFRecord()
		throws IOException
		{
		for ( int i = 0 ; i < this.recordBuf.length ; ++i )
			this.recordBuf[i] = 0;
		this.buffer.writeRecord( this.recordBuf );
		}

	}