KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > TextConversionStream


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.util;
21
22 import java.io.*;
23
24 /**
25  *
26  * <p>This class processes text data and corrects line endings according to
27  * the settings provided. Specifically you can set the input style to process
28  * so that lone \r and \n characters within a \r\n encoded file are written
29  * through without amendments (the SFTP protocol states that this must be
30  * the case when processing text files).</p>
31  *
32  * @author Lee David Painter
33  */

34 public class TextConversionStream extends FilterOutputStream {
35
36         /**
37          * This output style specifies that the text will have line endings set to
38          * the current system setting.
39          */

40         public final static int TEXT_SYSTEM = 0;
41         /**
42          * This indicates a CRLF line ending combinarion and can be used
43          * for both input and output style parameters.
44          */

45         public final static int TEXT_WINDOWS = 1;
46         /**
47          * This indicates a CRLF line ending combinarion and can be used
48          * for both input and output style parameters.
49          */

50         public final static int TEXT_DOS = 1;
51         /*
52          * This indicates a CRLF line ending combinarion and can be used
53          * for both input and output style parameters.
54          */

55         public final static int TEXT_CRLF = 1;
56         /**
57          * This indicates a LF line ending style and can be
58          * used for either input or output style parameters
59          */

60         public final static int TEXT_UNIX = 2;
61         /**
62          * This indicates a single LF line ending style and can be used for
63          * either input or output style parameters
64          */

65         public final static int TEXT_LF = 2;
66         /**
67          * This indicates a MAC line ending and can be used in either the
68          * input or output style parameters
69          */

70         public final static int TEXT_MAC = 3;
71         /**
72          * This indicates a CR line ending style and can be used for either
73          * input or output style parameters
74          */

75         public final static int TEXT_CR = 3;
76
77         /**
78          * This input style instructs the conversion to strip
79          * all line ending type characters and replace with the output style
80          * line ending
81          */

82         public final static int TEXT_ALL = 4;
83
84         byte[] lineEnding;
85         String JavaDoc systemNL = System.getProperty("line.separator");
86         boolean stripCR;
87         boolean stripLF;
88         boolean stripCRLF;
89         boolean encounteredBinary = false;
90
91         boolean lastCharacterWasCR = false;
92
93         public TextConversionStream(int inputStyle, int outputStyle, OutputStream out) {
94
95             super(out);
96
97             switch (inputStyle) {
98                     case TEXT_CRLF: {
99                              stripCR = false;
100                              stripLF = false;
101                              stripCRLF = true;
102                     } break;
103                     case TEXT_CR: {
104                              stripCR = true;
105                              stripLF = false;
106                              stripCRLF = false;
107                     } break;
108                     case TEXT_LF: {
109                              stripCR = false;
110                              stripLF = true;
111                              stripCRLF = false;
112                     } break;
113                     case TEXT_ALL: {
114                         stripCR = true;
115                         stripLF = true;
116                         stripCRLF = true;
117                     } break;
118                     default: {
119                             throw new IllegalArgumentException JavaDoc("Unknown text style: " + outputStyle);
120                     }
121             }
122
123
124             switch (outputStyle) {
125                     case TEXT_SYSTEM: {
126                              lineEnding = systemNL.getBytes();
127                     } break;
128                     case TEXT_CRLF: {
129                              lineEnding = new byte[]{(byte)'\r',(byte)'\n'};
130                     } break;
131                     case TEXT_CR: {
132                              lineEnding = new byte[]{(byte)'\r'};
133                     } break;
134                     case TEXT_LF: {
135                              lineEnding = new byte[]{(byte)'\n'};
136                     } break;
137                     case TEXT_ALL: {
138                         throw new IllegalArgumentException JavaDoc("TEXT_ALL cannot be used for an output style");
139                     }
140                     default: {
141                             throw new IllegalArgumentException JavaDoc("Unknown text style: " + outputStyle);
142                     }
143             }
144         }
145
146         /**
147          * Check to see if binary data was encountered during the encoding
148          * process
149          * @return boolean
150          */

151         public boolean hasBinary() {
152             return encounteredBinary;
153         }
154
155         public void write(int b) throws IOException {
156             write(new byte[] { (byte)b });
157         }
158
159         public void close() throws IOException {
160
161             if(lastCharacterWasCR && !stripCR)
162                 out.write('\r');
163
164             super.close();
165         }
166
167         /**
168          *
169          * @param buf byte[]
170          * @param off int
171          * @param len int
172          * @param out OutputStream
173          * @throws IOException
174          */

175         public void write(byte[] buf,
176                                     int off,
177                                     int len)
178                 throws IOException {
179
180
181                 BufferedInputStream bin = new BufferedInputStream(
182                          new ByteArrayInputStream(buf, off, len), 32768);
183
184                 int b;
185                 while((b = bin.read()) != -1){
186
187                     if (b == '\r') {
188
189                         if(stripCRLF) {
190                             bin.mark(1);
191                             int ch = bin.read();
192                             if(ch==-1) {
193                                 lastCharacterWasCR = true;
194                                 break;
195                             }
196                             if(ch == '\n') {
197                                 // This is STYLE_RN do we output as is or replace with NL?
198
out.write(lineEnding);
199
200                             } else {
201                                 // move the stream back and process a single CR
202
bin.reset();
203                                 if(stripCR) {
204                                     out.write(lineEnding);
205                                 } else
206                                     out.write(b);
207                             }
208                         } else {
209                             // This is STYLE_R do we output as is or replace with NL?
210
if (stripCR)
211                                 out.write(lineEnding);
212                             else
213                                 out.write(b);
214
215                         }
216                     } else if(b == '\n') {
217
218                         // Are we processing between blocks and was the last character a CR
219
if(lastCharacterWasCR) {
220                             out.write(lineEnding);
221                             lastCharacterWasCR = false;
222                         } else {
223                                 // This is STYLE_N do we output as is or replace with NL?
224
if (stripLF)
225                                 out.write(lineEnding);
226                             else
227                                 out.write(b);
228                         }
229                     } else {
230
231                         // Check for previous CR in stream
232
if(lastCharacterWasCR) {
233                             if(stripCR) {
234                                out.write(lineEnding);
235                             } else
236                                 out.write(b);
237                         }
238
239                         // Not an EOL
240
if(b != 't'
241                            && b != '\f'
242                            && (b & 0xff) < 32) {
243                             encounteredBinary = true;
244                         }
245
246                         out.write(b);
247                     }
248                 }
249
250         }
251
252
253         public static void main(String JavaDoc[] args) {
254
255             try {
256                 TextConversionStream t = new TextConversionStream(
257                               TEXT_CRLF,
258                               TEXT_CR,
259                               new FileOutputStream("C:\\TEXT.txt"));
260
261
262                 t.write("1234567890\r".getBytes());
263                 t.write("\n01234567890\r\n".getBytes());
264                 t.write("\r\n12323445546657".getBytes());
265                 t.write("21344356545656\r".getBytes());
266
267                 t.close();
268
269             } catch(Exception JavaDoc e) {
270             }
271
272         }
273
274
275
276 }
277
Popular Tags