001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.io;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.Reader;
023
024/**
025 * CRLFLineReader implements a readLine() method that requires
026 * exactly CRLF to terminate an input line.
027 * This is required for IMAP, which allows bare CR and LF.
028 *
029 * @since 3.0
030 */
031public final class CRLFLineReader extends BufferedReader
032{
033    private static final char LF = '\n';
034    private static final char CR = '\r';
035
036    /**
037     * Creates a CRLFLineReader that wraps an existing Reader
038     * input source.
039     * @param reader  The Reader input source.
040     */
041    public CRLFLineReader(Reader reader)
042    {
043        super(reader);
044    }
045
046    /**
047     * Read a line of text.
048     * A line is considered to be terminated by carriage return followed immediately by a linefeed.
049     * This contrasts with BufferedReader which also allows other combinations.
050     * @since 3.0
051     */
052    @Override
053    public String readLine() throws IOException {
054        StringBuilder sb = new StringBuilder();
055        int intch;
056        boolean prevWasCR = false;
057        synchronized(lock) { // make thread-safe (hopefully!)
058            while((intch = read()) != -1)
059            {
060                if (prevWasCR && intch == LF) {
061                    return sb.substring(0, sb.length()-1);
062                }
063                if (intch == CR) {
064                    prevWasCR = true;
065                } else {
066                    prevWasCR = false;
067                }
068                sb.append((char) intch);
069            }
070        }
071        String string = sb.toString();
072        if (string.length() == 0) { // immediate EOF
073            return null;
074        }
075        return string;
076    }
077}