KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > CharArraysReader


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * CharArraysReader.java
25  *
26  * Created on 23 mai 2001, 18:15
27  */

28
29 package org.xquark.mapper.util;
30
31 import java.io.IOException JavaDoc;
32 import java.io.Reader JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35
36 /**
37  * Reader implementation basing on multiples char arrays avoiding allocations.
38  *
39  * <p>Intended to be used for direct transmission of SAX characters to JDBC2
40  * setCharArrayStream.</p>
41  */

42 public class CharArraysReader extends Reader JavaDoc
43 {
44 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
45 private static final String JavaDoc RCSName = "$Name: $";
46
47     private ArrayList JavaDoc buffers;
48     private int current = 0;
49     private int offset = 0;
50     private int totalLength = 0;
51     
52     /** Creates new CharArraysReader */
53     public CharArraysReader()
54     {
55         buffers = new ArrayList JavaDoc();
56     }
57     
58     public CharArraysReader(char[] str, int offset, int len)
59     {
60         this();
61         append(str, offset, len);
62     }
63     
64     public void append(char[] str, int offset, int len)
65     {
66         // MAY HAVE TO PROCEED A COPY BECAUSE CHAR BUFFER MIGHT CHANGE BEFORE END_ELEMENT ?
67
buffers.add(new CharBuffer(str, offset, len));
68         totalLength += len;
69     }
70
71     public int getLength()
72     {
73         return totalLength;
74     }
75
76     public void clear()
77     {
78         buffers.clear();
79         current = 0;
80         totalLength = 0;
81         offset = 0;
82     }
83     
84     //////////////////////////////////////////////////////
85
// Implementation of Reader
86
//////////////////////////////////////////////////////
87
public void close() throws IOException JavaDoc
88     {
89         clear();
90     }
91     
92     public int read(char[] values, int off, int len) throws IOException JavaDoc
93     {
94         if (current < buffers.size())
95         {
96             CharBuffer cb;
97             int remaining = len;
98             int currentRemaining;
99             int toRead;
100             while (remaining > 0)
101             {
102                 cb = (CharBuffer)buffers.get(current);
103                 currentRemaining = cb.len - offset;
104                 if (remaining >= currentRemaining)
105                 {
106                     toRead = currentRemaining;
107                     current++;
108                     offset = 0;
109                     if (current == buffers.size())
110                         break;
111                 }
112                 else
113                 {
114                     toRead = remaining;
115                     offset += toRead;
116                 }
117                 System.arraycopy(cb.chars, cb.off + offset, values, off + (len - remaining), toRead);
118                 remaining -= toRead;
119             }
120             return (len - remaining);
121         }
122         else
123             return -1;
124     }
125     
126     public void reset() throws IOException JavaDoc
127     {
128         current = 0;
129         offset = 0;
130     }
131     //////////////////////////////////////////////////////
132
// private utilities
133
//////////////////////////////////////////////////////
134
private class CharBuffer
135     {
136         char[] chars;
137         int off;
138         int len;
139         CharBuffer(char[] str, int offset, int len)
140         {
141             this.chars = str;
142             this.off = offset;
143             this.len = len;
144         }
145     }
146 }
147
Popular Tags