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     * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or
033     * null pointers. Must only be used for cleanup in exception handlers.
034     *
035     * @param closeables the objects to close.
036     */
037    public static void cleanup(final Closeable... closeables) {
038        if (closeables != null) {
039            for (final Closeable c : closeables) {
040                closeQuietly(c);
041            }
042        }
043    }
044
045    /**
046     * Closes the given {@link Closeable} quietly by ignoring IOException.
047     *
048     * @param closeable The resource to close.
049     * @since 1.1.0
050     */
051    public static void closeQuietly(final Closeable closeable) {
052        if (closeable != null) {
053            try {
054                closeable.close();
055            } catch (final IOException e) { // NOPMD
056            }
057        }
058    }
059
060    /**
061     * Does the readFully based on Input's positioned read. This does not change
062     * the current offset of the stream and is thread-safe.
063     *
064     * @param in the input source.
065     * @param position the given position.
066     * @param buffer the buffer to be read.
067     * @param length the maximum number of bytes to read.
068     * @param offset the start offset in array buffer.
069     * @throws IOException if an I/O error occurs.
070     */
071    public static void readFully(final Input in, final long position, final byte[] buffer,
072            final int offset, final int length) throws IOException {
073        int nread = 0;
074        while (nread < length) {
075            final int nbytes = in.read(position + nread, buffer, offset + nread,
076                    length - nread);
077            if (nbytes < 0) {
078                throw new IOException(
079                        "End of stream reached before reading fully.");
080            }
081            nread += nbytes;
082        }
083    }
084
085    /**
086     * Does the readFully based on the Input read.
087     *
088     * @param in the input stream of bytes.
089     * @param buf the buffer to be read.
090     * @param off the start offset in array buffer.
091     * @param len the maximum number of bytes to read.
092     * @throws IOException if an I/O error occurs.
093     */
094    public static void readFully(final InputStream in, final byte[] buf, int off, final int len)
095            throws IOException {
096        int toRead = len;
097        while (toRead > 0) {
098            final int ret = in.read(buf, off, toRead);
099            if (ret < 0) {
100                throw new IOException("Premature EOF from inputStream");
101            }
102            toRead -= ret;
103            off += ret;
104        }
105    }
106
107    /**
108     * The private constructor of {@link IoUtils}.
109     */
110    private IoUtils() {
111    }
112}