OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [nio/] [charset/] [UTF_16Decoder.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* UTF_16Decoder.java --
2
   Copyright (C) 2002, 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
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.
25
 
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. */
37
 
38
package gnu.java.nio.charset;
39
 
40
import java.nio.ByteBuffer;
41
import java.nio.CharBuffer;
42
import java.nio.charset.Charset;
43
import java.nio.charset.CharsetDecoder;
44
import java.nio.charset.CoderResult;
45
 
46
/**
47
 * Decoder for UTF-16, UTF-15LE, and UTF-16BE.
48
 *
49
 * @author Jesse Rosenstock
50
 */
51
final class UTF_16Decoder extends CharsetDecoder
52
{
53
  // byte orders
54
  static final int BIG_ENDIAN = 0;
55
  static final int LITTLE_ENDIAN = 1;
56
  static final int UNKNOWN_ENDIAN = 2;
57
  static final int MAYBE_BIG_ENDIAN = 3;
58
  static final int MAYBE_LITTLE_ENDIAN = 4;
59
 
60
  private static final char BYTE_ORDER_MARK = 0xFEFF;
61
  private static final char REVERSED_BYTE_ORDER_MARK = 0xFFFE;
62
 
63
  private final int originalByteOrder;
64
  private int byteOrder;
65
 
66
  UTF_16Decoder (Charset cs, int byteOrder)
67
  {
68
    super (cs, 0.5f, 1.0f);
69
    this.originalByteOrder = byteOrder;
70
    this.byteOrder = byteOrder;
71
  }
72
 
73
  protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
74
  {
75
    // TODO: Optimize this in the case in.hasArray() / out.hasArray()
76
 
77
    int inPos = in.position ();
78
    try
79
      {
80
        while (in.remaining () >= 2)
81
          {
82
            byte b1 = in.get ();
83
            byte b2 = in.get ();
84
 
85
            // handle byte order mark
86
            if (byteOrder == UNKNOWN_ENDIAN ||
87
                byteOrder == MAYBE_BIG_ENDIAN ||
88
                byteOrder == MAYBE_LITTLE_ENDIAN)
89
              {
90
                char c = (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF));
91
                if (c == BYTE_ORDER_MARK)
92
                  {
93
                    if (byteOrder == MAYBE_LITTLE_ENDIAN)
94
                      {
95
                        return CoderResult.malformedForLength (2);
96
                      }
97
                    byteOrder = BIG_ENDIAN;
98
                    inPos += 2;
99
                    continue;
100
                  }
101
                else if (c == REVERSED_BYTE_ORDER_MARK)
102
                  {
103
                    if (byteOrder == MAYBE_BIG_ENDIAN)
104
                      {
105
                        return CoderResult.malformedForLength (2);
106
                      }
107
                    byteOrder = LITTLE_ENDIAN;
108
                    inPos += 2;
109
                    continue;
110
                  }
111
                else
112
                  {
113
                    // assume big or little endian, do not consume bytes,
114
                    // continue with normal processing
115
                    byteOrder = (byteOrder == MAYBE_LITTLE_ENDIAN ?
116
                                 LITTLE_ENDIAN : BIG_ENDIAN);
117
                  }
118
              }
119
 
120
            // FIXME: Change so you only do a single comparison here.
121
            char c = (byteOrder == BIG_ENDIAN
122
                      ? (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF))
123
                      : (char) (((b2 & 0xFF) << 8) | (b1 & 0xFF)));
124
 
125
            if (0xD800 <= c && c <= 0xDFFF)
126
              {
127
                // c is a surrogate
128
 
129
                // make sure c is a high surrogate
130
                if (c > 0xDBFF)
131
                  return CoderResult.malformedForLength (2);
132
                if (in.remaining () < 2)
133
                  return CoderResult.UNDERFLOW;
134
                byte b3 = in.get ();
135
                byte b4 = in.get ();
136
                char d = (byteOrder == BIG_ENDIAN
137
                          ? (char) (((b3 & 0xFF) << 8) | (b4 & 0xFF))
138
                          : (char) (((b4 & 0xFF) << 8) | (b3 & 0xFF)));
139
                // make sure d is a low surrogate
140
                if (d < 0xDC00 || d > 0xDFFF)
141
                  return CoderResult.malformedForLength (2);
142
                out.put (c);
143
                out.put (d);
144
                inPos += 4;
145
              }
146
            else
147
              {
148
                if (!out.hasRemaining ())
149
                  return CoderResult.UNDERFLOW;
150
                out.put (c);
151
                inPos += 2;
152
              }
153
          }
154
 
155
        return CoderResult.UNDERFLOW;
156
      }
157
    finally
158
      {
159
        in.position (inPos);
160
      }
161
  }
162
 
163
  protected void implReset ()
164
  {
165
    byteOrder = originalByteOrder;
166
  }
167
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.