Android InputStream to String Convert stream2String(InputStream is, int maxLength)

Here you can find the source of stream2String(InputStream is, int maxLength)

Description

Read a String of up to maxLength bytes from an InputStream.

License

Apache License

Parameter

Parameter Description
is input stream
maxLength max number of bytes to read from "is". If this is -1, we read everything.

Return

String up to maxLength bytes, read from "is"

Declaration

@Deprecated
public static String stream2String(InputStream is, int maxLength)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*from w w w .  j  a v a  2 s.c  o  m*/
 * Copyright (c) 2000, Google 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.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public class Main {
    /**
     * Read a String of up to maxLength bytes from an InputStream.
     *
     * <p>Note that this method uses the default platform encoding, and expects
     * that encoding to be single-byte, which is not always the case. Its use
     * is discouraged. For reading the entire stream (maxLength == -1) you can use:
     * <pre>
     *   CharStreams.toString(new InputStreamReader(is, Charsets.ISO_8859_1))
     * </pre>
     * {@code CharStreams} is in the {@code com.google.common.io} package.
     *
     * <p>For maxLength >= 0 a literal translation would be
     * <pre>
     *   CharStreams.toString(new InputStreamReader(
     *       new LimitInputStream(is, maxLength), Charsets.ISO_8859_1))
     * </pre>
     * For multi-byte encodings that is broken because the limit could end in
     * the middle of the character--it would be better to limit the reader than
     * the underlying stream.
     *
     * @param is input stream
     * @param maxLength max number of bytes to read from "is". If this is -1, we
     *          read everything.
     *
     * @return String up to maxLength bytes, read from "is"
     * @deprecated see the advice above
     */
    @Deprecated
    public static String stream2String(InputStream is, int maxLength)
            throws IOException {
        byte[] buffer = new byte[4096];
        StringWriter sw = new StringWriter();
        int totalRead = 0;
        int read = 0;

        do {
            sw.write(new String(buffer, 0, read));
            totalRead += read;
            read = is.read(buffer, 0, buffer.length);
        } while (((-1 == maxLength) || (totalRead < maxLength))
                && (read != -1));

        return sw.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(float[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(long[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array
     */
    public static String toString(int[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append(iArray[i]);
            if (i != (iArray.length - 1)) {
                buffer.append(", ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given array.
     */
    public static String toString(String[] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("'").append(iArray[i]).append("'");
            if (i != iArray.length - 1) {
                buffer.append(", ");
            }
        }
        buffer.append("]");

        return buffer.toString();
    }

    /**
     * Returns the string, in single quotes, or "NULL". Intended only for
     * logging.
     *
     * @param s the string
     * @return the string, in single quotes, or the string "null" if it's null.
     */
    public static String toString(String s) {
        if (s == null) {
            return "NULL";
        } else {
            return new StringBuilder(s.length() + 2).append("'").append(s)
                    .append("'").toString();
        }
    }

    /**
     * @return a string representation of the given native array
     */
    public static String toString(int[][] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("[");
            for (int j = 0; j < iArray[i].length; j++) {
                buffer.append(iArray[i][j]);
                if (j != (iArray[i].length - 1)) {
                    buffer.append(", ");
                }
            }
            buffer.append("]");
            if (i != iArray.length - 1) {
                buffer.append(" ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a string representation of the given native array.
     */
    public static String toString(long[][] iArray) {
        if (iArray == null) {
            return "NULL";
        }

        StringBuilder buffer = new StringBuilder();
        buffer.append("[");
        for (int i = 0; i < iArray.length; i++) {
            buffer.append("[");
            for (int j = 0; j < iArray[i].length; j++) {
                buffer.append(iArray[i][j]);
                if (j != (iArray[i].length - 1)) {
                    buffer.append(", ");
                }
            }
            buffer.append("]");
            if (i != iArray.length - 1) {
                buffer.append(" ");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    /**
     * @return a String representation of the given object array.
     * The strings are obtained by calling toString() on the
     * underlying objects.
     */
    public static String toString(Object[] obj) {
        if (obj == null) {
            return "NULL";
        }
        StringBuilder tmp = new StringBuilder();
        tmp.append("[");
        for (int i = 0; i < obj.length; i++) {
            tmp.append(obj[i].toString());
            if (i != obj.length - 1) {
                tmp.append(",");
            }
        }
        tmp.append("]");
        return tmp.toString();
    }
}

Related

  1. readStreamAsString(InputStream is)
  2. readStringFromStream(InputStream is)
  3. getString(InputStream is)
  4. inputStream2String(InputStream is)
  5. inputStream2String(InputStream in)
  6. fromStream(InputStream stream)
  7. toConvertString(InputStream is)
  8. inStream2String(InputStream is)
  9. inputStream2String(InputStream is)