Source Code Cross Referenced for TarOutputStream.java in  » Build » ANT » org » apache » tools » tar » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.tar 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         *
017:         */
018:
019:        /*
020:         * This package is based on the work done by Timothy Gerard Endres
021:         * (time@ice.com) to whom the Ant project is very grateful for his great code.
022:         */
023:
024:        package org.apache.tools.tar;
025:
026:        import java.io.FilterOutputStream;
027:        import java.io.OutputStream;
028:        import java.io.IOException;
029:
030:        /**
031:         * The TarOutputStream writes a UNIX tar archive as an OutputStream.
032:         * Methods are provided to put entries, and then write their contents
033:         * by writing to this stream using write().
034:         *
035:         */
036:        public class TarOutputStream extends FilterOutputStream {
037:            /** Fail if a long file name is required in the archive. */
038:            public static final int LONGFILE_ERROR = 0;
039:
040:            /** Long paths will be truncated in the archive. */
041:            public static final int LONGFILE_TRUNCATE = 1;
042:
043:            /** GNU tar extensions are used to store long file names in the archive. */
044:            public static final int LONGFILE_GNU = 2;
045:
046:            // CheckStyle:VisibilityModifier OFF - bc
047:            protected boolean debug;
048:            protected long currSize;
049:            protected String currName;
050:            protected long currBytes;
051:            protected byte[] oneBuf;
052:            protected byte[] recordBuf;
053:            protected int assemLen;
054:            protected byte[] assemBuf;
055:            protected TarBuffer buffer;
056:            protected int longFileMode = LONGFILE_ERROR;
057:            // CheckStyle:VisibilityModifier ON
058:
059:            private boolean closed = false;
060:
061:            /**
062:             * Constructor for TarInputStream.
063:             * @param os the output stream to use
064:             */
065:            public TarOutputStream(OutputStream os) {
066:                this (os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
067:            }
068:
069:            /**
070:             * Constructor for TarInputStream.
071:             * @param os the output stream to use
072:             * @param blockSize the block size to use
073:             */
074:            public TarOutputStream(OutputStream os, int blockSize) {
075:                this (os, blockSize, TarBuffer.DEFAULT_RCDSIZE);
076:            }
077:
078:            /**
079:             * Constructor for TarInputStream.
080:             * @param os the output stream to use
081:             * @param blockSize the block size to use
082:             * @param recordSize the record size to use
083:             */
084:            public TarOutputStream(OutputStream os, int blockSize,
085:                    int recordSize) {
086:                super (os);
087:
088:                this .buffer = new TarBuffer(os, blockSize, recordSize);
089:                this .debug = false;
090:                this .assemLen = 0;
091:                this .assemBuf = new byte[recordSize];
092:                this .recordBuf = new byte[recordSize];
093:                this .oneBuf = new byte[1];
094:            }
095:
096:            /**
097:             * Set the long file mode.
098:             * This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or LONGFILE_GNU(2).
099:             * This specifies the treatment of long file names (names >= TarConstants.NAMELEN).
100:             * Default is LONGFILE_ERROR.
101:             * @param longFileMode the mode to use
102:             */
103:            public void setLongFileMode(int longFileMode) {
104:                this .longFileMode = longFileMode;
105:            }
106:
107:            /**
108:             * Sets the debugging flag.
109:             *
110:             * @param debugF True to turn on debugging.
111:             */
112:            public void setDebug(boolean debugF) {
113:                this .debug = debugF;
114:            }
115:
116:            /**
117:             * Sets the debugging flag in this stream's TarBuffer.
118:             *
119:             * @param debug True to turn on debugging.
120:             */
121:            public void setBufferDebug(boolean debug) {
122:                this .buffer.setDebug(debug);
123:            }
124:
125:            /**
126:             * Ends the TAR archive without closing the underlying OutputStream.
127:             * The result is that the two EOF records of nulls are written.
128:             * @throws IOException on error
129:             */
130:            public void finish() throws IOException {
131:                // See Bugzilla 28776 for a discussion on this
132:                // http://issues.apache.org/bugzilla/show_bug.cgi?id=28776
133:                this .writeEOFRecord();
134:                this .writeEOFRecord();
135:            }
136:
137:            /**
138:             * Ends the TAR archive and closes the underlying OutputStream.
139:             * This means that finish() is called followed by calling the
140:             * TarBuffer's close().
141:             * @throws IOException on error
142:             */
143:            public void close() throws IOException {
144:                if (!closed) {
145:                    this .finish();
146:                    this .buffer.close();
147:                    out.close();
148:                    closed = true;
149:                }
150:            }
151:
152:            /**
153:             * Get the record size being used by this stream's TarBuffer.
154:             *
155:             * @return The TarBuffer record size.
156:             */
157:            public int getRecordSize() {
158:                return this .buffer.getRecordSize();
159:            }
160:
161:            /**
162:             * Put an entry on the output stream. This writes the entry's
163:             * header record and positions the output stream for writing
164:             * the contents of the entry. Once this method is called, the
165:             * stream is ready for calls to write() to write the entry's
166:             * contents. Once the contents are written, closeEntry()
167:             * <B>MUST</B> be called to ensure that all buffered data
168:             * is completely written to the output stream.
169:             *
170:             * @param entry The TarEntry to be written to the archive.
171:             * @throws IOException on error
172:             */
173:            public void putNextEntry(TarEntry entry) throws IOException {
174:                if (entry.getName().length() >= TarConstants.NAMELEN) {
175:
176:                    if (longFileMode == LONGFILE_GNU) {
177:                        // create a TarEntry for the LongLink, the contents
178:                        // of which are the entry's name
179:                        TarEntry longLinkEntry = new TarEntry(
180:                                TarConstants.GNU_LONGLINK,
181:                                TarConstants.LF_GNUTYPE_LONGNAME);
182:
183:                        longLinkEntry.setSize(entry.getName().length() + 1);
184:                        putNextEntry(longLinkEntry);
185:                        write(entry.getName().getBytes());
186:                        write(0);
187:                        closeEntry();
188:                    } else if (longFileMode != LONGFILE_TRUNCATE) {
189:                        throw new RuntimeException("file name '"
190:                                + entry.getName() + "' is too long ( > "
191:                                + TarConstants.NAMELEN + " bytes)");
192:                    }
193:                }
194:
195:                entry.writeEntryHeader(this .recordBuf);
196:                this .buffer.writeRecord(this .recordBuf);
197:
198:                this .currBytes = 0;
199:
200:                if (entry.isDirectory()) {
201:                    this .currSize = 0;
202:                } else {
203:                    this .currSize = entry.getSize();
204:                }
205:                currName = entry.getName();
206:            }
207:
208:            /**
209:             * Close an entry. This method MUST be called for all file
210:             * entries that contain data. The reason is that we must
211:             * buffer data written to the stream in order to satisfy
212:             * the buffer's record based writes. Thus, there may be
213:             * data fragments still being assembled that must be written
214:             * to the output stream before this entry is closed and the
215:             * next entry written.
216:             * @throws IOException on error
217:             */
218:            public void closeEntry() throws IOException {
219:                if (this .assemLen > 0) {
220:                    for (int i = this .assemLen; i < this .assemBuf.length; ++i) {
221:                        this .assemBuf[i] = 0;
222:                    }
223:
224:                    this .buffer.writeRecord(this .assemBuf);
225:
226:                    this .currBytes += this .assemLen;
227:                    this .assemLen = 0;
228:                }
229:
230:                if (this .currBytes < this .currSize) {
231:                    throw new IOException("entry '" + currName
232:                            + "' closed at '" + this .currBytes
233:                            + "' before the '" + this .currSize
234:                            + "' bytes specified in the header were written");
235:                }
236:            }
237:
238:            /**
239:             * Writes a byte to the current tar archive entry.
240:             *
241:             * This method simply calls read( byte[], int, int ).
242:             *
243:             * @param b The byte written.
244:             * @throws IOException on error
245:             */
246:            public void write(int b) throws IOException {
247:                this .oneBuf[0] = (byte) b;
248:
249:                this .write(this .oneBuf, 0, 1);
250:            }
251:
252:            /**
253:             * Writes bytes to the current tar archive entry.
254:             *
255:             * This method simply calls write( byte[], int, int ).
256:             *
257:             * @param wBuf The buffer to write to the archive.
258:             * @throws IOException on error
259:             */
260:            public void write(byte[] wBuf) throws IOException {
261:                this .write(wBuf, 0, wBuf.length);
262:            }
263:
264:            /**
265:             * Writes bytes to the current tar archive entry. This method
266:             * is aware of the current entry and will throw an exception if
267:             * you attempt to write bytes past the length specified for the
268:             * current entry. The method is also (painfully) aware of the
269:             * record buffering required by TarBuffer, and manages buffers
270:             * that are not a multiple of recordsize in length, including
271:             * assembling records from small buffers.
272:             *
273:             * @param wBuf The buffer to write to the archive.
274:             * @param wOffset The offset in the buffer from which to get bytes.
275:             * @param numToWrite The number of bytes to write.
276:             * @throws IOException on error
277:             */
278:            public void write(byte[] wBuf, int wOffset, int numToWrite)
279:                    throws IOException {
280:                if ((this .currBytes + numToWrite) > this .currSize) {
281:                    throw new IOException("request to write '" + numToWrite
282:                            + "' bytes exceeds size in header of '"
283:                            + this .currSize + "' bytes for entry '" + currName
284:                            + "'");
285:
286:                    //
287:                    // We have to deal with assembly!!!
288:                    // The programmer can be writing little 32 byte chunks for all
289:                    // we know, and we must assemble complete records for writing.
290:                    // REVIEW Maybe this should be in TarBuffer? Could that help to
291:                    // eliminate some of the buffer copying.
292:                    //
293:                }
294:
295:                if (this .assemLen > 0) {
296:                    if ((this .assemLen + numToWrite) >= this .recordBuf.length) {
297:                        int aLen = this .recordBuf.length - this .assemLen;
298:
299:                        System.arraycopy(this .assemBuf, 0, this .recordBuf, 0,
300:                                this .assemLen);
301:                        System.arraycopy(wBuf, wOffset, this .recordBuf,
302:                                this .assemLen, aLen);
303:                        this .buffer.writeRecord(this .recordBuf);
304:
305:                        this .currBytes += this .recordBuf.length;
306:                        wOffset += aLen;
307:                        numToWrite -= aLen;
308:                        this .assemLen = 0;
309:                    } else {
310:                        System.arraycopy(wBuf, wOffset, this .assemBuf,
311:                                this .assemLen, numToWrite);
312:
313:                        wOffset += numToWrite;
314:                        this .assemLen += numToWrite;
315:                        numToWrite -= numToWrite;
316:                    }
317:                }
318:
319:                //
320:                // When we get here we have EITHER:
321:                // o An empty "assemble" buffer.
322:                // o No bytes to write (numToWrite == 0)
323:                //
324:                while (numToWrite > 0) {
325:                    if (numToWrite < this .recordBuf.length) {
326:                        System.arraycopy(wBuf, wOffset, this .assemBuf,
327:                                this .assemLen, numToWrite);
328:
329:                        this .assemLen += numToWrite;
330:
331:                        break;
332:                    }
333:
334:                    this .buffer.writeRecord(wBuf, wOffset);
335:
336:                    int num = this .recordBuf.length;
337:
338:                    this .currBytes += num;
339:                    numToWrite -= num;
340:                    wOffset += num;
341:                }
342:            }
343:
344:            /**
345:             * Write an EOF (end of archive) record to the tar archive.
346:             * An EOF record consists of a record of all zeros.
347:             */
348:            private void writeEOFRecord() throws IOException {
349:                for (int i = 0; i < this .recordBuf.length; ++i) {
350:                    this .recordBuf[i] = 0;
351:                }
352:
353:                this.buffer.writeRecord(this.recordBuf);
354:            }
355:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.