Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StreamUtil |
|
| 2.0;2 |
1 | package liquibase.util; | |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
5 | import java.io.InputStreamReader; | |
6 | import java.io.OutputStream; | |
7 | import java.io.Reader; | |
8 | ||
9 | /** | |
10 | * Utilities for working with streams. | |
11 | */ | |
12 | 0 | public class StreamUtil { |
13 | ||
14 | 1 | final public static String lineSeparator = System.getProperty("line.separator"); |
15 | ||
16 | public static String getLineSeparator() { | |
17 | 3 | return lineSeparator; |
18 | } | |
19 | ||
20 | /** | |
21 | * Reads a stream until the end of file into a String and uses the machines default encoding to convert to | |
22 | * characters the bytes from the Stream. | |
23 | * | |
24 | * @param ins | |
25 | * The InputStream to read. | |
26 | * @return The contents of the input stream as a String | |
27 | * @throws IOException | |
28 | * If there is an error reading the stream. | |
29 | */ | |
30 | public static String getStreamContents(InputStream ins) throws IOException { | |
31 | ||
32 | 1 | InputStreamReader reader = new InputStreamReader(ins); |
33 | 1 | return getReaderContents(reader); |
34 | } | |
35 | ||
36 | /** | |
37 | * Reads a stream until the end of file into a String and uses the machines default encoding to convert to | |
38 | * characters the bytes from the Stream. | |
39 | * | |
40 | * @param ins | |
41 | * The InputStream to read. | |
42 | * @param charsetName | |
43 | * The name of a supported {@link java.nio.charset.Charset </code>charset<code>} | |
44 | * @return The contents of the input stream as a String | |
45 | * @throws IOException | |
46 | * If there is an error reading the stream. | |
47 | */ | |
48 | public static String getStreamContents(InputStream ins, String charsetName) throws IOException { | |
49 | ||
50 | 10 | InputStreamReader reader = (charsetName != null) ? new InputStreamReader(ins, charsetName) |
51 | : new InputStreamReader(ins); | |
52 | 10 | return getReaderContents(reader); |
53 | } | |
54 | ||
55 | /** | |
56 | * Reads all the characters into a String. | |
57 | * | |
58 | * @param reader | |
59 | * The Reader to read. | |
60 | * @return The contents of the input stream as a String | |
61 | * @throws IOException | |
62 | * If there is an error reading the stream. | |
63 | */ | |
64 | public static String getReaderContents(Reader reader) throws IOException { | |
65 | try { | |
66 | 12 | StringBuffer result = new StringBuffer(); |
67 | ||
68 | 12 | char[] buffer = new char[2048]; |
69 | int read; | |
70 | 24 | while ((read = reader.read(buffer)) > -1) { |
71 | 12 | result.append(buffer, 0, read); |
72 | } | |
73 | 12 | return result.toString(); |
74 | } finally { | |
75 | 0 | try { |
76 | 12 | reader.close(); |
77 | 0 | } catch (IOException ioe) { // NOPMD |
78 | // can safely ignore | |
79 | 24 | } |
80 | } | |
81 | } | |
82 | ||
83 | public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException { | |
84 | 0 | byte[] bytes = new byte[1024]; |
85 | 0 | int r = inputStream.read(bytes); |
86 | 0 | while (r > 0) { |
87 | 0 | outputStream.write(bytes, 0, r); |
88 | 0 | r = inputStream.read(bytes); |
89 | } | |
90 | 0 | } |
91 | } |