KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > io > AbstractCharDecoder


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util.io;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 /**
24  * This class is the superclass of all the char decoders.
25  *
26  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: AbstractCharDecoder.java,v 1.4 2004/08/18 07:15:58 vhardy Exp $
28  */

29 public abstract class AbstractCharDecoder implements CharDecoder {
30
31     /**
32      * The buffer size.
33      */

34     protected final static int BUFFER_SIZE = 8192;
35
36     /**
37      * The input stream to read.
38      */

39     protected InputStream JavaDoc inputStream;
40     
41     /**
42      * The input buffer.
43      */

44     protected byte[] buffer = new byte[BUFFER_SIZE];
45
46     /**
47      * The current position in the buffer.
48      */

49     protected int position;
50
51     /**
52      * The byte count in the buffer.
53      */

54     protected int count;
55
56     /**
57      * Creates a new CharDecoder object.
58      * @param is The stream to read.
59      */

60     protected AbstractCharDecoder(InputStream JavaDoc is) {
61         inputStream = is;
62     }
63
64     /**
65      * Disposes the associated resources.
66      */

67     public void dispose() throws IOException JavaDoc {
68         inputStream.close();
69         inputStream = null;
70     }
71
72     /**
73      * Fills the input buffer.
74      */

75     protected void fillBuffer() throws IOException JavaDoc {
76         count = inputStream.read(buffer, 0, BUFFER_SIZE);
77         position = 0;
78     }
79
80     /**
81      * To throws an exception when the input stream contains an
82      * invalid character.
83      * @param encoding The encoding name.
84      */

85     protected void charError(String JavaDoc encoding) throws IOException JavaDoc {
86         throw new IOException JavaDoc
87             (Messages.formatMessage("invalid.char",
88                                     new Object JavaDoc[] { encoding }));
89     }
90
91     /**
92      * To throws an exception when the end of stream was unexpected.
93      * @param encoding The encoding name.
94      */

95     protected void endOfStreamError(String JavaDoc encoding) throws IOException JavaDoc {
96         throw new IOException JavaDoc
97             (Messages.formatMessage("end.of.stream",
98                                     new Object JavaDoc[] { encoding }));
99     }
100 }
101
Popular Tags