File: TarInputStream.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 (519 lines) | stat: -rw-r--r-- 11,087 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
** 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 TarInputStream reads a UNIX tar archive as an InputStream.
 * methods are provided to position at each successive entry in
 * the archive, and the read each entry as a normal input stream
 * using read().
 *
 * @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		TarInputStream
extends		FilterInputStream
	{
	protected boolean			debug;
	protected boolean			hasHitEOF;

	protected int				entrySize;
	protected int				entryOffset;

	protected byte[]			oneBuf;
	protected byte[]			readBuf;

	protected TarBuffer			buffer;

	protected TarEntry			currEntry;

	protected EntryFactory		eFactory;


	public
	TarInputStream( InputStream is )
		{
		this( is, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE );
		}

	public
	TarInputStream( InputStream is, int blockSize )
		{
		this( is, blockSize, TarBuffer.DEFAULT_RCDSIZE );
		}

	public
	TarInputStream( InputStream is, int blockSize, int recordSize )
		{
		super( is );

		this.buffer = new TarBuffer( is, blockSize, recordSize );

		this.readBuf = null;
		this.oneBuf = new byte[1];
		this.debug = false;
		this.hasHitEOF = false;
		this.eFactory = null;
		}

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

	/**
	 * Sets the debugging flag.
	 *
	 * @param debugF True to turn on debugging.
	 */
	public void
	setEntryFactory( EntryFactory factory )
		{
		this.eFactory = factory;
		}

	/**
	 * 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 );
		}

	/**
	 * Closes this stream. Calls the TarBuffer's close() method.
	 */
	public void
	close()
		throws IOException
		{
		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();
		}

	/**
	 * Get the available data that can be read from the current
	 * entry in the archive. This does not indicate how much data
	 * is left in the entire archive, only in the current entry.
	 * This value is determined from the entry's size header field
	 * and the amount of data already read from the current entry.
	 * 
	 *
	 * @return The number of available bytes for the current entry.
	 */
	public int
	available()
		throws IOException
		{
		return this.entrySize - this.entryOffset;
		}

	/**
	 * Skip bytes in the input buffer. This skips bytes in the
	 * current entry's data, not the entire archive, and will
	 * stop at the end of the current entry's data if the number
	 * to skip extends beyond that point.
	 *
	 * @param numToSkip The number of bytes to skip.
	 */
	public void
	skip( int numToSkip )
		throws IOException
		{
		// REVIEW
		// This is horribly inefficient, but it ensures that we
		// properly skip over bytes via the TarBuffer...
		//

		byte[] skipBuf = new byte[ 8 * 1024 ];

		for ( int num = numToSkip ; num > 0 ; )
			{
			int numRead =
				this.read( skipBuf, 0,
					( num > skipBuf.length ? skipBuf.length : num ) );

			if ( numRead == -1 )
				break;

			num -= numRead;
			}
		}

	/**
	 * Since we do not support marking just yet, we return false.
	 *
	 * @return False.
	 */
	public boolean
	markSupported()
		{
		return false;
		}

	/**
	 * Since we do not support marking just yet, we do nothing.
	 *
	 * @param markLimit The limit to mark.
	 */
	public void
	mark( int markLimit )
		{
		}

	/**
	 * Since we do not support marking just yet, we do nothing.
	 */
	public void
	reset()
		{
		}

	/**
	 * Get the next entry in this tar archive. This will skip
	 * over any remaining data in the current entry, if there
	 * is one, and place the input stream at the header of the
	 * next entry, and read the header and instantiate a new
	 * TarEntry from the header bytes and return that entry.
	 * If there are no more entries in the archive, null will
	 * be returned to indicate that the end of the archive has
	 * been reached.
	 *
	 * @return The next TarEntry in the archive, or null.
	 */
	public TarEntry
	getNextEntry()
		throws IOException
		{
		if ( this.hasHitEOF )
			return null;

		if ( this.currEntry != null )
			{
			int numToSkip = this.entrySize - this.entryOffset;

			if ( this.debug )
			System.err.println
				( "TarInputStream: SKIP currENTRY '"
				+ this.currEntry.getName() + "' SZ "
				+ this.entrySize + " OFF " + this.entryOffset
				+ "  skipping " + numToSkip + " bytes" );

			if ( numToSkip > 0 )
				{
				this.skip( numToSkip );
				}

			this.readBuf = null;
			}

		byte[] headerBuf = this.buffer.readRecord();

		if ( headerBuf == null )
			{
			if ( this.debug )
				{
				System.err.println( "READ NULL RECORD" );
				}

			this.hasHitEOF = true;
			}
		else if ( this.buffer.isEOFRecord( headerBuf ) )
			{
			if ( this.debug )
				{
				System.err.println( "READ EOF RECORD" );
				}

			this.hasHitEOF = true;
			}

		if ( this.hasHitEOF )
			{
			this.currEntry = null;
			}
		else
			{
			try {
				if ( this.eFactory == null )
					{
					this.currEntry = new TarEntry( headerBuf );
					}
				else
					{
					this.currEntry =
						this.eFactory.createEntry( headerBuf );
					}

				if ( ! ( headerBuf[257] == 'u' && headerBuf[258] == 's'
						&& headerBuf[259] == 't' && headerBuf[260] == 'a'
						&& headerBuf[261] == 'r' ) )
					{
					throw new InvalidHeaderException
						( "header magic is not 'ustar', but '"
							+ headerBuf[257] + headerBuf[258] + headerBuf[259]
							+ headerBuf[260] + headerBuf[261] + "', or (dec) "
							+ ((int)headerBuf[257]) + ", "
							+ ((int)headerBuf[258]) + ", "
							+ ((int)headerBuf[259]) + ", "
							+ ((int)headerBuf[260]) + ", "
							+ ((int)headerBuf[261]) );
					}

				if ( this.debug )
				System.err.println
					( "TarInputStream: SET CURRENTRY '"
						+ this.currEntry.getName()
						+ "' size = " + this.currEntry.getSize() );

				this.entryOffset = 0;
				// REVIEW How do we resolve this discrepancy?!
				this.entrySize = (int) this.currEntry.getSize();
				}
			catch ( InvalidHeaderException ex )
				{
				this.entrySize = 0;
				this.entryOffset = 0;
				this.currEntry = null;
				throw new InvalidHeaderException
					( "bad header in block "
						+ this.buffer.getCurrentBlockNum()
						+ " record "
						+ this.buffer.getCurrentRecordNum()
						+ ", " + ex.getMessage() );
				}
			}

		return this.currEntry;
		}

	/**
	 * Reads a byte from the current tar archive entry.
	 *
	 * This method simply calls read( byte[], int, int ).
	 *
	 * @return The byte read, or -1 at EOF.
	 */
	public int
	read()
		throws IOException
		{
		int num = this.read( this.oneBuf, 0, 1 );
		if ( num == -1 )
			return num;
		else
			return this.oneBuf[0];
		}

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

	/**
	 * Reads bytes from the current tar archive entry.
	 *
	 * This method is aware of the boundaries of the current
	 * entry in the archive and will deal with them as if they
	 * were this stream's start and EOF.
	 *
	 * @param buf The buffer into which to place bytes read.
	 * @param offset The offset at which to place bytes read.
	 * @param numToRead The number of bytes to read.
	 * @return The number of bytes read, or -1 at EOF.
	 */
	public int
	read( byte[] buf, int offset, int numToRead )
		throws IOException
		{
		int totalRead = 0;

		if ( this.entryOffset >= this.entrySize )
			return -1;

		if ( (numToRead + this.entryOffset) > this.entrySize )
			{
			numToRead = (this.entrySize - this.entryOffset);
			}

		if ( this.readBuf != null )
			{
			int sz = ( numToRead > this.readBuf.length )
						? this.readBuf.length : numToRead;

			System.arraycopy( this.readBuf, 0, buf, offset, sz );

			if ( sz >= this.readBuf.length )
				{
				this.readBuf = null;
				}
			else
				{
				int newLen = this.readBuf.length - sz;
				byte[] newBuf = new byte[ newLen ];
				System.arraycopy( this.readBuf, sz, newBuf, 0, newLen );
				this.readBuf = newBuf;
				}

			totalRead += sz;
			numToRead -= sz;
			offset += sz;
			}

		for ( ; numToRead > 0 ; )
			{
			byte[] rec = this.buffer.readRecord();
			if ( rec == null )
				{
				// Unexpected EOF!
				throw new IOException
					( "unexpected EOF with " + numToRead + " bytes unread" );
				}

			int sz = numToRead;
			int recLen = rec.length;

			if ( recLen > sz )
				{
				System.arraycopy( rec, 0, buf, offset, sz );
				this.readBuf = new byte[ recLen - sz ];
				System.arraycopy( rec, sz, this.readBuf, 0, recLen - sz );
				}
			else
				{
				sz = recLen;
				System.arraycopy( rec, 0, buf, offset, recLen );
				}

			totalRead += sz;
			numToRead -= sz;
			offset += sz;
			}

		this.entryOffset += totalRead;

		return totalRead;
		}

	/**
	 * Copies the contents of the current tar archive entry directly into
	 * an output stream.
	 *
	 * @param out The OutputStream into which to write the entry's data.
	 */
	public void
	copyEntryContents( OutputStream out )
		throws IOException
		{
		byte[] buf = new byte[ 32 * 1024 ];

		for ( ; ; )
			{
			int numRead = this.read( buf, 0, buf.length );
			if ( numRead == -1 )
				break;
			out.write( buf, 0, numRead );
			}
		}

	/**
	 * This interface is provided, with the method setEntryFactory(), to allow
	 * the programmer to have their own TarEntry subclass instantiated for the
	 * entries return from getNextEntry().
	 */

	public
	interface	EntryFactory
		{
		public TarEntry
			createEntry( String name );

		public TarEntry
			createEntry( File path )
				throws InvalidHeaderException;

		public TarEntry
			createEntry( byte[] headerBuf )
				throws InvalidHeaderException;
		}

	public
	class		EntryAdapter
	implements	EntryFactory
		{
		public TarEntry
		createEntry( String name )
			{
			return new TarEntry( name );
			}

		public TarEntry
		createEntry( File path )
			throws InvalidHeaderException
			{
			return new TarEntry( path );
			}

		public TarEntry
		createEntry( byte[] headerBuf )
			throws InvalidHeaderException
			{
			return new TarEntry( headerBuf );
			}
		}

	}