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 * 030 */ 031 032public final class ToNetASCIIOutputStream extends FilterOutputStream { 033 private boolean lastWasCR; 034 035 /** 036 * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream. 037 * 038 * @param output The OutputStream to wrap. 039 */ 040 public ToNetASCIIOutputStream(final OutputStream output) { 041 super(output); 042 lastWasCR = false; 043 } 044 045 /** 046 * Writes a byte array to the stream. 047 * 048 * @param buffer The byte array to write. 049 * @throws IOException If an error occurs while writing to the underlying stream. 050 */ 051 @Override 052 public synchronized void write(final byte buffer[]) throws IOException { 053 write(buffer, 0, buffer.length); 054 } 055 056 /** 057 * Writes a number of bytes from a byte array to the stream starting from a given offset. 058 * 059 * @param buffer The byte array to write. 060 * @param offset The offset into the array at which to start copying data. 061 * @param length The number of bytes to write. 062 * @throws IOException If an error occurs while writing to the underlying stream. 063 */ 064 @Override 065 public synchronized void write(final byte buffer[], int offset, int length) throws IOException { 066 while (length-- > 0) { 067 write(buffer[offset++]); 068 } 069 } 070 071 /** 072 * 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 073 * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness. 074 * 075 * @param ch The byte to write. 076 * @throws IOException If an error occurs while writing to the underlying stream. 077 */ 078 @Override 079 public synchronized void write(final int ch) throws IOException { 080 switch (ch) { 081 case '\r': 082 lastWasCR = true; 083 out.write('\r'); 084 return; 085 case '\n': 086 if (!lastWasCR) { 087 out.write('\r'); 088 } 089 //$FALL-THROUGH$ 090 default: 091 lastWasCR = false; 092 out.write(ch); 093 } 094 } 095 096}