You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TarOutputStream.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. /*
  55. * This package is based on the work done by Timothy Gerard Endres
  56. * (time@ice.com) to whom the Ant project is very grateful for his great code.
  57. */
  58. package org.apache.tools.tar;
  59. import java.io.FilterOutputStream;
  60. import java.io.OutputStream;
  61. import java.io.IOException;
  62. /**
  63. * The TarOutputStream writes a UNIX tar archive as an OutputStream.
  64. * Methods are provided to put entries, and then write their contents
  65. * by writing to this stream using write().
  66. *
  67. * @author Timothy Gerard Endres <a href="mailto:time@ice.com">time@ice.com</a>
  68. */
  69. public class TarOutputStream extends FilterOutputStream {
  70. public final static int LONGFILE_ERROR = 0;
  71. public final static int LONGFILE_TRUNCATE = 1;
  72. public final static int LONGFILE_GNU = 2;
  73. protected boolean debug;
  74. protected int currSize;
  75. protected int currBytes;
  76. protected byte[] oneBuf;
  77. protected byte[] recordBuf;
  78. protected int assemLen;
  79. protected byte[] assemBuf;
  80. protected TarBuffer buffer;
  81. protected int longFileMode = LONGFILE_ERROR;
  82. public TarOutputStream(OutputStream os) {
  83. this(os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
  84. }
  85. public TarOutputStream(OutputStream os, int blockSize) {
  86. this(os, blockSize, TarBuffer.DEFAULT_RCDSIZE);
  87. }
  88. public TarOutputStream(OutputStream os, int blockSize, int recordSize) {
  89. super(os);
  90. this.buffer = new TarBuffer(os, blockSize, recordSize);
  91. this.debug = false;
  92. this.assemLen = 0;
  93. this.assemBuf = new byte[recordSize];
  94. this.recordBuf = new byte[recordSize];
  95. this.oneBuf = new byte[1];
  96. }
  97. public void setLongFileMode(int longFileMode) {
  98. this.longFileMode = longFileMode;
  99. }
  100. /**
  101. * Sets the debugging flag.
  102. *
  103. * @param debugF True to turn on debugging.
  104. */
  105. public void setDebug(boolean debugF) {
  106. this.debug = debugF;
  107. }
  108. /**
  109. * Sets the debugging flag in this stream's TarBuffer.
  110. *
  111. * @param debugF True to turn on debugging.
  112. */
  113. public void setBufferDebug(boolean debug) {
  114. this.buffer.setDebug(debug);
  115. }
  116. /**
  117. * Ends the TAR archive without closing the underlying OutputStream.
  118. * The result is that the EOF record of nulls is written.
  119. */
  120. public void finish() throws IOException {
  121. this.writeEOFRecord();
  122. }
  123. /**
  124. * Ends the TAR archive and closes the underlying OutputStream.
  125. * This means that finish() is called followed by calling the
  126. * TarBuffer's close().
  127. */
  128. public void close() throws IOException {
  129. this.finish();
  130. this.buffer.close();
  131. }
  132. /**
  133. * Get the record size being used by this stream's TarBuffer.
  134. *
  135. * @return The TarBuffer record size.
  136. */
  137. public int getRecordSize() {
  138. return this.buffer.getRecordSize();
  139. }
  140. /**
  141. * Put an entry on the output stream. This writes the entry's
  142. * header record and positions the output stream for writing
  143. * the contents of the entry. Once this method is called, the
  144. * stream is ready for calls to write() to write the entry's
  145. * contents. Once the contents are written, closeEntry()
  146. * <B>MUST</B> be called to ensure that all buffered data
  147. * is completely written to the output stream.
  148. *
  149. * @param entry The TarEntry to be written to the archive.
  150. */
  151. public void putNextEntry(TarEntry entry) throws IOException {
  152. if (entry.getName().length() >= TarConstants.NAMELEN) {
  153. if (longFileMode == LONGFILE_GNU) {
  154. // create a TarEntry for the LongLink, the contents
  155. // of which are the entry's name
  156. TarEntry longLinkEntry = new TarEntry(TarConstants.GNU_LONGLINK,
  157. TarConstants.LF_GNUTYPE_LONGNAME);
  158. longLinkEntry.setSize(entry.getName().length() + 1);
  159. putNextEntry(longLinkEntry);
  160. write(entry.getName().getBytes());
  161. write(0);
  162. closeEntry();
  163. }
  164. else if (longFileMode != LONGFILE_TRUNCATE) {
  165. throw new RuntimeException("file name '" + entry.getName()
  166. + "' is too long ( > "
  167. + TarConstants.NAMELEN + " bytes)");
  168. }
  169. }
  170. entry.writeEntryHeader(this.recordBuf);
  171. this.buffer.writeRecord(this.recordBuf);
  172. this.currBytes = 0;
  173. if (entry.isDirectory()) {
  174. this.currSize = 0;
  175. } else {
  176. this.currSize = (int) entry.getSize();
  177. }
  178. }
  179. /**
  180. * Close an entry. This method MUST be called for all file
  181. * entries that contain data. The reason is that we must
  182. * buffer data written to the stream in order to satisfy
  183. * the buffer's record based writes. Thus, there may be
  184. * data fragments still being assembled that must be written
  185. * to the output stream before this entry is closed and the
  186. * next entry written.
  187. */
  188. public void closeEntry() throws IOException {
  189. if (this.assemLen > 0) {
  190. for (int i = this.assemLen; i < this.assemBuf.length; ++i) {
  191. this.assemBuf[i] = 0;
  192. }
  193. this.buffer.writeRecord(this.assemBuf);
  194. this.currBytes += this.assemLen;
  195. this.assemLen = 0;
  196. }
  197. if (this.currBytes < this.currSize) {
  198. throw new IOException("entry closed at '" + this.currBytes
  199. + "' before the '" + this.currSize
  200. + "' bytes specified in the header were written");
  201. }
  202. }
  203. /**
  204. * Writes a byte to the current tar archive entry.
  205. *
  206. * This method simply calls read( byte[], int, int ).
  207. *
  208. * @param b The byte written.
  209. */
  210. public void write(int b) throws IOException {
  211. this.oneBuf[0] = (byte) b;
  212. this.write(this.oneBuf, 0, 1);
  213. }
  214. /**
  215. * Writes bytes to the current tar archive entry.
  216. *
  217. * This method simply calls write( byte[], int, int ).
  218. *
  219. * @param wBuf The buffer to write to the archive.
  220. * @return The number of bytes read, or -1 at EOF.
  221. */
  222. public void write(byte[] wBuf) throws IOException {
  223. this.write(wBuf, 0, wBuf.length);
  224. }
  225. /**
  226. * Writes bytes to the current tar archive entry. This method
  227. * is aware of the current entry and will throw an exception if
  228. * you attempt to write bytes past the length specified for the
  229. * current entry. The method is also (painfully) aware of the
  230. * record buffering required by TarBuffer, and manages buffers
  231. * that are not a multiple of recordsize in length, including
  232. * assembling records from small buffers.
  233. *
  234. * @param wBuf The buffer to write to the archive.
  235. * @param wOffset The offset in the buffer from which to get bytes.
  236. * @param numToWrite The number of bytes to write.
  237. */
  238. public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException {
  239. if ((this.currBytes + numToWrite) > this.currSize) {
  240. throw new IOException("request to write '" + numToWrite
  241. + "' bytes exceeds size in header of '"
  242. + this.currSize + "' bytes");
  243. //
  244. // We have to deal with assembly!!!
  245. // The programmer can be writing little 32 byte chunks for all
  246. // we know, and we must assemble complete records for writing.
  247. // REVIEW Maybe this should be in TarBuffer? Could that help to
  248. // eliminate some of the buffer copying.
  249. //
  250. }
  251. if (this.assemLen > 0) {
  252. if ((this.assemLen + numToWrite) >= this.recordBuf.length) {
  253. int aLen = this.recordBuf.length - this.assemLen;
  254. System.arraycopy(this.assemBuf, 0, this.recordBuf, 0,
  255. this.assemLen);
  256. System.arraycopy(wBuf, wOffset, this.recordBuf,
  257. this.assemLen, aLen);
  258. this.buffer.writeRecord(this.recordBuf);
  259. this.currBytes += this.recordBuf.length;
  260. wOffset += aLen;
  261. numToWrite -= aLen;
  262. this.assemLen = 0;
  263. } else {
  264. System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen,
  265. numToWrite);
  266. wOffset += numToWrite;
  267. this.assemLen += numToWrite;
  268. numToWrite -= numToWrite;
  269. }
  270. }
  271. //
  272. // When we get here we have EITHER:
  273. // o An empty "assemble" buffer.
  274. // o No bytes to write (numToWrite == 0)
  275. //
  276. while (numToWrite > 0) {
  277. if (numToWrite < this.recordBuf.length) {
  278. System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen,
  279. numToWrite);
  280. this.assemLen += numToWrite;
  281. break;
  282. }
  283. this.buffer.writeRecord(wBuf, wOffset);
  284. int num = this.recordBuf.length;
  285. this.currBytes += num;
  286. numToWrite -= num;
  287. wOffset += num;
  288. }
  289. }
  290. /**
  291. * Write an EOF (end of archive) record to the tar archive.
  292. * An EOF record consists of a record of all zeros.
  293. */
  294. private void writeEOFRecord() throws IOException {
  295. for (int i = 0; i < this.recordBuf.length; ++i) {
  296. this.recordBuf[i] = 0;
  297. }
  298. this.buffer.writeRecord(this.recordBuf);
  299. }
  300. }