001/*
002** Contributed by "Bay" <bayard@generationjava.com>
003**
004** This code has been placed into the public domain.
005*/
006
007package com.ice.tar;
008
009import java.io.ByteArrayOutputStream;
010import java.io.IOException;
011import java.io.OutputStream;
012
013import java.util.zip.GZIPOutputStream;
014
015
016// we extend TarOutputStream to have the same type, 
017// BUT, we don't use ANY methods. It's all about 
018// typing.
019
020/**
021 * Outputs tar.gz files. Added functionality that it 
022 * doesn't need to know the size of an entry. If an 
023 * entry has zero size when it is put in the Tar, then 
024 * it buffers it until it's closed and it knows the size.
025 *
026 * @author "Bay" <bayard@generationjava.com>
027 */
028
029public
030class           TarGzOutputStream
031extends         TarOutputStream
032        {
033    private TarOutputStream                     tos = null;
034    private GZIPOutputStream            gzip = null;
035    private ByteArrayOutputStream       bos = null;
036    private TarEntry                            currentEntry = null;
037
038        public
039        TarGzOutputStream( OutputStream out )
040                throws IOException
041                {
042                super( null );
043                this.gzip = new GZIPOutputStream( out );
044                this.tos = new TarOutputStream( this.gzip );
045                this.bos = new ByteArrayOutputStream();
046                }
047
048        // proxy all methods, but buffer if unknown size
049
050        public void
051        setDebug( boolean b )
052                {
053                this.tos.setDebug(b);
054                }
055
056        public void
057        setBufferDebug( boolean b )
058                {
059                this.tos.setBufferDebug(b);
060                }
061
062        public void
063        finish()
064                throws IOException
065                {
066                if ( this.currentEntry != null )
067                        {
068                        closeEntry();
069                        }
070
071                this.tos.finish();
072                }
073
074        public void
075        close()
076                throws IOException
077                {
078                this.tos.close();
079                this.gzip.finish();
080                }
081
082        public int
083        getRecordSize()
084                {
085                return this.tos.getRecordSize();
086                }
087
088        public void
089        putNextEntry(TarEntry entry)
090                throws IOException
091                {
092                if ( entry.getSize() != 0 )
093                        {
094                        this.tos.putNextEntry( entry );
095                        }
096                else
097                        {
098                        this.currentEntry = entry;
099                        }
100                }
101
102        public void
103        closeEntry()
104                throws IOException
105                {
106                if(this.currentEntry == null)
107                        {
108                        this.tos.closeEntry();
109                        }
110                else
111                        {
112                        this.currentEntry.setSize( bos.size() );
113                        this.tos.putNextEntry( this.currentEntry );
114                        this.bos.writeTo( this.tos );
115                        this.tos.closeEntry();
116                        this.currentEntry = null;
117                        this.bos = new ByteArrayOutputStream();
118                        }
119                }
120
121        public void
122        write( int b )
123                throws IOException
124                {
125                if ( this.currentEntry == null )
126                        {
127                        this.tos.write( b );
128                        }
129                else
130                        {
131                        this.bos.write( b );
132                        }
133                }
134
135        public void
136        write( byte[] b )
137                throws IOException
138                {
139                if ( this.currentEntry == null )
140                        {
141                        this.tos.write( b );
142                        }
143                else
144                        {
145                        this.bos.write( b );
146                        }
147                }
148
149        public void
150        write( byte[] b, int start, int length )
151                throws IOException
152                {
153                if ( this.currentEntry == null )
154                        {
155                        this.tos.write( b, start, length );
156                        }
157                else
158                        {
159                        this.bos.write( b, start, length );
160                        }
161                }
162
163        }