Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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 org.apache.commons.net.io;
 
 import java.io.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.

 
 
 public final class DotTerminatedMessageWriter extends Writer
 {
     private static final int __NOTHING_SPECIAL_STATE = 0;
     private static final int __LAST_WAS_CR_STATE = 1;
     private static final int __LAST_WAS_NL_STATE = 2;
 
     private int __state;
     private Writer __output;


    
Creates a DotTerminatedMessageWriter that wraps an existing Writer output destination.

Parameters:
output The Writer output destination to write the message.
 
     public DotTerminatedMessageWriter(Writer output)
     {
         super(output);
          = output;
          = ;
     }


    
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.

Parameters:
ch The character to write.
Throws:
java.io.IOException If an error occurs while writing to the underlying output.
 
     @Override
     public void write(int chthrows IOException
     {
         synchronized ()
         {
             switch (ch)
             {
             case '\r':
                  = ;
                 .write('\r');
                 return ;
             case '\n':
                 if ( != ) {
                     .write('\r');
                 }
                 .write('\n');
                  = ;
                 return ;
             case '.':
                 // Double the dot at the beginning of a line
                 if ( == ) {
                     .write('.');
                 }
                 //$FALL-THROUGH$
             default:
                  = ;
                .write(ch);
                return ;
            }
        }
    }


    
Writes a number of characters from a character array to the output starting from a given offset.

Parameters:
buffer The character array to write.
offset The offset into the array at which to start copying data.
length The number of characters to write.
Throws:
java.io.IOException If an error occurs while writing to the underlying output.
    @Override
    public void write(char[] bufferint offsetint lengththrows IOException
    {
        synchronized ()
        {
            while (length-- > 0) {
                write(buffer[offset++]);
            }
        }
    }


    
Writes a character array to the output.

Parameters:
buffer The character array to write.
Throws:
java.io.IOException If an error occurs while writing to the underlying output.
    @Override
    public void write(char[] bufferthrows IOException
    {
        write(buffer, 0, buffer.length);
    }


    
Writes a String to the output.

Parameters:
string The String to write.
Throws:
java.io.IOException If an error occurs while writing to the underlying output.
    @Override
    public void write(String stringthrows IOException
    {
        write(string.toCharArray());
    }


    
Writes part of a String to the output starting from a given offset.

Parameters:
string The String to write.
offset The offset into the String at which to start copying data.
length The number of characters to write.
Throws:
java.io.IOException If an error occurs while writing to the underlying output.
    @Override
    public void write(String stringint offsetint lengththrows IOException
    {
        write(string.toCharArray(), offsetlength);
    }


    
Flushes the underlying output, writing all buffered output.

Throws:
java.io.IOException If an error occurs while writing to the underlying output.
    @Override
    public void flush() throws IOException
    {
        synchronized ()
        {
            .flush();
        }
    }


    
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.

Throws:
java.io.IOException If an error occurs while writing to the underlying output or closing the Writer.
    @Override
    public void close() throws IOException
    {
        synchronized ()
        {
            if ( == null) {
                return ;
            }
            if ( == ) {
                .write('\n');
            } else if ( != ) {
                .write("\r\n");
            }
            .write(".\r\n");
            .flush();
             = null;
        }
    }
New to GrepCode? Check out our FAQ X