View Javadoc

1   /*
2    * Copyright 2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.commons.jjar;
19  
20  import java.lang.Package;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.Collection;
25  import java.util.Map;
26  import java.util.List;
27  import java.util.StringTokenizer;
28  import java.util.jar.JarFile;
29  import java.util.jar.Manifest;
30  import java.util.jar.Attributes;
31  import java.util.jar.JarInputStream;
32  
33  import java.io.File;
34  import java.io.FileOutputStream;
35  import java.io.BufferedInputStream;
36  import java.io.BufferedOutputStream;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  
40  import java.net.JarURLConnection;
41  import java.net.URLConnection;
42  import java.net.URL;
43  
44  
45  /**
46   *  bit moving  support for JJAR
47   * 
48   *  @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
49   *  @version $Id: Transport.java 155454 2005-02-26 13:23:34Z dirkv $
50   */
51  public class Transport
52  {
53      public static boolean fetchJar( URLConnection source, String dest )
54      {
55          BufferedInputStream bis = null;
56          BufferedOutputStream bos = null;
57  
58          try
59          {
60              bis = new BufferedInputStream( source.getInputStream());
61              bos = new BufferedOutputStream( new FileOutputStream( dest ) );
62         
63              return fetch( bis, bos );
64          }
65          catch( Exception e )
66          {
67              System.out.println("Transport.fetchJar() :  " + e );
68          }
69          finally
70          {
71              try{
72                  if (bos != null)
73                      bos.close();
74              }catch(Exception e){}
75          }
76  
77          return false;
78      }
79  
80      private static boolean fetch( InputStream is, OutputStream os )
81          throws Exception
82      {
83          byte[] buf = new byte[ 1024];
84          int count = 0;
85          
86          do 
87          {
88              os.write( buf, 0, count);
89              count = is.read(buf, 0, buf.length);
90          } while (count != -1);
91  
92          return true;
93      }
94  }