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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [nio/] [charset/] [ByteCharset.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* ByteCharset.java -- Abstract class for generic 1-byte encodings.
2
   Copyright (C) 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.CharsetEncoder;
45
import java.nio.charset.CoderResult;
46
 
47
/**
48
 * A generic encoding framework for single-byte encodings,
49
 * utilizing a look-up table.
50
 *
51
 * This replaces the gnu.java.io.EncoderEightBitLookup class,
52
 * created by Aron Renn.
53
 *
54
 * @author Sven de Marothy
55
 */
56
abstract class ByteCharset extends Charset
57
{
58
  protected char[] lookupTable;
59
    /**
60
     * Char to signify the character in the table is undefined
61
     */
62
  protected static final char NONE = (char)0xFFFD;
63
 
64
  ByteCharset (String canonicalName, String[] aliases)
65
  {
66
    super (canonicalName, aliases);
67
  }
68
 
69
  /**
70
   * Most western charsets include ASCII, but this should
71
   * be overloaded for others.
72
   */
73
  public boolean contains (Charset cs)
74
  {
75
    return cs instanceof US_ASCII || (cs.getClass() == getClass());
76
  }
77
 
78
  char[] getLookupTable()
79
  {
80
    return lookupTable;
81
  }
82
 
83
  public CharsetDecoder newDecoder ()
84
  {
85
    return new Decoder (this);
86
  }
87
 
88
  public CharsetEncoder newEncoder ()
89
  {
90
    return new Encoder (this);
91
  }
92
 
93
  private static final class Decoder extends CharsetDecoder
94
  {
95
    private char[] lookup;
96
 
97
    // Package-private to avoid a trampoline constructor.
98
    Decoder (ByteCharset cs)
99
    {
100
      super (cs, 1.0f, 1.0f);
101
      lookup = cs.getLookupTable();
102
    }
103
 
104
    protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
105
    {
106
      // TODO: Optimize this in the case in.hasArray() / out.hasArray()
107
      while (in.hasRemaining ())
108
      {
109
        byte b = in.get ();
110
        char c;
111
 
112
        if (!out.hasRemaining ())
113
          {
114
            in.position (in.position () - 1);
115
            return CoderResult.OVERFLOW;
116
          }
117
 
118
        if((c = lookup[(int) (b & 0xFF)]) == NONE);
119
        //        return CoderResult.unmappableForLength (1);           
120
        out.put (c);
121
      }
122
 
123
      return CoderResult.UNDERFLOW;
124
    }
125
  }
126
 
127
  private static final class Encoder extends CharsetEncoder
128
  {
129
    private byte[] lookup;
130
 
131
    // Package-private to avoid a trampoline constructor.
132
    Encoder (ByteCharset cs)
133
    {
134
      super (cs, 1.0f, 1.0f);
135
 
136
      char[] lookup_table = cs.getLookupTable();
137
 
138
      // Create the inverse look-up table.
139
      // determine required size of encoding_table: 
140
      int max = 0;
141
      for (int i = 0; i < lookup_table.length; i++)
142
          {
143
              int c = (int)lookup_table[i];
144
              max = (c > max && c < NONE) ? c : max;
145
          }
146
 
147
      lookup = new byte[max+1];
148
 
149
      for (int i = 0; i < lookup_table.length; i++)
150
          {
151
            int c = (int)lookup_table[i];
152
            if (c != 0 && c < NONE)
153
              {
154
                lookup[c] = (byte)i;
155
              }
156
          }
157
    }
158
 
159
    protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
160
    {
161
      // TODO: Optimize this in the case in.hasArray() / out.hasArray()
162
      while (in.hasRemaining ())
163
      {
164
        int c = (int)in.get ();
165
 
166
        if (!out.hasRemaining ())
167
          {
168
            in.position (in.position () - 1);
169
            return CoderResult.OVERFLOW;
170
          }
171
 
172
        // lookup byte encoding
173
        byte b = (c < lookup.length) ? lookup[c] : (byte)0;
174
 
175
        if ((int)b != 0 || (int)c == 0)
176
            {
177
                out.put (b);
178
            } else {
179
                in.position (in.position () - 1);
180
                return CoderResult.unmappableForLength (1);
181
            }
182
      }
183
 
184
      return CoderResult.UNDERFLOW;
185
    }
186
  }
187
}

powered by: WebSVN 2.1.0

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