001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.log4j.lf5.util; 019 020import java.io.ByteArrayOutputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.OutputStream; 024 025/** 026 * Provides utility methods for input and output streams. 027 * 028 * @author Richard Wan 029 */ 030 031// Contributed by ThoughtWorks Inc. 032 033public abstract class StreamUtils { 034 //-------------------------------------------------------------------------- 035 // Constants: 036 //-------------------------------------------------------------------------- 037 038 /** 039 * Default value is 2048. 040 */ 041 public static final int DEFAULT_BUFFER_SIZE = 2048; 042 043 //-------------------------------------------------------------------------- 044 // Protected Variables: 045 //-------------------------------------------------------------------------- 046 047 //-------------------------------------------------------------------------- 048 // Private Variables: 049 //-------------------------------------------------------------------------- 050 051 //-------------------------------------------------------------------------- 052 // Constructors: 053 //-------------------------------------------------------------------------- 054 055 //-------------------------------------------------------------------------- 056 // Public Methods: 057 //-------------------------------------------------------------------------- 058 059 /** 060 * Copies information from the input stream to the output stream using 061 * a default buffer size of 2048 bytes. 062 * @throws java.io.IOException 063 */ 064 public static void copy(InputStream input, OutputStream output) 065 throws IOException { 066 copy(input, output, DEFAULT_BUFFER_SIZE); 067 } 068 069 /** 070 * Copies information from the input stream to the output stream using 071 * the specified buffer size 072 * @throws java.io.IOException 073 */ 074 public static void copy(InputStream input, 075 OutputStream output, 076 int bufferSize) 077 throws IOException { 078 byte[] buf = new byte[bufferSize]; 079 int bytesRead = input.read(buf); 080 while (bytesRead != -1) { 081 output.write(buf, 0, bytesRead); 082 bytesRead = input.read(buf); 083 } 084 output.flush(); 085 } 086 087 /** 088 * Copies information between specified streams and then closes 089 * both of the streams. 090 * @throws java.io.IOException 091 */ 092 public static void copyThenClose(InputStream input, OutputStream output) 093 throws IOException { 094 copy(input, output); 095 input.close(); 096 output.close(); 097 } 098 099 /** 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}