Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.IoUtil

LineHitsNoteSource
1  /*
2   * $Id: IoUtil.java 1638 2010-09-13 19:24:53Z mgriffel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.util;
34  
35  import java.io.ByteArrayOutputStream;
36  import java.io.EOFException;
37  import java.io.File;
38  import java.io.FileInputStream;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.io.OutputStream;
43  import java.io.RandomAccessFile;
44  import java.io.Reader;
45  import java.io.StringWriter;
46  import java.io.Writer;
47  import java.net.Socket;
48  import java.nio.channels.Channel;
49  import java.util.logging.Level;
50  import java.util.logging.Logger;
51  
52  /**
53   * Collects some I/O utility functions.
54   *
55   * @author Michael Griffel
56   * @author Andreas Mandel
57   */
58  public final class IoUtil
59  {
60     /** class name for use in logger */
6175    private static final String CLASSNAME = IoUtil.class.getName();
62  
63     /** logging facility */
64100    private static final Logger logger = Logger.getLogger(CLASSNAME);
65  
66     private static final int BUFFER_SIZE = 4096;
67  
68     /**
69      * Constructor.
70      */
71     private IoUtil ()
720    {
73        // Utility class -- only static methods
740    }
75  
76     /**
77      * Closes the input stream (safe).
78      *
79      * This method tries to close the given input stream and
80      * if an IOException occurs a message with the level
81      * {@link Level#FINE} is logged. It's safe to pass a
82      * <code>null</code> reference for the argument.
83      *
84      * @param in the input stream that should be closed.
85      */
86     public static void close (InputStream in)
87     {
88100       if (in != null)
89        {
90           try
91           {
92100             in.close();
93           }
94100          catch (IOException x)
95           {
96100             logCloseFailedWarningMessage(
97                    InputStream.class, in.getClass(), x);
98100          }
99        }
100100    }
101  
102     /**
103      * Closes the output stream (safe).
104      *
105      * This method tries to close the given output stream and
106      * if an IOException occurs a message with the level
107      * {@link Level#FINE} is logged. It's safe to pass a
108      * <code>null</code> reference for the argument.
109      *
110      * @param out the output stream that should be closed.
111      */
112     public static void close (OutputStream out)
113     {
114100       if (out != null)
115        {
116           try
117           {
118100             out.close();
119           }
120100          catch (IOException x)
121           {
122100             logCloseFailedWarningMessage(
123                    OutputStream.class, out.getClass(), x);
124100          }
125        }
126100    }
127  
128     /**
129      * Closes the reader (safe).
130      *
131      * This method tries to close the given reader and if an IOException occurs
132      * a message with the level {@link Level#FINE} is logged. It's safe
133      * to pass a <code>null</code> reference for the argument.
134      *
135      * @param reader the reader that should be closed.
136      */
137     public static void close (Reader reader)
138     {
139100       if (reader != null)
140        {
141           try
142           {
1430             reader.close();
144           }
145100          catch (IOException x)
146           {
147100             logCloseFailedWarningMessage(
148                    Reader.class, reader.getClass(), x);
1490          }
150        }
151100    }
152  
153     /**
154      * Closes the RandomAccessFile (safe).
155      *
156      * This method tries to close the given RandomAccessFile and if an
157      * IOException occurs a message with the level {@link Level#FINE}
158      * is logged. It's safe to pass a <code>null</code> reference
159      * for the argument.
160      *
161      * @param randomAccessFile the randomAccessFile that should be closed.
162      */
163     public static void close (RandomAccessFile randomAccessFile)
164     {
165100       if (randomAccessFile != null)
166        {
167           try
168           {
1690             randomAccessFile.close();
170           }
171100          catch (IOException x)
172           {
17375             logCloseFailedWarningMessage(
174                    Reader.class, randomAccessFile.getClass(), x);
1750          }
176        }
177100    }
178  
179     /**
180      * Closes the writer (safe).
181      *
182      * This method tries to close the given writer and if an IOException occurs
183      * a message with the level {@link Level#FINE} is logged. It's safe
184      * to pass a <code>null</code> reference for the argument.
185      *
186      * @param writer the writer that should be closed.
187      */
188     public static void close (Writer writer)
189     {
190100       if (writer != null)
191        {
192           try
193           {
194100             writer.close();
195           }
196100          catch (IOException x)
197           {
198100             logCloseFailedWarningMessage(
199                    Writer.class, writer.getClass(), x);
200100          }
201        }
202100    }
203  
204     /**
205      * Closes the channel (safe).
206      *
207      * This method tries to close the given channel and if an IOException occurs
208      * a message with the level {@link Level#FINE} is logged. It's safe
209      * to pass a <code>null</code> reference for the argument.
210      *
211      * @param channel the channel that should be closed.
212      */
213     public static void close (Channel channel)
214     {
215100       if (channel != null)
216        {
217           try
218           {
219100             channel.close();
220           }
221100          catch (IOException x)
222           {
22375             logCloseFailedWarningMessage(Channel.class, channel.getClass(), x);
224100          }
225        }
226100    }
227  
228     /**
229      * Closes the socket (safe).
230      *
231      * This method tries to close the given socket and if an IOException occurs
232      * a message with the level {@link Level#FINE} is logged. It's safe
233      * to pass a <code>null</code> reference for the argument.
234      *
235      * @param socket the socket that should be closed.
236      */
237     public static void close (Socket socket)
238     {
2390       if (socket != null)
240        {
241           try
242           {
2430              socket.close();
244           }
2450          catch (IOException x)
246           {
2470             logCloseFailedWarningMessage(Channel.class, socket.getClass(), x);
2480          }
249        }
2500    }
251  
252     /**
253      * Reads <code>expectedLength</code> bytes from the input
254      * stream <code>in</code>.
255      *
256      * @param in the input stream to read from.
257      * @param expectedLength the expected size.
258      * @return an byte array with <code>expectedLength</code> bytes.
259      * @throws IOException in an I/O error occurs or if the read size is not the
260      *       expected size.
261      */
262     public static byte[] readFully (InputStream in, int expectedLength)
263           throws IOException
264     {
265100       final byte[] buffer = new byte[expectedLength];
266100       int pos = 0;
267100       int read = 0;
268  
269100       while ((read = in.read(buffer, pos, expectedLength - pos)) >= 0)
270        {
271100          pos += read;
272100          if (expectedLength == pos)
273           {
274100             break;
275           }
276        }
277  
278100       if (expectedLength != pos)
279        {
280100          throw new IOException(
281                 "Buffer underread. Could not read " + expectedLength
282                 + " bytes from stream. Expected was " + expectedLength
283                 + " bytes, but got only " + pos + " bytes");
284        }
285  
286100       return buffer;
287     }
288  
289     /**
290      * Reads all bytes from the input stream <code>in</code>.
291      *
292      * @param in the input stream to read from.
293      * @return an byte array with read bytes.
294      * @throws IOException in an I/O error occurs.
295      */
296     public static byte[] readFully (InputStream in)
297           throws IOException
298     {
2990       final byte[] buffer = new byte[BUFFER_SIZE];
3000       int read = 0;
3010       final ByteArrayOutputStream out = new ByteArrayOutputStream();
302  
3030       while ((read = in.read(buffer)) >= 0)
304        {
3050          out.write(buffer, 0, read);
306        }
307  
3080       return out.toByteArray();
309     }
310  
311     /**
312      * Reads all characters from the reader <code>in</code>.
313      *
314      * @param in the Reader to read from.
315      * @return an String containing the read data
316      * @throws IOException in an I/O error occurs.
317      */
318     public static String readFully (Reader in)
319           throws IOException
320     {
3210       final char[] buffer = new char[BUFFER_SIZE];
3220       int read = 0;
3230       final StringWriter out = new StringWriter();
324  
3250       while ((read = in.read(buffer)) >= 0)
326        {
3270          out.write(buffer, 0, read);
328        }
329  
3300       return out.toString();
331     }
332  
333  
334     /**
335      * Reads all characters from the reader <code>in</code> and
336      * normalizes newlines to single '\n'.
337      *
338      * @param in the Reader to read from.
339      * @return an String containing the read data
340      * @throws IOException in an I/O error occurs.
341      */
342     public static String readFullyNormalizeNewLine (Reader in)
343           throws IOException
344     {
3450       final StringWriter out = new StringWriter();
346  
347        int c;
3480       int last = 0;
3490       while ((c = in.read()) != -1)
350        {
3510          if (c == '\n')
352           {
3530              if (last != '\r')
354               {
3550                  out.write(c);
356               }
357           }
3580          else if (c == '\r' || c == '\u0085'
359               || c == '\u2028' || c == '\u2029')
360           {
3610              if (last != '\n')
362               {
3630                  out.write('\n');
364               }
365           }
366           else
367           {
3680              out.write(c);
369           }
3700          last = c;
371        }
372  
3730       return out.toString();
374     }
375  
376     /**
377      * Reads all data from in and copies it to the out stream.
378      * @param in the stream to read.
379      * @param out the stream to write.
380      * @throws IOException if an I/O issue occurs.
381      */
382     public static void copy (InputStream in, OutputStream out)
383           throws IOException
384     {
3850       final byte[] buffer = new byte[BUFFER_SIZE];
386        int read;
387  
3880       while ((read = in.read(buffer)) >= 0)
389        {
3900          out.write(buffer, 0, read);
391        }
3920    }
393  
394     /**
395      * Copies file <tt>src</tt> to file <tt>dest</tt>.
396      * @param src the source file.
397      * @param dest the destination file.
398      * @throws IOException if an I/O issue occurs.
399      */
400     public static void copy (File src, File dest)
401           throws IOException
402     {
4030       final InputStream in = new FileInputStream(src);
404        try
405        {
4060           final OutputStream out = new FileOutputStream(dest);
407            try
408            {
4090              copy(in, out);
410            }
411            finally
412            {
4130              close(out);
4140           }
415        }
416        finally
417        {
4180          close(in);
4190       }
4200    }
421     
422     /**
423      * Ensures that the given number of bytes are skipped from the given 
424      * input stream.
425      * @param in the input stream.
426      * @param bytes the number of bytes to skip.
427      * @throws IOException if the stream does not support seek,
428      *              or if some other I/O error occurs.
429      * @see InputStream#skip
430      */
431      public static void skip (InputStream in, int bytes)
432          throws IOException
433      {
434100         long remaining = bytes;
435100         while (remaining != 0)
436          {
437100             final long skipped = in.skip(remaining);
438100             if (skipped == 0)
439              {
4400                 throw new EOFException();
441              }
442100             remaining -= skipped;
443100         }
444100     }
445  
446  
447     private static void logCloseFailedWarningMessage (
448           Class resource, Class clazz, IOException x)
449     {
450100       logger.log(Level.FINE, "Error while closing " + resource.getName() + ": "
451              + clazz.getName() + ".close()", x);
452100    }
453  
454  }

Findings in this File