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 package org.apache.logging.log4j.core.util; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.io.Reader; 23 import java.io.Writer; 24 25 /** 26 * Copied from Apache Commons IO revision 1686747. 27 */ 28 public class IOUtils { 29 30 /** 31 * The default buffer size ({@value}) to use for 32 * {@link #copyLarge(InputStream, OutputStream)} 33 * and 34 * {@link #copyLarge(Reader, Writer)} 35 */ 36 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 37 38 /** 39 * Represents the end-of-file (or stream). 40 */ 41 public static final int EOF = -1; 42 43 /** 44 * Copies chars from a <code>Reader</code> to a <code>Writer</code>. 45 * <p/> 46 * This method buffers the input internally, so there is no need to use a 47 * <code>BufferedReader</code>. 48 * <p/> 49 * Large streams (over 2GB) will return a chars copied value of 50 * <code>-1</code> after the copy has completed since the correct 51 * number of chars cannot be returned as an int. For large streams 52 * use the <code>copyLarge(Reader, Writer)</code> method. 53 * 54 * @param input the <code>Reader</code> to read from 55 * @param output the <code>Writer</code> to write to 56 * @return the number of characters copied, or -1 if > Integer.MAX_VALUE 57 * @throws NullPointerException if the input or output is null 58 * @throws IOException if an I/O error occurs 59 * @since 1.1 60 */ 61 public static int copy(final Reader input, final Writer output) throws IOException { 62 final long count = copyLarge(input, output); 63 if (count > Integer.MAX_VALUE) { 64 return -1; 65 } 66 return (int) count; 67 } 68 69 /** 70 * Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>. 71 * <p/> 72 * This method buffers the input internally, so there is no need to use a 73 * <code>BufferedReader</code>. 74 * <p/> 75 * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. 76 * 77 * @param input the <code>Reader</code> to read from 78 * @param output the <code>Writer</code> to write to 79 * @return the number of characters copied 80 * @throws NullPointerException if the input or output is null 81 * @throws IOException if an I/O error occurs 82 * @since 1.3 83 */ 84 public static long copyLarge(final Reader input, final Writer output) throws IOException { 85 return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]); 86 } 87 88 /** 89 * Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>. 90 * <p/> 91 * This method uses the provided buffer, so there is no need to use a 92 * <code>BufferedReader</code>. 93 * <p/> 94 * 95 * @param input the <code>Reader</code> to read from 96 * @param output the <code>Writer</code> to write to 97 * @param buffer the buffer to be used for the copy 98 * @return the number of characters copied 99 * @throws NullPointerException if the input or output is null 100 * @throws IOException if an I/O error occurs 101 * @since 2.2 102 */ 103 public static long copyLarge(final Reader input, final Writer output, final char[] buffer) throws IOException { 104 long count = 0; 105 int n; 106 while (EOF != (n = input.read(buffer))) { 107 output.write(buffer, 0, n); 108 count += n; 109 } 110 return count; 111 } 112 113 /** 114 * Gets the contents of a <code>Reader</code> as a String. 115 * <p/> 116 * This method buffers the input internally, so there is no need to use a 117 * <code>BufferedReader</code>. 118 * 119 * @param input the <code>Reader</code> to read from 120 * @return the requested String 121 * @throws NullPointerException if the input is null 122 * @throws IOException if an I/O error occurs 123 */ 124 public static String toString(final Reader input) throws IOException { 125 final StringBuilderWriter sw = new StringBuilderWriter(); 126 copy(input, sw); 127 return sw.toString(); 128 } 129 130 }