Android InputStream to Byte Array Convert readFile(InputStream in, String encoding)

Here you can find the source of readFile(InputStream in, String encoding)

Description

Reads the given input stream to a string content.

License

Apache License

Declaration

public static String readFile(InputStream in, String encoding)
        throws IOException 

Method Source Code

/**//from w  w  w  . j  a  v a2s  .  c  o m
 *  Copyright 2009 Welocalize, Inc. 
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License 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.
 *  
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class Main{
    public static byte[] readFile(File file, int size) throws IOException {
        return readFile(new FileInputStream(file), size);
    }
    /**
     * Reads bytes from given input stream with specified length.
     */
    public static byte[] readFile(InputStream in, int size)
            throws IOException {
        byte[] b = new byte[size];
        try {
            in.read(b, 0, size);
        } finally {
            if (in != null) {
                in.close();
            }
        }

        return b;
    }
    public static String readFile(File file) throws IOException {
        FileInputStream in = null;

        try {
            in = new FileInputStream(file);
            byte[] b = new byte[in.available()];
            in.read(b, 0, b.length);
            return new String(b);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
    public static String readFile(File file, String encoding)
            throws IOException {
        return readFile(new FileInputStream(file), encoding);
    }
    /**
     * Reads the given input stream to a string content.
     */
    public static String readFile(InputStream in, String encoding)
            throws IOException {
        try {
            byte[] b = new byte[in.available()];
            in.read(b);
            return new String(b, encoding);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

Related

  1. getBytes(InputStream inputStream)
  2. getFileByte(InputStream inputStream)
  3. readBytes(InputStream s)
  4. readFile(InputStream in, int size)
  5. stream2byte(InputStream inStream)
  6. inStream2byte(InputStream inStream)
  7. inStream2byte(InputStream inStream)