001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.io;
017:
018: import java.io.FilterOutputStream;
019: import java.io.IOException;
020: import java.io.OutputStream;
021:
022: /***
023: * This class wraps an output stream, replacing all singly occurring
024: * <LF> (linefeed) characters with <CR><LF> (carriage return
025: * followed by linefeed), which is the NETASCII standard for representing
026: * a newline.
027: * You would use this class to implement ASCII file transfers requiring
028: * conversion to NETASCII.
029: * <p>
030: * <p>
031: * @author Daniel F. Savarese
032: ***/
033:
034: public final class ToNetASCIIOutputStream extends FilterOutputStream {
035: private boolean __lastWasCR;
036:
037: /***
038: * Creates a ToNetASCIIOutputStream instance that wraps an existing
039: * OutputStream.
040: * <p>
041: * @param output The OutputStream to wrap.
042: ***/
043: public ToNetASCIIOutputStream(OutputStream output) {
044: super (output);
045: __lastWasCR = false;
046: }
047:
048: /***
049: * Writes a byte to the stream. Note that a call to this method
050: * may result in multiple writes to the underlying input stream in order
051: * to convert naked newlines to NETASCII line separators.
052: * This is transparent to the programmer and is only mentioned for
053: * completeness.
054: * <p>
055: * @param ch The byte to write.
056: * @exception IOException If an error occurs while writing to the underlying
057: * stream.
058: ***/
059: public synchronized void write(int ch) throws IOException {
060: switch (ch) {
061: case '\r':
062: __lastWasCR = true;
063: out.write('\r');
064: return;
065: case '\n':
066: if (!__lastWasCR)
067: out.write('\r');
068: // Fall through
069: default:
070: __lastWasCR = false;
071: out.write(ch);
072: return;
073: }
074: }
075:
076: /***
077: * Writes a byte array to the stream.
078: * <p>
079: * @param buffer The byte array to write.
080: * @exception IOException If an error occurs while writing to the underlying
081: * stream.
082: ***/
083: public synchronized void write(byte buffer[]) throws IOException {
084: write(buffer, 0, buffer.length);
085: }
086:
087: /***
088: * Writes a number of bytes from a byte array to the stream starting from
089: * a given offset.
090: * <p>
091: * @param buffer The byte array to write.
092: * @param offset The offset into the array at which to start copying data.
093: * @param length The number of bytes to write.
094: * @exception IOException If an error occurs while writing to the underlying
095: * stream.
096: ***/
097: public synchronized void write(byte buffer[], int offset, int length)
098: throws IOException {
099: while (length-- > 0)
100: write(buffer[offset++]);
101: }
102:
103: }
|