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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.commons.crypto.utils;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.apache.commons.crypto.stream.input.Input;
025
026/**
027 * General utility methods for working with IO.
028 */
029public final class IoUtils {
030
031    /**
032     * The private constructor of {@link IoUtils}.
033     */
034    private IoUtils() {
035    }
036
037    /**
038     * Does the readFully based on the Input read.
039     *
040     * @param in the input stream of bytes.
041     * @param buf the buffer to be read.
042     * @param off the start offset in array buffer.
043     * @param len the maximum number of bytes to read.
044     * @throws IOException if an I/O error occurs.
045     */
046    public static void readFully(final InputStream in, final byte buf[], int off, final int len)
047            throws IOException {
048        int toRead = len;
049        while (toRead > 0) {
050            final int ret = in.read(buf, off, toRead);
051            if (ret < 0) {
052                throw new IOException("Premature EOF from inputStream");
053            }
054            toRead -= ret;
055            off += ret;
056        }
057    }
058
059    /**
060     * Does the readFully based on Input's positioned read. This does not change
061     * the current offset of the stream and is thread-safe.
062     *
063     * @param in the input source.
064     * @param position the given position.
065     * @param buffer the buffer to be read.
066     * @param length the maximum number of bytes to read.
067     * @param offset the start offset in array buffer.
068     * @throws IOException if an I/O error occurs.
069     */
070    public static void readFully(final Input in, final long position, final byte[] buffer,
071            final int offset, final int length) throws IOException {
072        int nread = 0;
073        while (nread < length) {
074            final int nbytes = in.read(position + nread, buffer, offset + nread,
075                    length - nread);
076            if (nbytes < 0) {
077                throw new IOException(
078                        "End of stream reached before reading fully.");
079            }
080            nread += nbytes;
081        }
082    }
083
084    /**
085     * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or
086     * null pointers. Must only be used for cleanup in exception handlers.
087     *
088     * @param closeables the objects to close.
089     */
090    public static void cleanup(final Closeable... closeables) {
091        if (closeables != null) {
092            for (final Closeable c : closeables) {
093                closeQuietly(c);
094            }
095        }
096    }
097
098    /**
099     * Closes the given {@link Closeable} quietly by ignoring IOException.
100     *
101     * @param closeable The resource to close.
102     * @since 1.1.0
103     */
104    public static void closeQuietly(final Closeable closeable) {
105        if (closeable != null) {
106            try {
107                closeable.close();
108            } catch (final IOException e) { // NOPMD
109            }
110        }
111    }
112}