39 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
40 |
* @version $Revision$ $Date$ |
* @version $Revision$ $Date$ |
41 |
*/ |
*/ |
42 |
public class MessageOutputStream |
public class MessageOutputStream extends FilterOutputStream |
|
extends FilterOutputStream |
|
43 |
{ |
{ |
44 |
|
|
45 |
/** |
/** |
46 |
* The stream termination octet. |
* The stream termination octet. |
47 |
*/ |
*/ |
48 |
public static final int END = 46; |
public static final int END = 46; |
49 |
|
|
50 |
/** |
/** |
51 |
* The line termination octet. |
* The line termination octet. |
52 |
*/ |
*/ |
53 |
public static final int LF = 10; |
public static final int LF = 10; |
54 |
|
|
55 |
int[] last = { LF, LF }; // the last character written to the stream |
int[] last = { LF, LF }; // the last character written to the stream |
56 |
|
|
57 |
/** |
/** |
58 |
* Constructs a message output stream connected to the specified output stream. |
* Constructs a message output stream connected to the specified output stream. |
59 |
* @param out the target output stream |
* @param out the target output stream |
60 |
*/ |
*/ |
61 |
public MessageOutputStream(OutputStream out) |
public MessageOutputStream(OutputStream out) |
62 |
{ |
{ |
63 |
super(out); |
super(out); |
64 |
} |
} |
65 |
|
|
66 |
/** |
/** |
67 |
* Character write. |
* Character write. |
68 |
*/ |
*/ |
69 |
public void write(int c) |
public void write(int c) throws IOException |
70 |
throws IOException |
{ |
71 |
{ |
if (last[0] == LF && last[1] == END && c == LF) |
72 |
if (last[0]==LF && last[1]==END && c==LF) |
out.write(END); |
73 |
out.write(END); |
out.write(c); |
74 |
out.write(c); |
last[0] = last[1]; |
75 |
last[0] = last[1]; |
last[1] = c; |
76 |
last[1] = c; |
} |
77 |
} |
|
78 |
|
public void write(byte[]bytes) throws IOException |
79 |
public void write(byte[] bytes) |
{ |
80 |
throws IOException |
write(bytes, 0, bytes.length); |
81 |
{ |
} |
82 |
write(bytes, 0, bytes.length); |
|
83 |
} |
/** |
|
|
|
|
/** |
|
84 |
* Block write. |
* Block write. |
85 |
*/ |
*/ |
86 |
public void write(byte[] bytes, int off, int len) |
public void write(byte[]bytes, int off, int len) throws IOException |
87 |
throws IOException |
{ |
88 |
{ |
for (int i = 0; i < len; i++) |
89 |
for (int i = 0; i < len; i++) |
{ |
90 |
{ |
int c = (int) bytes[off + i]; |
91 |
int c = (int)bytes[off+i]; |
if (last[0] == LF && last[1] == END && c == LF) |
92 |
if (last[0]==LF && last[1]==END && c==LF) |
{ |
93 |
{ |
byte[]b2 = new byte[bytes.length + 1]; |
94 |
byte[] b2 = new byte[bytes.length+1]; |
System.arraycopy(bytes, off, b2, off, i); |
95 |
System.arraycopy(bytes, off, b2, off, i); |
b2[off + i] = END; |
96 |
b2[off+i] = END; |
System.arraycopy(bytes, off + i, b2, off + i + 1, len - i); |
97 |
System.arraycopy(bytes, off+i, b2, off+i+1, len-i); |
bytes = b2; |
98 |
bytes = b2; |
i++; |
99 |
i++; len++; |
len++; |
100 |
} |
} |
101 |
last[0] = last[1]; |
last[0] = last[1]; |
102 |
last[1] = c; |
last[1] = c; |
103 |
} |
} |
104 |
out.write(bytes, off, len); |
out.write(bytes, off, len); |
105 |
} |
} |
|
|
|
|
} |
|
106 |
|
|
107 |
|
} |