KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > io > ToNetASCIIOutputStream


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.net.io;
17
18 import java.io.FilterOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /***
23  * This class wraps an output stream, replacing all singly occurring
24  * <LF> (linefeed) characters with <CR><LF> (carriage return
25  * followed by linefeed), which is the NETASCII standard for representing
26  * a newline.
27  * You would use this class to implement ASCII file transfers requiring
28  * conversion to NETASCII.
29  * <p>
30  * <p>
31  * @author Daniel F. Savarese
32  ***/

33
34 public final class ToNetASCIIOutputStream extends FilterOutputStream JavaDoc
35 {
36     private boolean __lastWasCR;
37
38     /***
39      * Creates a ToNetASCIIOutputStream instance that wraps an existing
40      * OutputStream.
41      * <p>
42      * @param output The OutputStream to wrap.
43      ***/

44     public ToNetASCIIOutputStream(OutputStream JavaDoc output)
45     {
46         super(output);
47         __lastWasCR = false;
48     }
49
50
51     /***
52      * Writes a byte to the stream. Note that a call to this method
53      * may result in multiple writes to the underlying input stream in order
54      * to convert naked newlines to NETASCII line separators.
55      * This is transparent to the programmer and is only mentioned for
56      * completeness.
57      * <p>
58      * @param ch The byte to write.
59      * @exception IOException If an error occurs while writing to the underlying
60      * stream.
61      ***/

62     public synchronized void write(int ch)
63     throws IOException JavaDoc
64     {
65         switch (ch)
66         {
67         case '\r':
68             __lastWasCR = true;
69             out.write('\r');
70             return ;
71         case '\n':
72             if (!__lastWasCR)
73                 out.write('\r');
74             // Fall through
75
default:
76             __lastWasCR = false;
77             out.write(ch);
78             return ;
79         }
80     }
81
82
83     /***
84      * Writes a byte array to the stream.
85      * <p>
86      * @param buffer The byte array to write.
87      * @exception IOException If an error occurs while writing to the underlying
88      * stream.
89      ***/

90     public synchronized void write(byte buffer[])
91     throws IOException JavaDoc
92     {
93         write(buffer, 0, buffer.length);
94     }
95
96
97     /***
98      * Writes a number of bytes from a byte array to the stream starting from
99      * a given offset.
100      * <p>
101      * @param buffer The byte array to write.
102      * @param offset The offset into the array at which to start copying data.
103      * @param length The number of bytes to write.
104      * @exception IOException If an error occurs while writing to the underlying
105      * stream.
106      ***/

107     public synchronized void write(byte buffer[], int offset, int length)
108     throws IOException JavaDoc
109     {
110         while (length-- > 0)
111             write(buffer[offset++]);
112     }
113
114 }
115
Popular Tags