This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / gnu / java / nio / ChannelReader.java
blob11e66a77ebb48a2e37ca7828d02e2b15f108bf31
1 /* ChannelReader.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.nio;
41 import java.io.IOException;
42 import java.io.Reader;
43 import java.nio.ByteBuffer;
44 import java.nio.CharBuffer;
45 import java.nio.channels.ReadableByteChannel;
46 import java.nio.charset.CharsetDecoder;
47 import java.nio.charset.CoderResult;
48 import java.nio.charset.CodingErrorAction;
50 /**
51 * A Reader implementation that works using a ReadableByteChannel and a
52 * CharsetDecoder.
54 * <p>
55 * This is a bridge between NIO <-> IO character decoding.
56 * </p>
58 * @author Robert Schuster
60 public class ChannelReader extends Reader
63 private static final int DEFAULT_BUFFER_CAP = 8192;
65 private ReadableByteChannel channel;
67 private CharsetDecoder decoder;
69 private ByteBuffer byteBuffer;
71 private CharBuffer charBuffer;
73 public ChannelReader(ReadableByteChannel channel, CharsetDecoder decoder,
74 int minBufferCap)
76 this.channel = channel;
77 this.decoder = decoder;
79 // JDK reports errors, so we do the same.
80 decoder.onMalformedInput(CodingErrorAction.REPORT);
81 decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
82 decoder.reset();
84 int size = (minBufferCap == -1) ? DEFAULT_BUFFER_CAP : minBufferCap;
86 // Allocates the buffers and prepares them for reading, because that is the
87 // first operation being done on them.
88 byteBuffer = ByteBuffer.allocate(size);
89 byteBuffer.flip();
90 charBuffer = CharBuffer.allocate((int) (size * decoder.averageCharsPerByte()));
93 public int read(char[] buf, int offset, int count) throws IOException
95 // I declared channel being null meaning that the reader is closed.
96 if (!channel.isOpen())
97 throw new IOException("Reader was already closed.");
99 // I declared decoder being null meaning that there is no more data to read
100 // and convert.
101 if (decoder == null)
102 return -1;
104 // Stores the amount of character being read. It -1 so that if no conversion
105 // occured the caller will see this as an 'end of file'.
106 int sum = -1;
108 // Copies any characters which may be left from the last invocation into the
109 // destination array.
110 if (charBuffer.remaining() > 0)
112 sum = Math.min(count, charBuffer.remaining());
113 charBuffer.get(buf, offset, sum);
115 // Updates the control variables according to the latest copy operation.
116 offset += sum;
117 count -= sum;
120 // Copies the character which have not been put in the destination array to
121 // the beginning. If data is actually copied count will be 0. If no data is
122 // copied count is >0 and we can now convert some more characters.
123 charBuffer.compact();
125 int converted = 0;
126 boolean last = false;
128 while (count != 0)
130 // Tries to convert some bytes (Which will intentionally fail in the
131 // first place because we have not read any bytes yet.)
132 CoderResult result = decoder.decode(byteBuffer, charBuffer, last);
133 if (result.isMalformed() || result.isUnmappable())
135 // JDK throws exception when bytes are malformed for sure.
136 // FIXME: Unsure what happens when a character is simply
137 // unmappable.
138 result.throwException();
141 // Marks that we should end this loop regardless whether the caller
142 // wants more chars or not, when this was the last conversion.
143 if (last)
145 decoder = null;
147 else if (result.isUnderflow())
149 // We need more bytes to do the conversion.
151 // Copies the not yet converted bytes to the beginning making it
152 // being able to receive more bytes.
153 byteBuffer.compact();
155 // Reads in another bunch of bytes for being converted.
156 if (channel.read(byteBuffer) == -1)
158 // If there is no more data available in the channel we mark
159 // that state for the final character conversion run which is
160 // done in the next loop iteration.
161 last = true;
164 // Prepares the byteBuffer for the next character conversion run.
165 byteBuffer.flip();
168 // Prepares the charBuffer for being drained.
169 charBuffer.flip();
171 converted = Math.min(count, charBuffer.remaining());
172 charBuffer.get(buf, offset, converted);
174 // Copies characters which have not yet being copied into the char-Array
175 // to the beginning making it possible to read them later (If data is
176 // really copied here, then the caller has received enough characters so
177 // far.).
178 charBuffer.compact();
180 // Updates the control variables according to the latest copy operation.
181 offset += converted;
182 count -= converted;
184 // Updates the amount of transferred characters.
185 sum += converted;
187 if (decoder == null)
189 break;
192 // Now that more characters have been transfered we let the loop decide
193 // what to do next.
196 // Makes the charBuffer ready for reading on the next invocation.
197 charBuffer.flip();
199 return sum;
202 public void close() throws IOException
204 channel.close();
206 // Makes sure all intermediate data is released by the decoder.
207 if (decoder != null)
208 decoder.reset();