KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > OutputFiller


1 package com.openedit.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8
9 /**
10  * Creation date: (9/11/2001 4:43:00 PM)
11  * @author: CSB
12  */

13 public class OutputFiller
14 {
15     protected int fieldBufferSize = 1024;
16
17     /**
18      * InputFlusher constructor comment.
19      */

20     public OutputFiller()
21     {
22         super();
23     }
24
25     /**
26      * Creation date: (9/11/2001 4:55:34 PM)
27      * @param inBufferSize int
28      */

29     public OutputFiller(int inBufferSize)
30     {
31         setBufferSize(inBufferSize);
32     }
33
34     public void fill(java.io.InputStream JavaDoc in, java.io.OutputStream JavaDoc out) throws java.io.IOException JavaDoc
35     {
36         byte[] bytes = new byte[getBufferSize()];
37
38         int iRead = -1;
39
40         while (true)
41         {
42             iRead = in.read(bytes);
43
44             if (iRead != -1)
45             {
46                 out.write(bytes, 0, iRead);
47             }
48             else
49             {
50                 break;
51             }
52         }
53         out.flush();
54     }
55
56     public void fill(java.io.Reader JavaDoc in, java.io.Writer JavaDoc out) throws java.io.IOException JavaDoc
57     {
58         char[] bytes = new char[getBufferSize()];
59
60         int iRead = -1;
61
62         while (true)
63         {
64             iRead = in.read(bytes);
65
66             if (iRead != -1)
67             {
68                 out.write(bytes, 0, iRead);
69             }
70             else
71             {
72                 break;
73             }
74
75         }
76         out.flush();
77     }
78
79     public void fill(File JavaDoc inSource, File JavaDoc inOut) throws IOException JavaDoc
80     {
81         FileInputStream JavaDoc in = null;
82         try
83         {
84             in = new FileInputStream JavaDoc(inSource);
85             inOut.getParentFile().mkdirs();
86             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(inOut);
87             try
88             {
89                 fill(in, out);
90             }
91             finally
92             {
93                 FileUtils.safeClose(out);
94             }
95         }
96         finally
97         {
98             FileUtils.safeClose(in);
99         }
100     }
101
102     /**
103      * Creation date: (9/11/2001 4:53:38 PM)
104      * @return int
105      */

106     public int getBufferSize()
107     {
108         return fieldBufferSize;
109     }
110
111     /**
112      * Creation date: (9/11/2001 4:53:38 PM)
113      * @param newBufferSize int
114      */

115     public void setBufferSize(int newBufferSize)
116     {
117         fieldBufferSize = newBufferSize;
118     }
119
120     /**
121      * @param inIn
122      */

123     public void close(InputStream JavaDoc inIn)
124     {
125         if ( inIn != null)
126         {
127             try
128             {
129                 inIn.close();
130             } catch (IOException JavaDoc ex)
131             {
132                 //fail silently
133
}
134         }
135     }
136 }
Popular Tags