com.discovery.darchrow.io.ReaderUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.io.ReaderUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * 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.
 */
package com.discovery.darchrow.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;

/**
 * {@link java.io.Reader} .
 *
 * @author feilong
 * @version 1.0.9 201536 ?10:51:54
 * @see java.io.BufferedReader
 * @see java.io.CharArrayReader
 * @see java.io.FilterReader
 * @see java.io.InputStreamReader
 * @see java.io.PipedReader
 * @see java.io.StringReader
 * @see java.io.LineNumberReader
 * @since 1.0.9
 */
public final class ReaderUtil {

    /** Don't let anyone instantiate this class. */
    private ReaderUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     *  {@link java.io.Reader} ? {@link java.lang.String}.
     *
     * @param reader
     *            the reader
     * @return the string
     * @see org.apache.commons.io.IOUtils#toBufferedReader(Reader)
     */
    public static String toString(Reader reader) {

        BufferedReader bufferedReader = org.apache.commons.io.IOUtils.toBufferedReader(reader);

        try {
            StringBuilder sb = new StringBuilder();
            String line = "";

            // ?.???? ('\n')? ('\r') ???.
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
                sb.append(SystemUtils.LINE_SEPARATOR);
            }
            return sb.toString();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    /**
     *  {@link java.io.Reader} ? {@link java.lang.String}.
     *
     * @param reader
     *            the reader
     * @return the string
     * @see org.apache.commons.io.IOUtils#toBufferedReader(Reader)
     */
    public static String readLine(Reader reader) {

        BufferedReader bufferedReader = IOUtils.toBufferedReader(reader);

        if (null == bufferedReader) {
            throw new NullPointerException("the bufferedReader is null or empty!");
        }

        try {
            // ?.???? ('\n')? ('\r') ???.
            String readLine = bufferedReader.readLine();
            return readLine;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}