Java InputStream to String inputStreamToText(InputStream inputStream)

Here you can find the source of inputStreamToText(InputStream inputStream)

Description

Converts a response in an InputStream to a String for easy comparisons

License

Apache License

Declaration

public static String inputStreamToText(InputStream inputStream) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   ww w  . ja va  2  s .  c o m*/
 *  Copyright 2003-2009 Terracotta, 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Converts a response in an InputStream to a String for easy comparisons
     */
    public static String inputStreamToText(InputStream inputStream) throws IOException {
        byte[] bytes = inputStreamToBytes(inputStream);
        return new String(bytes);
    }

    /**
     * Converts a response in an InputStream to a byte[] for easy manipulation
     */
    public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[10000];
        int r;
        while ((r = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, r);
        }
        return byteArrayOutputStream.toByteArray();
    }
}

Related

  1. inputStreamToString(InputStream stream)
  2. InputStreamToStringArray(InputStream input_stream)
  3. inputStreamToStringBuffer(InputStream stream)
  4. inputStreamToStringBuilder(final InputStream inputStream, final String charset)
  5. inputStreamToStringBuilder(InputStream in, int minimumCapacity)
  6. loadText(InputStream in)
  7. loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish)
  8. valueOf(InputStream is)