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 org.apache.log4j.lf5.util; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.OutputStream; 24 25 /** 26 * Provides utility methods for input and output streams. 27 * 28 * @author Richard Wan 29 */ 30 31 // Contributed by ThoughtWorks Inc. 32 33 public abstract class StreamUtils { 34 //-------------------------------------------------------------------------- 35 // Constants: 36 //-------------------------------------------------------------------------- 37 38 /** 39 * Default value is 2048. 40 */ 41 public static final int DEFAULT_BUFFER_SIZE = 2048; 42 43 //-------------------------------------------------------------------------- 44 // Protected Variables: 45 //-------------------------------------------------------------------------- 46 47 //-------------------------------------------------------------------------- 48 // Private Variables: 49 //-------------------------------------------------------------------------- 50 51 //-------------------------------------------------------------------------- 52 // Constructors: 53 //-------------------------------------------------------------------------- 54 55 //-------------------------------------------------------------------------- 56 // Public Methods: 57 //-------------------------------------------------------------------------- 58 59 /** 60 * Copies information from the input stream to the output stream using 61 * a default buffer size of 2048 bytes. 62 * @throws java.io.IOException 63 */ 64 public static void copy(InputStream input, OutputStream output) 65 throws IOException { 66 copy(input, output, DEFAULT_BUFFER_SIZE); 67 } 68 69 /** 70 * Copies information from the input stream to the output stream using 71 * the specified buffer size 72 * @throws java.io.IOException 73 */ 74 public static void copy(InputStream input, 75 OutputStream output, 76 int bufferSize) 77 throws IOException { 78 byte[] buf = new byte[bufferSize]; 79 int bytesRead = input.read(buf); 80 while (bytesRead != -1) { 81 output.write(buf, 0, bytesRead); 82 bytesRead = input.read(buf); 83 } 84 output.flush(); 85 } 86 87 /** 88 * Copies information between specified streams and then closes 89 * both of the streams. 90 * @throws java.io.IOException 91 */ 92 public static void copyThenClose(InputStream input, OutputStream output) 93 throws IOException { 94 copy(input, output); 95 input.close(); 96 output.close(); 97 } 98 99 /** 100 * @return a byte[] containing the information contained in the 101 * specified InputStream. 102 * @throws java.io.IOException 103 */ 104 public static byte[] getBytes(InputStream input) 105 throws IOException { 106 ByteArrayOutputStream result = new ByteArrayOutputStream(); 107 copy(input, result); 108 result.close(); 109 return result.toByteArray(); 110 } 111 112 //-------------------------------------------------------------------------- 113 // Protected Methods: 114 //-------------------------------------------------------------------------- 115 116 //-------------------------------------------------------------------------- 117 // Private Methods: 118 //-------------------------------------------------------------------------- 119 120 //-------------------------------------------------------------------------- 121 // Nested Top-Level Classes or Interfaces 122 //-------------------------------------------------------------------------- 123 124 }