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.File;
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.OutputStream;
028import java.nio.Buffer;
029import java.nio.ByteBuffer;
030import java.nio.channels.ReadableByteChannel;
031import java.nio.file.Files;
032import java.nio.file.LinkOption;
033
034/**
035 * Utility functions
036 * @Immutable (has mutable data but it is write-only)
037 */
038public final class IOUtils {
039
040    private static final int COPY_BUF_SIZE = 8024;
041    private static final int SKIP_BUF_SIZE = 4096;
042
043    /**
044     * Empty array of of type {@link LinkOption}.
045     *
046     * @since 1.21
047     */
048    public static final LinkOption[] EMPTY_LINK_OPTIONS = {};
049
050    // This buffer does not need to be synchronized because it is write only; the contents are ignored
051    // Does not affect Immutability
052    private static final byte[] SKIP_BUF = new byte[SKIP_BUF_SIZE];
053
054    /** Private constructor to prevent instantiation of this utility class. */
055    private IOUtils(){
056    }
057
058    /**
059     * Copies the content of a InputStream into an OutputStream.
060     * Uses a default buffer size of 8024 bytes.
061     *
062     * @param input
063     *            the InputStream to copy
064     * @param output
065     *            the target, may be null to simulate output to dev/null on Linux and NUL on Windows
066     * @return the number of bytes copied
067     * @throws IOException
068     *             if an error occurs
069     */
070    public static long copy(final InputStream input, final OutputStream output) throws IOException {
071        return copy(input, output, COPY_BUF_SIZE);
072    }
073
074    /**
075     * Copies the content of a InputStream into an OutputStream
076     *
077     * @param input
078     *            the InputStream to copy
079     * @param output
080     *            the target, may be null to simulate output to dev/null on Linux and NUL on Windows
081     * @param buffersize
082     *            the buffer size to use, must be bigger than 0
083     * @return the number of bytes copied
084     * @throws IOException
085     *             if an error occurs
086     * @throws IllegalArgumentException
087     *             if buffersize is smaller than or equal to 0
088     */
089    public static long copy(final InputStream input, final OutputStream output, final int buffersize) throws IOException {
090        if (buffersize < 1) {
091            throw new IllegalArgumentException("buffersize must be bigger than 0");
092        }
093        final byte[] buffer = new byte[buffersize];
094        int n = 0;
095        long count=0;
096        while (-1 != (n = input.read(buffer))) {
097            if (output != null) {
098                output.write(buffer, 0, n);
099            }
100            count += n;
101        }
102        return count;
103    }
104
105    /**
106     * Skips the given number of bytes by repeatedly invoking skip on
107     * the given input stream if necessary.
108     *
109     * <p>In a case where the stream's skip() method returns 0 before
110     * the requested number of bytes has been skip this implementation
111     * will fall back to using the read() method.</p>
112     *
113     * <p>This method will only skip less than the requested number of
114     * bytes if the end of the input stream has been reached.</p>
115     *
116     * @param input stream to skip bytes in
117     * @param numToSkip the number of bytes to skip
118     * @return the number of bytes actually skipped
119     * @throws IOException on error
120     */
121    public static long skip(final InputStream input, long numToSkip) throws IOException {
122        final long available = numToSkip;
123        while (numToSkip > 0) {
124            final long skipped = input.skip(numToSkip);
125            if (skipped == 0) {
126                break;
127            }
128            numToSkip -= skipped;
129        }
130
131        while (numToSkip > 0) {
132            final int read = readFully(input, SKIP_BUF, 0,
133                                 (int) Math.min(numToSkip, SKIP_BUF_SIZE));
134            if (read < 1) {
135                break;
136            }
137            numToSkip -= read;
138        }
139        return available - numToSkip;
140    }
141
142    /**
143     * Reads as much from the file as possible to fill the given array.
144     *
145     * <p>This method may invoke read repeatedly to fill the array and
146     * only read less bytes than the length of the array if the end of
147     * the stream has been reached.</p>
148     *
149     * @param file file to read
150     * @param array buffer to fill
151     * @return the number of bytes actually read
152     * @throws IOException on error
153     * @since 1.20
154     */
155    public static int read(final File file, final byte[] array) throws IOException {
156        try (InputStream inputStream = Files.newInputStream(file.toPath())) {
157            return readFully(inputStream, array, 0, array.length);
158        }
159    }
160
161    /**
162     * Reads as much from input as possible to fill the given array.
163     *
164     * <p>This method may invoke read repeatedly to fill the array and
165     * only read less bytes than the length of the array if the end of
166     * the stream has been reached.</p>
167     *
168     * @param input stream to read from
169     * @param array buffer to fill
170     * @return the number of bytes actually read
171     * @throws IOException on error
172     */
173    public static int readFully(final InputStream input, final byte[] array) throws IOException {
174        return readFully(input, array, 0, array.length);
175    }
176
177    /**
178     * Reads as much from input as possible to fill the given array
179     * with the given amount of bytes.
180     *
181     * <p>This method may invoke read repeatedly to read the bytes and
182     * only read less bytes than the requested length if the end of
183     * the stream has been reached.</p>
184     *
185     * @param input stream to read from
186     * @param array buffer to fill
187     * @param offset offset into the buffer to start filling at
188     * @param len of bytes to read
189     * @return the number of bytes actually read
190     * @throws IOException
191     *             if an I/O error has occurred
192     */
193    public static int readFully(final InputStream input, final byte[] array, final int offset, final int len)
194        throws IOException {
195        if (len < 0 || offset < 0 || len + offset > array.length || len + offset < 0) {
196            throw new IndexOutOfBoundsException();
197        }
198        int count = 0, x = 0;
199        while (count != len) {
200            x = input.read(array, offset + count, len - count);
201            if (x == -1) {
202                break;
203            }
204            count += x;
205        }
206        return count;
207    }
208
209    /**
210     * Reads {@code b.remaining()} bytes from the given channel
211     * starting at the current channel's position.
212     *
213     * <p>This method reads repeatedly from the channel until the
214     * requested number of bytes are read. This method blocks until
215     * the requested number of bytes are read, the end of the channel
216     * is detected, or an exception is thrown.</p>
217     *
218     * @param channel the channel to read from
219     * @param byteBuffer the buffer into which the data is read.
220     * @throws IOException - if an I/O error occurs.
221     * @throws EOFException - if the channel reaches the end before reading all the bytes.
222     */
223    public static void readFully(final ReadableByteChannel channel, final ByteBuffer byteBuffer) throws IOException {
224        final int expectedLength = byteBuffer.remaining();
225        int read = 0;
226        while (read < expectedLength) {
227            final int readNow = channel.read(byteBuffer);
228            if (readNow <= 0) {
229                break;
230            }
231            read += readNow;
232        }
233        if (read < expectedLength) {
234            throw new EOFException();
235        }
236    }
237
238    // toByteArray(InputStream) copied from:
239    // commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941
240    // January 8th, 2013
241    //
242    // Assuming our copy() works just as well as theirs!  :-)
243
244    /**
245     * Gets the contents of an {@code InputStream} as a {@code byte[]}.
246     * <p>
247     * This method buffers the input internally, so there is no need to use a
248     * {@code BufferedInputStream}.
249     *
250     * @param input  the {@code InputStream} to read from
251     * @return the requested byte array
252     * @throws NullPointerException if the input is null
253     * @throws IOException if an I/O error occurs
254     * @since 1.5
255     */
256    public static byte[] toByteArray(final InputStream input) throws IOException {
257        final ByteArrayOutputStream output = new ByteArrayOutputStream();
258        copy(input, output);
259        return output.toByteArray();
260    }
261
262    /**
263     * Closes the given Closeable and swallows any IOException that may occur.
264     * @param c Closeable to close, can be null
265     * @since 1.7
266     */
267    public static void closeQuietly(final Closeable c) {
268        if (c != null) {
269            try {
270                c.close();
271            } catch (final IOException ignored) { // NOPMD NOSONAR
272            }
273        }
274    }
275
276    /**
277     * Copies the source file to the given output stream.
278     * @param sourceFile The file to read.
279     * @param outputStream The output stream to write.
280     * @throws IOException if an I/O error occurs when reading or writing.
281     * @since 1.21
282     */
283    public static void copy(final File sourceFile, final OutputStream outputStream) throws IOException {
284        Files.copy(sourceFile.toPath(), outputStream);
285    }
286
287    /**
288     * Copies part of the content of a InputStream into an OutputStream.
289     * Uses a default buffer size of 8024 bytes.
290     *
291     * @param input
292     *            the InputStream to copy
293     * @param output
294     *            the target Stream
295     * @param len
296     *            maximum amount of bytes to copy
297     * @return the number of bytes copied
298     * @throws IOException
299     *             if an error occurs
300     * @since 1.21
301     */
302    public static long copyRange(final InputStream input, final long len, final OutputStream output)
303        throws IOException {
304        return copyRange(input, len, output, COPY_BUF_SIZE);
305    }
306
307    /**
308     * Copies part of the content of a InputStream into an OutputStream
309     *
310     * @param input
311     *            the InputStream to copy
312     * @param len
313     *            maximum amount of bytes to copy
314     * @param output
315     *            the target, may be null to simulate output to dev/null on Linux and NUL on Windows
316     * @param buffersize
317     *            the buffer size to use, must be bigger than 0
318     * @return the number of bytes copied
319     * @throws IOException
320     *             if an error occurs
321     * @throws IllegalArgumentException
322     *             if buffersize is smaller than or equal to 0
323     * @since 1.21
324     */
325    public static long copyRange(final InputStream input, final long len, final OutputStream output,
326        final int buffersize) throws IOException {
327        if (buffersize < 1) {
328            throw new IllegalArgumentException("buffersize must be bigger than 0");
329        }
330        final byte[] buffer = new byte[(int) Math.min(buffersize, len)];
331        int n = 0;
332        long count = 0;
333        while (count < len && -1 != (n = input.read(buffer, 0, (int) Math.min(len - count, buffer.length)))) {
334            if (output != null) {
335                output.write(buffer, 0, n);
336            }
337            count += n;
338        }
339        return count;
340    }
341
342    /**
343     * Gets part of the contents of an {@code InputStream} as a {@code byte[]}.
344     *
345     * @param input  the {@code InputStream} to read from
346     * @param len
347     *            maximum amount of bytes to copy
348     * @return the requested byte array
349     * @throws NullPointerException if the input is null
350     * @throws IOException if an I/O error occurs
351     * @since 1.21
352     */
353    public static byte[] readRange(final InputStream input, final int len) throws IOException {
354        final ByteArrayOutputStream output = new ByteArrayOutputStream();
355        copyRange(input, len, output);
356        return output.toByteArray();
357    }
358
359    /**
360     * Gets part of the contents of an {@code ReadableByteChannel} as a {@code byte[]}.
361     *
362     * @param input  the {@code ReadableByteChannel} to read from
363     * @param len
364     *            maximum amount of bytes to copy
365     * @return the requested byte array
366     * @throws NullPointerException if the input is null
367     * @throws IOException if an I/O error occurs
368     * @since 1.21
369     */
370    public static byte[] readRange(final ReadableByteChannel input, final int len) throws IOException {
371        final ByteArrayOutputStream output = new ByteArrayOutputStream();
372        final ByteBuffer b = ByteBuffer.allocate(Math.min(len, COPY_BUF_SIZE));
373        int read = 0;
374        while (read < len) {
375            // Make sure we never read more than len bytes
376            ((Buffer)b).limit(Math.min(len - read, b.capacity()));
377            final int readNow = input.read(b);
378            if (readNow <= 0) {
379                break;
380            }
381            output.write(b.array(), 0, readNow);
382            ((Buffer)b).rewind();
383            read += readNow;
384        }
385        return output.toByteArray();
386    }
387
388}