| 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 | |
|
| 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 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 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 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 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 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 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) { |
| 78 | |
|
| 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 | |
} |