• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package tests.support;
19 
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Reader;
24 import java.io.StringWriter;
25 
26 /**
27  * Utility methods for working with byte and character streams.
28  */
29 public class Streams {
Streams()30     private Streams() {
31     }
32 
33     /**
34      * Drains the stream into a byte array and returns the result.
35      */
streamToBytes(InputStream source)36     public static byte[] streamToBytes(InputStream source) throws IOException {
37         byte[] buffer = new byte[1024];
38         ByteArrayOutputStream out = new ByteArrayOutputStream();
39         int count;
40         while ((count = source.read(buffer)) != -1) {
41             out.write(buffer, 0, count);
42         }
43         return out.toByteArray();
44     }
45 
46     /**
47      * Drains the stream into a string and returns the result.
48      */
streamToString(Reader fileReader)49     public static String streamToString(Reader fileReader) throws IOException {
50         char[] buffer = new char[1024];
51         StringWriter out = new StringWriter();
52         int count;
53         while ((count = fileReader.read(buffer)) != -1) {
54             out.write(buffer, 0, count);
55         }
56         return out.toString();
57     }
58 }
59