KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.InputStream JavaDoc;
24
25 class EOLProcessorInputStream extends InputStream JavaDoc {
26
27     EOLProcessor processor;
28     InputStream JavaDoc in;
29     DynamicBuffer buf = new DynamicBuffer();
30     byte[] tmp = new byte[32768];
31
32     public EOLProcessorInputStream(int inputStyle,
33             int outputStyle,
34             InputStream JavaDoc in) throws IOException {
35         this.in = in;
36         processor = new EOLProcessor(inputStyle,
37                     outputStyle,
38                     buf.getOutputStream());
39     }
40
41     /**
42      * Reads the next byte of data from the input stream.
43      *
44      * @return the next byte of data, or <code>-1</code> if the end of the
45      * stream is reached.
46      * @throws IOException if an I/O error occurs.
47      * @todo Implement this java.io.InputStream method
48      */

49     public int read() throws IOException {
50         fillBuffer(1);
51         return buf.getInputStream().read();
52     }
53
54     public int available() throws IOException {
55         return in.available();
56     }
57
58     public int read(byte[] b, int off, int len) throws IOException {
59         fillBuffer(len);
60         return buf.getInputStream().read(b, off, len);
61     }
62
63     private void fillBuffer(int count) throws IOException {
64
65         while(buf.available() < count) {
66             int read = in.read(tmp);
67             if(read == -1) {
68                 buf.close();
69                 return;
70             }
71             processor.processBytes(tmp, 0, read);
72         }
73     }
74     
75     public void close() throws IOException {
76         in.close();
77     }
78 }
79
Popular Tags