KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > encoders > BufferedDecoder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.geronimo.util.encoders;
19
20
21 /**
22  * a buffering class to allow translation from one format to another to
23  * be done in discrete chunks.
24  */

25 public class BufferedDecoder
26 {
27     protected byte[] buf;
28     protected int bufOff;
29
30     protected Translator translator;
31
32     /**
33      * @param translator the translator to use.
34      * @param bufSize amount of input to buffer for each chunk.
35      */

36     public BufferedDecoder(
37         Translator translator,
38         int bufSize)
39     {
40         this.translator = translator;
41
42         if ((bufSize % translator.getEncodedBlockSize()) != 0)
43         {
44             throw new IllegalArgumentException JavaDoc("buffer size not multiple of input block size");
45         }
46
47         buf = new byte[bufSize];
48         bufOff = 0;
49     }
50
51     public int processByte(
52         byte in,
53         byte[] out,
54         int outOff)
55     {
56         int resultLen = 0;
57
58         buf[bufOff++] = in;
59
60         if (bufOff == buf.length)
61         {
62             resultLen = translator.decode(buf, 0, buf.length, out, outOff);
63             bufOff = 0;
64         }
65
66         return resultLen;
67     }
68
69     public int processBytes(
70         byte[] in,
71         int inOff,
72         int len,
73         byte[] out,
74         int outOff)
75     {
76         if (len < 0)
77         {
78             throw new IllegalArgumentException JavaDoc("Can't have a negative input length!");
79         }
80
81         int resultLen = 0;
82         int gapLen = buf.length - bufOff;
83
84         if (len > gapLen)
85         {
86             System.arraycopy(in, inOff, buf, bufOff, gapLen);
87
88             resultLen += translator.decode(buf, 0, buf.length, out, outOff);
89
90             bufOff = 0;
91
92             len -= gapLen;
93             inOff += gapLen;
94             outOff += resultLen;
95
96             int chunkSize = len - (len % buf.length);
97
98             resultLen += translator.decode(in, inOff, chunkSize, out, outOff);
99
100             len -= chunkSize;
101             inOff += chunkSize;
102         }
103
104         if (len != 0)
105         {
106             System.arraycopy(in, inOff, buf, bufOff, len);
107
108             bufOff += len;
109         }
110
111         return resultLen;
112     }
113 }
114
Popular Tags