38 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
39 |
* @author $Revision$ $Date$ |
* @author $Revision$ $Date$ |
40 |
*/ |
*/ |
41 |
public class CRLFOutputStream |
public class CRLFOutputStream extends FilterOutputStream |
|
extends FilterOutputStream |
|
42 |
{ |
{ |
43 |
|
|
44 |
static final String US_ASCII = "US-ASCII"; |
static final String US_ASCII = "US-ASCII"; |
45 |
|
|
46 |
/** |
/** |
47 |
* The CR octet. |
* The CR octet. |
76 |
* Writes a character to the underlying stream. |
* Writes a character to the underlying stream. |
77 |
* @exception IOException if an I/O error occurred |
* @exception IOException if an I/O error occurred |
78 |
*/ |
*/ |
79 |
public void write(int ch) |
public void write(int ch) throws IOException |
|
throws IOException |
|
80 |
{ |
{ |
81 |
if (ch==CR) |
if (ch == CR) |
82 |
out.write(CRLF); |
out.write(CRLF); |
83 |
else if (ch==LF) |
else if (ch == LF) |
84 |
{ |
{ |
85 |
if (last!=CR) |
if (last != CR) |
86 |
out.write(CRLF); |
out.write(CRLF); |
87 |
} |
} |
88 |
else |
else |
89 |
out.write(ch); |
out.write(ch); |
90 |
last = ch; |
last = ch; |
91 |
} |
} |
92 |
|
|
94 |
* Writes a byte array to the underlying stream. |
* Writes a byte array to the underlying stream. |
95 |
* @exception IOException if an I/O error occurred |
* @exception IOException if an I/O error occurred |
96 |
*/ |
*/ |
97 |
public void write(byte b[]) |
public void write(byte b[]) throws IOException |
|
throws IOException |
|
98 |
{ |
{ |
99 |
write(b, 0, b.length); |
write(b, 0, b.length); |
100 |
} |
} |
103 |
* Writes a portion of a byte array to the underlying stream. |
* Writes a portion of a byte array to the underlying stream. |
104 |
* @exception IOException if an I/O error occurred |
* @exception IOException if an I/O error occurred |
105 |
*/ |
*/ |
106 |
public void write(byte b[], int off, int len) |
public void write(byte b[], int off, int len) throws IOException |
|
throws IOException |
|
107 |
{ |
{ |
108 |
int d = off; |
int d = off; |
109 |
len += off; |
len += off; |
110 |
for (int i=off; i<len; i++) |
for (int i = off; i < len; i++) |
111 |
{ |
{ |
112 |
switch (b[i]) |
switch (b[i]) |
113 |
{ |
{ |
114 |
case CR: |
case CR: |
115 |
out.write(b, d, i-d); |
out.write(b, d, i - d); |
116 |
|
out.write(CRLF, 0, 2); |
117 |
|
d = i + 1; |
118 |
|
break; |
119 |
|
case LF:if (last != CR) |
120 |
|
{ |
121 |
|
out.write(b, d, i - d); |
122 |
out.write(CRLF, 0, 2); |
out.write(CRLF, 0, 2); |
123 |
d = i+1; |
} |
124 |
break; |
d = i + 1; |
125 |
case LF: |
break; |
|
if (last!=CR) |
|
|
{ |
|
|
out.write(b, d, i-d); |
|
|
out.write(CRLF, 0, 2); |
|
|
} |
|
|
d = i+1; |
|
|
break; |
|
126 |
} |
} |
127 |
last = b[i]; |
last = b[i]; |
128 |
} |
} |
129 |
if (len-d>0) |
if (len - d > 0) |
130 |
out.write(b, d, len-d); |
out.write(b, d, len - d); |
131 |
} |
} |
132 |
|
|
133 |
/** |
/** |
134 |
* Writes the specified ASCII string to the underlying stream. |
* Writes the specified ASCII string to the underlying stream. |
135 |
* @exception IOException if an I/O error occurred |
* @exception IOException if an I/O error occurred |
136 |
*/ |
*/ |
137 |
public void write(String text) |
public void write(String text) throws IOException |
|
throws IOException |
|
138 |
{ |
{ |
139 |
try |
try |
140 |
{ |
{ |
141 |
byte[] bytes = text.getBytes(US_ASCII); |
byte[]bytes = text.getBytes(US_ASCII); |
142 |
write(bytes, 0, bytes.length); |
write(bytes, 0, bytes.length); |
143 |
} |
} |
144 |
catch (UnsupportedEncodingException e) |
catch(UnsupportedEncodingException e) |
145 |
{ |
{ |
146 |
throw new IOException("Not ASCII: "+text); |
throw new IOException("Not ASCII: " + text); |
147 |
} |
} |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
151 |
* Writes a newline to the underlying stream. |
* Writes a newline to the underlying stream. |
152 |
* @exception IOException if an I/O error occurred |
* @exception IOException if an I/O error occurred |
153 |
*/ |
*/ |
154 |
public void writeln() |
public void writeln() throws IOException |
|
throws IOException |
|
155 |
{ |
{ |
156 |
out.write(CRLF); |
out.write(CRLF); |
157 |
} |
} |