FileDocCategorySizeDatePackage
DotTerminatedMessageWriter.javaAPI DocApache Commons NET 1.4.1 API6413Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.io

DotTerminatedMessageWriter

public final class DotTerminatedMessageWriter extends Writer
DotTerminatedMessageWriter is a class used to write messages to a server that are terminated by a single dot followed by a <CR><LF> sequence and with double dots appearing at the begining of lines which do not signal end of message yet start with a dot. Various Internet protocols such as NNTP and POP3 produce messages of this type.

This class handles the doubling of line-starting periods, converts single linefeeds to NETASCII newlines, and on closing will send the final message terminator dot and NETASCII newline sequence.

author
Daniel F. Savarese

Fields Summary
private static final int
__NOTHING_SPECIAL_STATE
private static final int
__LAST_WAS_CR_STATE
private static final int
__LAST_WAS_NL_STATE
private int
__state
private Writer
__output
Constructors Summary
public DotTerminatedMessageWriter(Writer output)
Creates a DotTerminatedMessageWriter that wraps an existing Writer output destination.

param
output The Writer output destination to write the message.



                               
      
    
        super(output);
        __output = output;
        __state = __NOTHING_SPECIAL_STATE;
    
Methods Summary
public voidclose()
Flushes the underlying output, writing all buffered output, but doesn't actually close the underlying stream. The underlying stream may still be used for communicating with the server and therefore is not closed.

exception
IOException If an error occurs while writing to the underlying output or closing the Writer.

        synchronized (lock)
        {
            if (__output == null)
                return ;

            if (__state == __LAST_WAS_CR_STATE)
                __output.write('\n");
            else if (__state != __LAST_WAS_NL_STATE)
                __output.write("\r\n");

            __output.write(".\r\n");

            __output.flush();
            __output = null;
        }
    
public voidflush()
Flushes the underlying output, writing all buffered output.

exception
IOException If an error occurs while writing to the underlying output.

        synchronized (lock)
        {
            __output.flush();
        }
    
public voidwrite(int ch)
Writes a character to the output. Note that a call to this method may result in multiple writes to the underling Writer in order to convert naked linefeeds to NETASCII line separators and to double line-leading periods. This is transparent to the programmer and is only mentioned for completeness.

param
ch The character to write.
exception
IOException If an error occurs while writing to the underlying output.

        synchronized (lock)
        {
            switch (ch)
            {
            case '\r":
                __state = __LAST_WAS_CR_STATE;
                __output.write('\r");
                return ;
            case '\n":
                if (__state != __LAST_WAS_CR_STATE)
                    __output.write('\r");
                __output.write('\n");
                __state = __LAST_WAS_NL_STATE;
                return ;
            case '.":
                // Double the dot at the beginning of a line
                if (__state == __LAST_WAS_NL_STATE)
                    __output.write('.");
                // Fall through
            default:
                __state = __NOTHING_SPECIAL_STATE;
                __output.write(ch);
                return ;
            }
        }
    
public voidwrite(char[] buffer, int offset, int length)
Writes a number of characters from a character array to the output starting from a given offset.

param
buffer The character array to write.
param
offset The offset into the array at which to start copying data.
param
length The number of characters to write.
exception
IOException If an error occurs while writing to the underlying output.

        synchronized (lock)
        {
            while (length-- > 0)
                write(buffer[offset++]);
        }
    
public voidwrite(char[] buffer)
Writes a character array to the output.

param
buffer The character array to write.
exception
IOException If an error occurs while writing to the underlying output.

        write(buffer, 0, buffer.length);
    
public voidwrite(java.lang.String string)
Writes a String to the output.

param
string The String to write.
exception
IOException If an error occurs while writing to the underlying output.

        write(string.toCharArray());
    
public voidwrite(java.lang.String string, int offset, int length)
Writes part of a String to the output starting from a given offset.

param
string The String to write.
param
offset The offset into the String at which to start copying data.
param
length The number of characters to write.
exception
IOException If an error occurs while writing to the underlying output.

        write(string.toCharArray(), offset, length);