001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.common.utils;
024
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028
029/**
030 * This class provides various utilities for inputstreams.
031 */
032public class InputStreamUtils {
033
034    /**
035     * Read a line of bytes from an InputStream. Useful when an InputStream may contain both text and binary data.
036     *
037     * @param inputStream A source of data
038     * @return A line of text read from inputStream, with terminating \r\n or \n removed, or null if no data is
039     * available.
040     * @throws IOException on trouble reading from input stream
041     */
042    public static String readLine(InputStream inputStream) throws IOException {
043        byte[] rawdata = readRawLine(inputStream);
044        if (rawdata == null) {
045            return null;
046        }
047        int len = rawdata.length;
048        if (len > 0) {
049            if (rawdata[len - 1] == '\n') {
050                --len;
051                if (len > 0) {
052                    if (rawdata[len - 1] == '\r') {
053                        --len;
054                    }
055                }
056            }
057        }
058        return new String(rawdata, 0, len);
059    }
060
061    /**
062     * Reads a raw line from an InputStream, up till \n. Since HTTP allows \r\n and \n as terminators, this gets the
063     * whole line. This code is adapted from org.apache.commons.httpclient.HttpParser
064     *
065     * @param inputStream A stream to read from.
066     * @return Array of bytes read or null if none are available.
067     * @throws IOException if the underlying reads fail
068     */
069    public static byte[] readRawLine(InputStream inputStream) throws IOException {
070        ByteArrayOutputStream buf = new ByteArrayOutputStream();
071        int ch;
072        while ((ch = inputStream.read()) >= 0) {
073            buf.write(ch);
074            if (ch == '\n') {
075                break;
076            }
077        }
078        if (buf.size() == 0) {
079            return null;
080        }
081        return buf.toByteArray();
082    }
083
084}