Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.util;
 
 
 
Utilities for IO operations.
 
 public enum IOUtils {
     ;
     private static final int BUFFER_SIZE = 1024 * 4;
     private static final Log defaultLog = LogFactory.getLog(IOUtils.class);

    
Reads and returns the rest of the given input stream as a byte array. Caller is responsible for closing the given input stream.
 
     public static byte[] toByteArray(InputStream isthrows IOException {
         ByteArrayOutputStream output = new ByteArrayOutputStream();
         try {
             byte[] b = new byte[];
             int n = 0;
             while ((n = is.read(b)) != -1) {
                 output.write(b, 0, n);
             }
             return output.toByteArray();
         } finally {
             output.close();
         }
     }

    
Reads and returns the rest of the given input stream as a string. Caller is responsible for closing the given input stream.
 
     public static String toString(InputStream isthrows IOException {
         return new String(toByteArray(is), .);
     }

    
Closes the given Closeable quietly.

Parameters:
is the given closeable
log logger used to log any failure should the close fail
 
     public static void closeQuietly(Closeable isLog log) {
         if (is != null) {
             try {
                 is.close();
             } catch (IOException ex) {
                 Log logger = log == null ?  : log;
                 if (logger.isDebugEnabled())
                     logger.debug("Ignore failure in closing the Closeable"ex);
             }
         }
     }

    
Releases the given java.io.Closeable especially if it was an instance of com.amazonaws.internal.Releasable.

For example, the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ResettableInputStream#disableClose(), so that the release method becomes the only way to truly close the opened file.

 
     public static void release(Closeable isLog log) {
         closeQuietly(islog);
         if (is instanceof Releasable) {
             Releasable r = (Releasableis;
            r.release();
        }
    }

    
Copies all bytes from the given input stream to the given output stream. Caller is responsible for closing the streams.

Throws:
java.io.IOException if there is any IO exception during read or write.
    public static long copy(InputStream inOutputStream out)
            throws IOException {
        byte[] buf = new byte[];
        long count = 0;
        int n = 0;
        while ((n = in.read(buf)) > -1) {
            out.write(buf, 0, n);
            count += n;
        }
        return count;
    }
New to GrepCode? Check out our FAQ X