View Javadoc

1   /*
2    * Copyright (c) 2001 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version
8    * 
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  package org.backsource.utils.io;
19  import java.io.*;
20  /***
21   * A simple IO helper that has bean arround some time.
22   *
23   * @author Peter Antman
24   * @version
25   */
26  
27  public class IOHelper  {
28     /***
29      * Totally static class.
30      */
31      private IOHelper() {
32  	
33      }
34  
35      /***
36       * Connect an is and os, when the method returns the stuff will have
37       * been written to the os, be it a file or a byte[]. Its is up to the
38       * user to close() the streams, the os will have been flushed
39       */
40      public static void connect(InputStream is, OutputStream os) throws IOException {
41  	byte[] buff = new byte[1024];
42          for(;;) {
43              int size = is.read( buff );
44              if (size == -1 ) {
45                  break;
46              }
47              os.write(buff, 0, size);
48          }
49  	os.flush();
50      }
51      
52      /***
53       * Connect an reader and writer, when the method returns the stuff will have
54       * been written to the writer, be it a file or a char[]. Its is up to the
55       * user to close() the reader and writer, the  writer will have been flushed
56       */
57      public static void connect(Reader reader, Writer writer) throws IOException {
58  	char[] buff = new char[1024];
59          for(;;) {
60              int size = reader.read( buff );
61              if (size == -1 ) {
62                  break;
63              }
64              writer.write(buff, 0, size);
65          }
66  	writer.flush();
67      }
68      
69      
70  } // IOHelper