|
| 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 | +/* $Id$ */ |
| 19 | + |
| 20 | +package org.apache.xmlgraphics.util.io; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.Closeable; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.OutputStream; |
| 27 | +import java.io.Reader; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility class providing convenience methods for I/O operations. |
| 31 | + */ |
| 32 | +public final class IOUtils { |
| 33 | + private IOUtils() { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Copies all bytes from the provided input stream to the given output stream. |
| 38 | + * The method operates using a buffer and is useful for transferring data |
| 39 | + * between streams without modifying the contents. |
| 40 | + * |
| 41 | + * @param input the {@code InputStream} to read data from, must not be {@code null} |
| 42 | + * @param output the {@code OutputStream} to write data to, must not be {@code null} |
| 43 | + * @return the total number of bytes copied |
| 44 | + * @throws IOException if an I/O error occurs during reading or writing |
| 45 | + */ |
| 46 | + public static long copy(InputStream input, OutputStream output) throws IOException { |
| 47 | + // TODO replace with input.transferTo(output) when Java 9+ is acceptable |
| 48 | + byte[] buffer = new byte[4096]; |
| 49 | + long count = 0; |
| 50 | + int n; |
| 51 | + while ((n = input.read(buffer)) != -1) { |
| 52 | + output.write(buffer, 0, n); |
| 53 | + count += n; |
| 54 | + } |
| 55 | + return count; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Converts the contents of the provided InputStream into a byte array. |
| 60 | + * This method reads all the data from the InputStream and writes it into |
| 61 | + * a ByteArrayOutputStream, which is then converted to a byte array. |
| 62 | + * |
| 63 | + * @param input the InputStream to read data from, must not be {@code null} |
| 64 | + * @return a byte array containing all the data read from the InputStream |
| 65 | + * @throws IOException if an I/O error occurs while reading from the InputStream |
| 66 | + */ |
| 67 | + public static byte[] toByteArray(InputStream input) throws IOException { |
| 68 | + // TODO replace with InputStream.readAllBytes when Java 9+ is acceptable |
| 69 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 70 | + copy(input, output); |
| 71 | + return output.toByteArray(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Converts the contents of the provided Reader into a String. |
| 76 | + * This method reads characters from the Reader into a buffer and then |
| 77 | + * appends them to a StringBuilder, which is returned as a String. |
| 78 | + * |
| 79 | + * @param reader the Reader to read data from, must not be null |
| 80 | + * @return a String containing all the characters read from the Reader |
| 81 | + * @throws IOException if an I/O error occurs while reading from the Reader |
| 82 | + */ |
| 83 | + public static String toString(Reader reader) throws IOException { |
| 84 | + StringBuilder sb = new StringBuilder(); |
| 85 | + char [] buf = new char[128]; |
| 86 | + int n; |
| 87 | + while ((n = reader.read(buf)) > 0) { |
| 88 | + sb.append(buf, 0, n); |
| 89 | + } |
| 90 | + return sb.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Closes a {@code Closeable} object quietly, suppressing any exceptions |
| 95 | + * that might occur during the close operation. If the provided {@code Closeable} |
| 96 | + * is {@code null}, this method does nothing. |
| 97 | + * |
| 98 | + * @param closeable the {@code Closeable} object to close; may be {@code null}. |
| 99 | + */ |
| 100 | + public static void closeQuietly(Closeable closeable) { |
| 101 | + if (closeable == null) { |
| 102 | + return; |
| 103 | + } |
| 104 | + try { |
| 105 | + closeable.close(); |
| 106 | + } catch (Exception ignore) { |
| 107 | + //Ignore |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments