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