/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.util;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;

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

/**
 * This class injests an <code>InputStream</code> into a <code>StringBuilder</code>.
 *
 * @author Alex Leigh
 */
public final class StreamReader {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(StreamReader.class);

    public static String ingest(final InputStream in) throws IOException {
        final StringBuilder out = new StringBuilder();
        final byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }

        logger.debug("Read: {}", out);
        return out.toString();
    }
}
