1 15 16 package installer; 17 18 import java.io.*; 19 20 21 34 35 36 public 37 class TarOutputStream 38 extends FilterOutputStream 39 { 40 protected boolean debug; 41 protected int currSize; 42 protected int currBytes; 43 protected byte[] oneBuf; 44 protected byte[] recordBuf; 45 protected int assemLen; 46 protected byte[] assemBuf; 47 protected TarBuffer buffer; 48 49 50 public 51 TarOutputStream( OutputStream os ) 52 { 53 this( os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE ); 54 } 55 56 public 57 TarOutputStream( OutputStream os, int blockSize ) 58 { 59 this( os, blockSize, TarBuffer.DEFAULT_RCDSIZE ); 60 } 61 62 public 63 TarOutputStream( OutputStream os, int blockSize, int recordSize ) 64 { 65 super( os ); 66 67 this.buffer = new TarBuffer( os, blockSize, recordSize ); 68 69 this.debug = false; 70 this.assemLen = 0; 71 this.assemBuf = new byte[ recordSize ]; 72 this.recordBuf = new byte[ recordSize ]; 73 this.oneBuf = new byte[1]; 74 } 75 76 81 public void 82 setDebug( boolean debugF ) 83 { 84 this.debug = debugF; 85 } 86 87 92 public void 93 setBufferDebug( boolean debug ) 94 { 95 this.buffer.setDebug( debug ); 96 } 97 98 102 103 public void 104 finish() 105 throws IOException 106 { 107 this.writeEOFRecord(); 108 } 109 110 115 116 public void 117 close() 118 throws IOException 119 { 120 this.finish(); 121 this.buffer.close(); 122 } 123 124 129 public int 130 getRecordSize() 131 { 132 return this.buffer.getRecordSize(); 133 } 134 135 146 public void 147 putNextEntry( TarEntry entry ) 148 throws IOException 149 { 150 if ( entry.getHeader().name.length() > TarHeader.NAMELEN ) 151 throw new InvalidHeaderException 152 ( "file name '" + entry.getHeader().name 153 + "' is too long ( > " 154 + TarHeader.NAMELEN + " bytes )" ); 155 156 entry.writeEntryHeader( this.recordBuf ); 157 this.buffer.writeRecord( this.recordBuf ); 158 159 this.currBytes = 0; 160 161 if ( entry.isDirectory() ) 162 this.currSize = 0; 163 else 164 this.currSize = (int)entry.getSize(); 165 } 166 167 176 public void 177 closeEntry() 178 throws IOException 179 { 180 if ( this.assemLen > 0 ) 181 { 182 for ( int i = this.assemLen ; i < this.assemBuf.length ; ++i ) 183 this.assemBuf[i] = 0; 184 185 this.buffer.writeRecord( this.assemBuf ); 186 187 this.currBytes += this.assemLen; 188 this.assemLen = 0; 189 } 190 191 if ( this.currBytes < this.currSize ) 192 throw new IOException 193 ( "entry closed at '" + this.currBytes 194 + "' before the '" + this.currSize 195 + "' bytes specified in the header were written" ); 196 } 197 198 205 public void 206 write( int b ) 207 throws IOException 208 { 209 this.oneBuf[0] = (byte) b; 210 this.write( this.oneBuf, 0, 1 ); 211 } 212 213 221 public void 222 write( byte[] wBuf ) 223 throws IOException 224 { 225 this.write( wBuf, 0, wBuf.length ); 226 } 227 228 243 public void 244 write( byte[] wBuf, int wOffset, int numToWrite ) 245 throws IOException 246 { 247 if ( (this.currBytes + numToWrite) > this.currSize ) 248 throw new IOException 249 ( "request to write '" + numToWrite 250 + "' bytes exceeds size in header of '" 251 + this.currSize + "' bytes" ); 252 253 if ( this.assemLen > 0 ) 261 { 262 if ( (this.assemLen + numToWrite ) >= this.recordBuf.length ) 263 { 264 int aLen = this.recordBuf.length - this.assemLen; 265 266 System.arraycopy 267 ( this.assemBuf, 0, this.recordBuf, 0, this.assemLen ); 268 269 System.arraycopy 270 ( wBuf, wOffset, this.recordBuf, this.assemLen, aLen ); 271 272 this.buffer.writeRecord( this.recordBuf ); 273 274 this.currBytes += this.recordBuf.length; 275 276 wOffset += aLen; 277 numToWrite -= aLen; 278 this.assemLen = 0; 279 } 280 else { 282 System.arraycopy 283 ( wBuf, wOffset, this.assemBuf, 284 this.assemLen, numToWrite ); 285 wOffset += numToWrite; 286 this.assemLen += numToWrite; 287 numToWrite -= numToWrite; 288 } 289 } 290 291 297 for ( ; numToWrite > 0 ; ) 298 { 299 if ( numToWrite < this.recordBuf.length ) 300 { 301 System.arraycopy 302 ( wBuf, wOffset, this.assemBuf, this.assemLen, numToWrite ); 303 this.assemLen += numToWrite; 304 break; 305 } 306 307 this.buffer.writeRecord( wBuf, wOffset ); 308 309 int num = this.recordBuf.length; 310 this.currBytes += num; 311 numToWrite -= num; 312 wOffset += num; 313 } 314 } 315 316 320 private void 321 writeEOFRecord() 322 throws IOException 323 { 324 for ( int i = 0 ; i < this.recordBuf.length ; ++i ) 325 this.recordBuf[i] = 0; 326 this.buffer.writeRecord( this.recordBuf ); 327 } 328 329 } 330 331 | Popular Tags |