Java InputStream Read by Encoding getStreamContents(InputStream stream, String charset)

Here you can find the source of getStreamContents(InputStream stream, String charset)

Description

get Stream Contents

License

Open Source License

Declaration

public static String getStreamContents(InputStream stream, String charset) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 Borland Software Corporation and others.
 * /*from www . j a  va 2s .  co  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Borland Software Corporation - initial API and implementation
 *******************************************************************************/

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String getStreamContents(InputStream stream, String charset) throws IOException {
        StringBuffer contents = new StringBuffer();
        char[] buf = new char[4096];
        InputStreamReader reader = null;

        try {
            if (charset == null) {
                reader = new InputStreamReader(stream);
            } else {
                reader = new InputStreamReader(stream, charset);
            }

            int read;
            while ((read = reader.read(buf)) > 0) {
                contents.append(buf, 0, read);
            }
            return contents.toString();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
}

Related

  1. getStreamContent(InputStream is, String encoding)