IoUtils.java

  1.  /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.commons.crypto.utils;

  19. import java.io.Closeable;
  20. import java.io.IOException;
  21. import java.io.InputStream;

  22. import org.apache.commons.crypto.stream.input.Input;

  23. /**
  24.  * General utility methods for working with IO.
  25.  */
  26. public final class IoUtils {

  27.     /**
  28.      * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or
  29.      * null pointers. Must only be used for cleanup in exception handlers.
  30.      *
  31.      * @param closeables the objects to close.
  32.      */
  33.     public static void cleanup(final Closeable... closeables) {
  34.         if (closeables != null) {
  35.             for (final Closeable c : closeables) {
  36.                 closeQuietly(c);
  37.             }
  38.         }
  39.     }

  40.     /**
  41.      * Closes the given {@link Closeable} quietly by ignoring IOException.
  42.      *
  43.      * @param closeable The resource to close.
  44.      * @since 1.1.0
  45.      */
  46.     public static void closeQuietly(final Closeable closeable) {
  47.         if (closeable != null) {
  48.             try {
  49.                 closeable.close();
  50.             } catch (final IOException e) { // NOPMD
  51.             }
  52.         }
  53.     }

  54.     /**
  55.      * Does the readFully based on Input's positioned read. This does not change
  56.      * the current offset of the stream and is thread-safe.
  57.      *
  58.      * @param in the input source.
  59.      * @param position the given position.
  60.      * @param buffer the buffer to be read.
  61.      * @param length the maximum number of bytes to read.
  62.      * @param offset the start offset in array buffer.
  63.      * @throws IOException if an I/O error occurs.
  64.      */
  65.     public static void readFully(final Input in, final long position, final byte[] buffer,
  66.             final int offset, final int length) throws IOException {
  67.         int nread = 0;
  68.         while (nread < length) {
  69.             final int nbytes = in.read(position + nread, buffer, offset + nread,
  70.                     length - nread);
  71.             if (nbytes < 0) {
  72.                 throw new IOException(
  73.                         "End of stream reached before reading fully.");
  74.             }
  75.             nread += nbytes;
  76.         }
  77.     }

  78.     /**
  79.      * Does the readFully based on the Input read.
  80.      *
  81.      * @param in the input stream of bytes.
  82.      * @param buf the buffer to be read.
  83.      * @param off the start offset in array buffer.
  84.      * @param len the maximum number of bytes to read.
  85.      * @throws IOException if an I/O error occurs.
  86.      */
  87.     public static void readFully(final InputStream in, final byte[] buf, int off, final int len)
  88.             throws IOException {
  89.         int toRead = len;
  90.         while (toRead > 0) {
  91.             final int ret = in.read(buf, off, toRead);
  92.             if (ret < 0) {
  93.                 throw new IOException("Premature EOF from inputStream");
  94.             }
  95.             toRead -= ret;
  96.             off += ret;
  97.         }
  98.     }

  99.     /**
  100.      * The private constructor of {@link IoUtils}.
  101.      */
  102.     private IoUtils() {
  103.     }
  104. }