/[classpath]/classpath/gnu/java/io/decode/DecoderUTF8.java
ViewVC logotype

Diff of /classpath/gnu/java/io/decode/DecoderUTF8.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by jfrijters, Wed Nov 17 15:14:32 2004 UTC revision 1.8 by jfrijters, Fri Nov 19 14:57:03 2004 UTC
# Line 56  public class DecoderUTF8 extends Decoder Line 56  public class DecoderUTF8 extends Decoder
56  {  {
57  private static final char REPLACEMENT_CHARACTER = '\uFFFD';  private static final char REPLACEMENT_CHARACTER = '\uFFFD';
58    
59    // If we're operating in stream mode and we encounter a surrogate pair that
60    // we can't fit in the output buffer, we use this field to store the
61    // second half of the surrogate pair.
62    private int pendingChar = -1;
63    
64  /*************************************************************************/  /*************************************************************************/
65    
66  /*  /*
# Line 81  public int Line 86  public int
86  charsInByteArray(byte[] buf, int offset, int len)  charsInByteArray(byte[] buf, int offset, int len)
87  {  {
88      int more = 0;      int more = 0;
89        int val = 0;
90      int num_chars = 0;      int num_chars = 0;
91    
92      for (int i = offset; i < offset + len; i++)      for (int i = offset; i < offset + len; i++)
# Line 100  charsInByteArray(byte[] buf, int offset, Line 106  charsInByteArray(byte[] buf, int offset,
106                  num_chars++; // ?                  num_chars++; // ?
107              else              else
108                {                {
109                    val <<= 6;
110                    val |= b & 0x3F;
111                  more--;                  more--;
112                  if (more == 0)                  if (more == 0)
113                      {
114                      num_chars++;                      num_chars++;
115                        if (val >= 0x10000)
116                            num_chars++;
117                      }
118                }                }
119            }            }
120          else          else
# Line 110  charsInByteArray(byte[] buf, int offset, Line 122  charsInByteArray(byte[] buf, int offset,
122              if (more != 0)              if (more != 0)
123                  num_chars++; // ?                  num_chars++; // ?
124    
125              if ((b & 0xF0) == 0xE0)              if ((b & 0xF8) == 0xF0)
126                  {
127                    val = b & 0x07;
128                    more = 3;
129                  }
130                else if ((b & 0xF0) == 0xE0)
131                  {
132                    val = b & 0x0F;
133                  more = 2;                  more = 2;
134                  }
135              else if ((b & 0xE0) == 0xC0)              else if ((b & 0xE0) == 0xC0)
136                  {
137                    val = b & 0x1F;
138                  more = 1;                  more = 1;
139                  }
140              else              else
141                  num_chars++; // ?                  num_chars++; // ?
142            }            }
# Line 155  convertToChars(byte[] buf, int buf_offse Line 178  convertToChars(byte[] buf, int buf_offse
178                  val |= b & 0x3F;                  val |= b & 0x3F;
179                  more--;                  more--;
180                  if (more == 0)                  if (more == 0)
181                      cbuf[cbuf_offset++] = (char)val;                    {
182                        if (val < 0x10000)
183                            cbuf[cbuf_offset++] = (char)val;
184                        else
185                          {
186                            val -= 0x10000;
187                            cbuf[cbuf_offset++] = (char)(0xD800 + (val >> 10));
188                            cbuf[cbuf_offset++] = (char)(0xDC00 + (val & 0x3FF));
189                          }
190                      }
191                }                }
192            }            }
193          else          else
# Line 163  convertToChars(byte[] buf, int buf_offse Line 195  convertToChars(byte[] buf, int buf_offse
195              if (more != 0)              if (more != 0)
196                  cbuf[cbuf_offset++] = REPLACEMENT_CHARACTER;                  cbuf[cbuf_offset++] = REPLACEMENT_CHARACTER;
197    
198              if ((b & 0xF0) == 0xE0)              if ((b & 0xF8) == 0xF0)
199                  {
200                    val = b & 0x07;
201                    more = 3;
202                  }
203                else if ((b & 0xF0) == 0xE0)
204                {                {
205                  val = b & 0x0F;                  val = b & 0x0F;
206                  more = 2;                  more = 2;
# Line 193  read(char[] cbuf, int offset, int len) t Line 230  read(char[] cbuf, int offset, int len) t
230      int more = 0;      int more = 0;
231      int val = 0;      int val = 0;
232    
233        if (pendingChar != -1 && len > 0)
234          {
235            cbuf[offset++] = (char)pendingChar;
236            pendingChar = -1;
237          }
238    
239      while (offset < start_offset + len)      while (offset < start_offset + len)
240        {        {
241          int b = in.read();          int b = in.read();
# Line 222  read(char[] cbuf, int offset, int len) t Line 265  read(char[] cbuf, int offset, int len) t
265                  val |= b & 0x3F;                  val |= b & 0x3F;
266                  more--;                  more--;
267                  if (more == 0)                  if (more == 0)
268                      cbuf[offset++] = (char)val;                    {
269                        if (val < 0x10000)
270                            cbuf[offset++] = (char)val;
271                        else
272                          {
273                            val -= 0x10000;
274                            cbuf[offset++] = (char)(0xD800 + (val >> 10));
275                            if (offset < start_offset + len)
276                                cbuf[offset++] = (char)(0xDC00 + (val & 0x3FF));
277                            else
278                                pendingChar = (char)(0xDC00 + (val & 0x3FF));
279                          }
280                      }
281                }                }
282            }            }
283          else          else
# Line 230  read(char[] cbuf, int offset, int len) t Line 285  read(char[] cbuf, int offset, int len) t
285              if (more != 0)              if (more != 0)
286                  cbuf[offset++] = REPLACEMENT_CHARACTER;                  cbuf[offset++] = REPLACEMENT_CHARACTER;
287    
288              if ((b & 0xF0) == 0xE0)              if ((b & 0xF8) == 0xF0)
289                  {
290                    val = b & 0x07;
291                    more = 3;
292                  }
293                else if ((b & 0xF0) == 0xE0)
294                {                {
295                  val = b & 0x0F;                  val = b & 0x0F;
296                  more = 2;                  more = 2;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26