Calls the method convert with the DEFAULT_ENCODING as an extra parameter via InputStream - Android File Input Output

Android examples for File Input Output:UTF File

Description

Calls the method convert with the DEFAULT_ENCODING as an extra parameter via InputStream

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static final String DEFAULT_ENCODING = "ISO-8859-1";

    /**/*from ww w. j  a v  a  2s.  com*/
     * Calls the method convert with the DEFAULT_ENCODING as an extra parameter
     * @param in the InputStream to be converted
     * @return the resulting String
     * @throws IOException
     */
    public static final String convert(final InputStream in)
            throws IOException {
        return convert(in, DEFAULT_ENCODING);
    }

    /**
     *  Converts a InputStream to String for a specific encoding
     * @param in the InputStream to be converted
     * @param encoding the type of encoding
     * @return the resulting String
     * @throws IOException
     */
    public static final String convert(final InputStream in,
            final String encoding) throws IOException {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final byte[] buf = new byte[2048];
        int rd;
        while ((rd = in.read(buf, 0, 2048)) >= 0) {
            out.write(buf, 0, rd);
        }
        return new String(out.toByteArray(), encoding);
    }
}

Related Tutorials