KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > zerog > ia > customcode > util > fileutils > OtherUtils


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.zerog.ia.customcode.util.fileutils;
5
6 import java.io.*;
7
8 public class OtherUtils
9 {
10     private static final int BUFFER_SIZE = 64 * 1024;
11     private static byte [] BUFFER = null;
12     
13     /**
14      * Create a buffer of the size indicated, and copy the InputStream to the
15      * OutputStream fully.
16      *
17      * @returns the number of bytes copied
18      */

19     public static long bufStreamCopy(InputStream is, OutputStream os, int bufferSizeInBytes )
20             throws IOException
21     {
22         byte[] b = new byte[bufferSizeInBytes];
23         
24         return bufStreamCopy( is, os, b );
25     }
26     
27     /**
28      * Copy the InputStream to the OutputStream fully, using a shared buffer.
29      *
30      * @returns the number of bytes copied
31      */

32     public static long bufStreamCopy(InputStream is, OutputStream os)
33             throws IOException
34     {
35         /* Lazily allocate this chunk. */
36         if ( BUFFER == null )
37         {
38             BUFFER = new byte[BUFFER_SIZE];
39         }
40         
41         synchronized ( BUFFER )
42         {
43             return bufStreamCopy( is, os, BUFFER );
44         }
45     }
46     
47     /**
48      * Copy the InputStream to the OutputStream fully, using the
49      * indicated buffer.
50      *
51      * @returns the number of bytes copied
52      */

53     public static long bufStreamCopy(InputStream is, OutputStream os, byte [] b )
54             throws IOException
55     {
56         long bytes = 0;
57         
58         int read = is.read( b, 0, b.length );
59         while ( read != -1 )
60         {
61             os.write( b, 0, read );
62             bytes += read;
63             read = is.read( b, 0, b.length );
64         }
65         
66         os.flush();
67         
68         return bytes;
69     }
70 }
71
Popular Tags