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  package org.portletbridge.portlet;
17  
18  import java.io.BufferedOutputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  /***
25   * @author jmccrindle
26   */
27  public class ResourceUtil {
28  
29      private ResourceUtil() {
30          super();
31      }
32  
33      public static void copy(InputStream inputStream, OutputStream outputStream,
34              int bufSize) throws IOException {
35          InputStream in = inputStream;
36          BufferedOutputStream out = new BufferedOutputStream(outputStream,
37                  bufSize);
38          try {
39              byte[] b = new byte[4096];
40              int i = -1;
41              while ((i = in.read(b)) != -1) {
42                  out.write(b, 0, i);
43              }
44              out.flush();
45          } finally {
46              in.close();
47          }
48      }
49      
50      public static String getString(InputStream in, String charSet) throws IOException {
51          ByteArrayOutputStream out = new ByteArrayOutputStream();
52          copy(in, out, 4096);
53          return new String(out.toByteArray(), charSet);
54      }
55  }