View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  package org.eclipse.jetty.util;
14  
15  import java.io.IOException;
16  
17  /* ------------------------------------------------------------ */
18  /**
19   * Utf8 Appendable abstract base class
20   *
21   * This abstract class wraps a standard {@link java.lang.Appendable} and provides methods to append UTF-8 encoded bytes, that are converted into characters.
22   *
23   * This class is stateful and up to 4 calls to {@link #append(byte)} may be needed before state a character is appended to the string buffer.
24   *
25   * The UTF-8 decoding is done by this class and no additional buffers or Readers are used. The UTF-8 code was inspired by
26   * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
27   *
28   * License information for Bjoern Hoehrmann's code:
29   *
30   * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
31   * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
32   * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33   * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
34   *
35   * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36   *
37   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
38   * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39   * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40   **/
41  public abstract class Utf8Appendable
42  {
43      private final char REPLACEMENT = '\ufffd';
44      private static final int UTF8_ACCEPT = 0;
45      private static final int UTF8_REJECT = 12;
46  
47      protected final Appendable _appendable;
48      protected int _state = UTF8_ACCEPT;
49  
50      private static final byte[] BYTE_TABLE =
51      {
52          // The first part of the table maps bytes to character classes that
53          // to reduce the size of the transition table and create bitmasks.
54           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
58           1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
59           7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
60           8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
61          10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8
62      };
63  
64      private static final byte[] TRANS_TABLE =
65      {
66          // The second part is a transition table that maps a combination
67          // of a state of the automaton and a character class to a state.
68           0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
69          12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
70          12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
71          12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
72          12,36,12,12,12,12,12,12,12,12,12,12
73      };
74  
75      private int _codep;
76  
77      public Utf8Appendable(Appendable appendable)
78      {
79          _appendable = appendable;
80      }
81  
82      public abstract int length();
83  
84      protected void reset()
85      {
86          _state = UTF8_ACCEPT;
87      }
88  
89      public void append(byte b)
90      {
91          try
92          {
93              appendByte(b);
94          }
95          catch (IOException e)
96          {
97              throw new RuntimeException(e);
98          }
99      }
100 
101     public void append(byte[] b, int offset, int length)
102     {
103         try
104         {
105             int end = offset + length;
106             for (int i = offset; i < end; i++)
107                 appendByte(b[i]);
108         }
109         catch (IOException e)
110         {
111             throw new RuntimeException(e);
112         }
113     }
114 
115     public boolean append(byte[] b, int offset, int length, int maxChars)
116     {
117         try
118         {
119             int end = offset + length;
120             for (int i = offset; i < end; i++)
121             {
122                 if (length() > maxChars)
123                     return false;
124                 appendByte(b[i]);
125             }
126             return true;
127         }
128         catch (IOException e)
129         {
130             throw new RuntimeException(e);
131         }
132     }
133 
134     protected void appendByte(byte b) throws IOException
135     {
136 
137         if (b > 0 && isUtf8SequenceComplete())
138         {
139             _appendable.append((char)(b & 0xFF));
140         }
141         else
142         {
143             int i = b & 0xFF;
144             int type = BYTE_TABLE[i];
145             _codep = isUtf8SequenceComplete() ? (0xFF >> type) & i : (i & 0x3F) | (_codep << 6);
146             _state = TRANS_TABLE[_state + type];
147 
148             if (isUtf8SequenceComplete())
149             {
150                 if (_codep < Character.MIN_HIGH_SURROGATE)
151                 {
152                     _appendable.append((char)_codep);
153                 }
154                 else
155                 {
156                     for (char c : Character.toChars(_codep))
157                         _appendable.append(c);
158                 }
159             }
160             else if (_state == UTF8_REJECT)
161             {
162                 _codep=0;
163                 _state = UTF8_ACCEPT;
164                 _appendable.append(REPLACEMENT);
165                 throw new NotUtf8Exception();
166             }
167         }
168     }
169 
170     protected boolean isUtf8SequenceComplete()
171     {
172         return _state == UTF8_ACCEPT;
173     }
174 
175     public static class NotUtf8Exception extends IllegalArgumentException
176     {
177         public NotUtf8Exception()
178         {
179             super("Not valid UTF8!");
180         }
181     }
182 }