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_16Encoder.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* UTF_16Encoder.java --
2
   Copyright (C) 2002 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.ByteOrder;
42
import java.nio.CharBuffer;
43
import java.nio.charset.Charset;
44
import java.nio.charset.CharsetEncoder;
45
import java.nio.charset.CoderResult;
46
 
47
/**
48
 * Encoder for UTF-16, UTF-15LE, and UTF-16BE.
49
 *
50
 * @author Jesse Rosenstock
51
 */
52
final class UTF_16Encoder extends CharsetEncoder
53
{
54
  // byte orders
55
  static final int BIG_ENDIAN = 0;
56
  static final int LITTLE_ENDIAN = 1;
57
 
58
  private static final char BYTE_ORDER_MARK = 0xFEFF;
59
 
60
  private final ByteOrder byteOrder;
61
  private final boolean useByteOrderMark;
62
  private boolean needsByteOrderMark;
63
 
64
  UTF_16Encoder (Charset cs, int byteOrder, boolean useByteOrderMark)
65
  {
66
    super (cs, 2.0f,
67
           useByteOrderMark ? 4.0f : 2.0f,
68
           byteOrder == BIG_ENDIAN
69
             ? new byte[] { (byte) 0xFF, (byte) 0xFD }
70
             : new byte[] { (byte) 0xFD, (byte) 0xFF });
71
    this.byteOrder = (byteOrder == BIG_ENDIAN) ?
72
        ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
73
    this.useByteOrderMark = useByteOrderMark;
74
    this.needsByteOrderMark = useByteOrderMark;
75
  }
76
 
77
  protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
78
  {
79
    // TODO: Optimize this in the case in.hasArray() / out.hasArray()
80
 
81
    ByteOrder originalBO = out.order();
82
    out.order(byteOrder);
83
 
84
    if (needsByteOrderMark)
85
      {
86
        if (out.remaining () < 2)
87
            {
88
                out.order(originalBO);
89
                return CoderResult.OVERFLOW;
90
            }
91
        out.putChar (BYTE_ORDER_MARK);
92
        needsByteOrderMark = false;
93
      }
94
 
95
    int inPos = in.position ();
96
    try
97
      {
98
        while (in.hasRemaining ())
99
          {
100
            char c = in.get ();
101
            if (0xD800 <= c && c <= 0xDFFF)
102
              {
103
                // c is a surrogate
104
 
105
                // make sure c is a high surrogate
106
                if (c > 0xDBFF)
107
                  return CoderResult.malformedForLength (1);
108
                if (in.remaining () < 1)
109
                  return CoderResult.UNDERFLOW;
110
                char d = in.get ();
111
                // make sure d is a low surrogate
112
                if (d < 0xDC00 || d > 0xDFFF)
113
                  return CoderResult.malformedForLength (1);
114
                out.putChar (c);
115
                out.putChar (d);
116
                inPos += 2;
117
              }
118
            else
119
              {
120
                if (out.remaining () < 2)
121
                  {
122
                    out.order(originalBO);
123
                    return CoderResult.OVERFLOW;
124
                  }
125
                out.putChar (c);
126
                inPos++;
127
              }
128
          }
129
        out.order(originalBO);
130
        return CoderResult.UNDERFLOW;
131
      }
132
    finally
133
      {
134
        in.position (inPos);
135
      }
136
  }
137
 
138
  protected void implReset ()
139
  {
140
    needsByteOrderMark = useByteOrderMark;
141
  }
142
 
143
  // TODO: override canEncode(char) and canEncode(CharSequence)
144
  // for performance
145
}

powered by: WebSVN 2.1.0

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