KickJava   Java API By Example, From Geeks To Geeks.

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


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 EOLProcessor {
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 = false;;
87         boolean stripLF = false;
88         boolean stripCRLF = false;
89         boolean encounteredBinary = false;
90
91         boolean lastCharacterWasCR = false;
92
93         OutputStream out;
94
95         public EOLProcessor(int inputStyle, int outputStyle, OutputStream out) throws IOException {
96
97             this.out = out;
98
99             switch (inputStyle) {
100                     case TEXT_CRLF: {
101                              stripCRLF = true;
102                     } break;
103                     case TEXT_CR: {
104                              stripCR = true;
105                     } break;
106                     case TEXT_LF: {
107                              stripLF = true;
108                     } break;
109                     case TEXT_ALL: {
110                         stripCR = true;
111                         stripLF = true;
112                         stripCRLF = true;
113                     } break;
114                     case TEXT_SYSTEM:
115                     {
116                         byte[] tmp = systemNL.getBytes();
117                         if(tmp.length == 2 && tmp[0] == '\r' && tmp[1] == '\n') {
118                             stripCRLF = true;
119                         } else if(tmp.length==1 && tmp[0] == '\r') {
120                             stripCR = true;
121                         } else if(tmp.length==1 && tmp[0] == '\n') {
122                             stripLF = true;
123                         } else {
124                            throw new IOException("Unsupported system EOL mode");
125                         }
126                         break;
127                     }
128                     default: {
129                             throw new IllegalArgumentException JavaDoc("Unknown text style: " + outputStyle);
130                     }
131             }
132
133
134             switch (outputStyle) {
135                     case TEXT_SYSTEM: {
136                              lineEnding = systemNL.getBytes();
137                     } break;
138                     case TEXT_CRLF: {
139                              lineEnding = new byte[]{(byte)'\r',(byte)'\n'};
140                     } break;
141                     case TEXT_CR: {
142                              lineEnding = new byte[]{(byte)'\r'};
143                     } break;
144                     case TEXT_LF: {
145                              lineEnding = new byte[]{(byte)'\n'};
146                     } break;
147                     case TEXT_ALL: {
148                         throw new IllegalArgumentException JavaDoc("TEXT_ALL cannot be used for an output style");
149                     }
150                     default: {
151                             throw new IllegalArgumentException JavaDoc("Unknown text style: " + outputStyle);
152                     }
153             }
154         }
155
156         /**
157          * Check to see if binary data was encountered during the encoding
158          * process
159          * @return boolean
160          */

161         public boolean hasBinary() {
162             return encounteredBinary;
163         }
164
165         public void close() throws IOException {
166
167             if(lastCharacterWasCR && !stripCR)
168                 out.write('\r');
169             
170             out.close();
171         }
172
173         /**
174          *
175          * @param buf byte[]
176          * @param off int
177          * @param len int
178          * @param out OutputStream
179          * @throws IOException
180          */

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