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.FilterOutputStream; 021import java.io.IOException; 022import java.io.OutputStream; 023 024/** 025 * This class wraps an output stream, replacing all singly occurring <LF> (linefeed) characters with <CR><LF> (carriage return followed by 026 * linefeed), which is the NETASCII standard for representing a newline. You would use this class to implement ASCII file transfers requiring conversion to 027 * NETASCII. 028 */ 029 030public final class ToNetASCIIOutputStream extends FilterOutputStream { 031 private boolean lastWasCR; 032 033 /** 034 * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream. 035 * 036 * @param output The OutputStream to wrap. 037 */ 038 public ToNetASCIIOutputStream(final OutputStream output) { 039 super(output); 040 lastWasCR = false; 041 } 042 043 /** 044 * Writes a byte array to the stream. 045 * 046 * @param buffer The byte array to write. 047 * @throws IOException If an error occurs while writing to the underlying stream. 048 */ 049 @Override 050 public synchronized void write(final byte buffer[]) throws IOException { 051 write(buffer, 0, buffer.length); 052 } 053 054 /** 055 * Writes a number of bytes from a byte array to the stream starting from a given offset. 056 * 057 * @param buffer The byte array to write. 058 * @param offset The offset into the array at which to start copying data. 059 * @param length The number of bytes to write. 060 * @throws IOException If an error occurs while writing to the underlying stream. 061 */ 062 @Override 063 public synchronized void write(final byte buffer[], int offset, int length) throws IOException { 064 while (length-- > 0) { 065 write(buffer[offset++]); 066 } 067 } 068 069 /** 070 * Writes a byte to the stream. Note that a call to this method may result in multiple writes to the underlying input stream in order to convert naked 071 * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness. 072 * 073 * @param ch The byte to write. 074 * @throws IOException If an error occurs while writing to the underlying stream. 075 */ 076 @Override 077 public synchronized void write(final int ch) throws IOException { 078 switch (ch) { 079 case '\r': 080 lastWasCR = true; 081 out.write('\r'); 082 return; 083 case '\n': 084 if (!lastWasCR) { 085 out.write('\r'); 086 } 087 //$FALL-THROUGH$ 088 default: 089 lastWasCR = false; 090 out.write(ch); 091 } 092 } 093 094}