001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.utils; 020 021import java.io.ByteArrayOutputStream; 022import java.io.Closeable; 023import java.io.EOFException; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.nio.ByteBuffer; 028import java.nio.channels.ReadableByteChannel; 029 030/** 031 * Utility functions 032 * @Immutable (has mutable data but it is write-only) 033 */ 034public final class IOUtils { 035 036 private static final int COPY_BUF_SIZE = 8024; 037 private static final int SKIP_BUF_SIZE = 4096; 038 039 // This buffer does not need to be synchronised because it is write only; the contents are ignored 040 // Does not affect Immutability 041 private static final byte[] SKIP_BUF = new byte[SKIP_BUF_SIZE]; 042 043 /** Private constructor to prevent instantiation of this utility class. */ 044 private IOUtils(){ 045 } 046 047 /** 048 * Copies the content of a InputStream into an OutputStream. 049 * Uses a default buffer size of 8024 bytes. 050 * 051 * @param input 052 * the InputStream to copy 053 * @param output 054 * the target Stream 055 * @return the number of bytes copied 056 * @throws IOException 057 * if an error occurs 058 */ 059 public static long copy(final InputStream input, final OutputStream output) throws IOException { 060 return copy(input, output, COPY_BUF_SIZE); 061 } 062 063 /** 064 * Copies the content of a InputStream into an OutputStream 065 * 066 * @param input 067 * the InputStream to copy 068 * @param output 069 * the target Stream 070 * @param buffersize 071 * the buffer size to use, must be bigger than 0 072 * @return the number of bytes copied 073 * @throws IOException 074 * if an error occurs 075 * @throws IllegalArgumentException 076 * if buffersize is smaller than or equal to 0 077 */ 078 public static long copy(final InputStream input, final OutputStream output, final int buffersize) throws IOException { 079 if (buffersize < 1) { 080 throw new IllegalArgumentException("buffersize must be bigger than 0"); 081 } 082 final byte[] buffer = new byte[buffersize]; 083 int n = 0; 084 long count=0; 085 while (-1 != (n = input.read(buffer))) { 086 output.write(buffer, 0, n); 087 count += n; 088 } 089 return count; 090 } 091 092 /** 093 * Skips the given number of bytes by repeatedly invoking skip on 094 * the given input stream if necessary. 095 * 096 * <p>In a case where the stream's skip() method returns 0 before 097 * the requested number of bytes has been skip this implementation 098 * will fall back to using the read() method.</p> 099 * 100 * <p>This method will only skip less than the requested number of 101 * bytes if the end of the input stream has been reached.</p> 102 * 103 * @param input stream to skip bytes in 104 * @param numToSkip the number of bytes to skip 105 * @return the number of bytes actually skipped 106 * @throws IOException on error 107 */ 108 public static long skip(final InputStream input, long numToSkip) throws IOException { 109 final long available = numToSkip; 110 while (numToSkip > 0) { 111 final long skipped = input.skip(numToSkip); 112 if (skipped == 0) { 113 break; 114 } 115 numToSkip -= skipped; 116 } 117 118 while (numToSkip > 0) { 119 final int read = readFully(input, SKIP_BUF, 0, 120 (int) Math.min(numToSkip, SKIP_BUF_SIZE)); 121 if (read < 1) { 122 break; 123 } 124 numToSkip -= read; 125 } 126 return available - numToSkip; 127 } 128 129 /** 130 * Reads as much from input as possible to fill the given array. 131 * 132 * <p>This method may invoke read repeatedly to fill the array and 133 * only read less bytes than the length of the array if the end of 134 * the stream has been reached.</p> 135 * 136 * @param input stream to read from 137 * @param b buffer to fill 138 * @return the number of bytes actually read 139 * @throws IOException on error 140 */ 141 public static int readFully(final InputStream input, final byte[] b) throws IOException { 142 return readFully(input, b, 0, b.length); 143 } 144 145 /** 146 * Reads as much from input as possible to fill the given array 147 * with the given amount of bytes. 148 * 149 * <p>This method may invoke read repeatedly to read the bytes and 150 * only read less bytes than the requested length if the end of 151 * the stream has been reached.</p> 152 * 153 * @param input stream to read from 154 * @param b buffer to fill 155 * @param offset offset into the buffer to start filling at 156 * @param len of bytes to read 157 * @return the number of bytes actually read 158 * @throws IOException 159 * if an I/O error has occurred 160 */ 161 public static int readFully(final InputStream input, final byte[] b, final int offset, final int len) 162 throws IOException { 163 if (len < 0 || offset < 0 || len + offset > b.length) { 164 throw new IndexOutOfBoundsException(); 165 } 166 int count = 0, x = 0; 167 while (count != len) { 168 x = input.read(b, offset + count, len - count); 169 if (x == -1) { 170 break; 171 } 172 count += x; 173 } 174 return count; 175 } 176 177 /** 178 * Reads {@code b.remaining()} bytes from the given channel 179 * starting at the current channel's position. 180 * 181 * <p>This method reads repeatedly from the channel until the 182 * requested number of bytes are read. This method blocks until 183 * the requested number of bytes are read, the end of the channel 184 * is detected, or an exception is thrown.</p> 185 * 186 * @param channel the channel to read from 187 * @param b the buffer into which the data is read. 188 * @throws IOException - if an I/O error occurs. 189 * @throws EOFException - if the channel reaches the end before reading all the bytes. 190 */ 191 public static void readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException { 192 final int expectedLength = b.remaining(); 193 int read = 0; 194 while (read < expectedLength) { 195 int readNow = channel.read(b); 196 if (readNow <= 0) { 197 break; 198 } 199 read += readNow; 200 } 201 if (read < expectedLength) { 202 throw new EOFException(); 203 } 204 } 205 206 // toByteArray(InputStream) copied from: 207 // commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941 208 // January 8th, 2013 209 // 210 // Assuming our copy() works just as well as theirs! :-) 211 212 /** 213 * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>. 214 * <p> 215 * This method buffers the input internally, so there is no need to use a 216 * <code>BufferedInputStream</code>. 217 * 218 * @param input the <code>InputStream</code> to read from 219 * @return the requested byte array 220 * @throws NullPointerException if the input is null 221 * @throws IOException if an I/O error occurs 222 * @since 1.5 223 */ 224 public static byte[] toByteArray(final InputStream input) throws IOException { 225 final ByteArrayOutputStream output = new ByteArrayOutputStream(); 226 copy(input, output); 227 return output.toByteArray(); 228 } 229 230 /** 231 * Closes the given Closeable and swallows any IOException that may occur. 232 * @param c Closeable to close, can be null 233 * @since 1.7 234 */ 235 public static void closeQuietly(final Closeable c) { 236 if (c != null) { 237 try { 238 c.close(); 239 } catch (final IOException ignored) { // NOPMD 240 } 241 } 242 } 243}