2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.log.Logger;
27 /* ------------------------------------------------------------ */
29 * Utf8 Appendable abstract base class
31 * This abstract class wraps a standard {@link java.lang.Appendable} and provides methods to append UTF-8 encoded bytes, that are converted into characters.
33 * 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.
35 * The UTF-8 decoding is done by this class and no additional buffers or Readers are used. The UTF-8 code was inspired by
36 * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
38 * License information for Bjoern Hoehrmann's code:
40 * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
41 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
42 * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
47 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
48 * 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
49 * 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.
51 public abstract class Utf8Appendable
53 protected static final Logger LOG = Log.getLogger(Utf8Appendable.class);
54 public static final char REPLACEMENT = '\ufffd';
55 public static final byte[] REPLACEMENT_UTF8 = new byte[] {(byte)0xEF,(byte)0xBF,(byte)0xBD };
56 private static final int UTF8_ACCEPT = 0;
57 private static final int UTF8_REJECT = 12;
59 protected final Appendable _appendable;
60 protected int _state = UTF8_ACCEPT;
62 private static final byte[] BYTE_TABLE =
64 // The first part of the table maps bytes to character classes that
65 // to reduce the size of the transition table and create bitmasks.
66 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,
67 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,
68 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,
69 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,
70 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,
71 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,
72 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,
73 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
76 private static final byte[] TRANS_TABLE =
78 // The second part is a transition table that maps a combination
79 // of a state of the automaton and a character class to a state.
80 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
81 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
82 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
83 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
84 12,36,12,12,12,12,12,12,12,12,12,12
89 public Utf8Appendable(Appendable appendable)
91 _appendable = appendable;
94 public abstract int length();
96 protected void reset()
101 public void append(byte b)
107 catch (IOException e)
109 throw new RuntimeException(e);
113 public void append(ByteBuffer buf)
117 while (buf.remaining() > 0)
119 appendByte(buf.get());
122 catch (IOException e)
124 throw new RuntimeException(e);
128 public void append(byte[] b, int offset, int length)
132 int end = offset + length;
133 for (int i = offset; i < end; i++)
136 catch (IOException e)
138 throw new RuntimeException(e);
142 public boolean append(byte[] b, int offset, int length, int maxChars)
146 int end = offset + length;
147 for (int i = offset; i < end; i++)
149 if (length() > maxChars)
155 catch (IOException e)
157 throw new RuntimeException(e);
161 protected void appendByte(byte b) throws IOException
164 if (b > 0 && _state == UTF8_ACCEPT)
166 _appendable.append((char)(b & 0xFF));
171 int type = BYTE_TABLE[i];
172 _codep = _state == UTF8_ACCEPT ? (0xFF >> type) & i : (i & 0x3F) | (_codep << 6);
173 int next = TRANS_TABLE[_state + type];
179 if (_codep < Character.MIN_HIGH_SURROGATE)
181 _appendable.append((char)_codep);
185 for (char c : Character.toChars(_codep))
186 _appendable.append(c);
191 String reason = "byte "+TypeUtil.toHexString(b)+" in state "+(_state/12);
193 _state = UTF8_ACCEPT;
194 _appendable.append(REPLACEMENT);
195 throw new NotUtf8Exception(reason);
204 public boolean isUtf8SequenceComplete()
206 return _state == UTF8_ACCEPT;
209 @SuppressWarnings("serial")
210 public static class NotUtf8Exception extends IllegalArgumentException
212 public NotUtf8Exception(String reason)
214 super("Not valid UTF8! "+reason);
218 protected void checkState()
220 if (!isUtf8SequenceComplete())
223 _state = UTF8_ACCEPT;
226 _appendable.append(REPLACEMENT);
230 throw new RuntimeException(e);
232 throw new NotUtf8Exception("incomplete UTF8 sequence");
236 public String toReplacedString()
238 if (!isUtf8SequenceComplete())
241 _state = UTF8_ACCEPT;
244 _appendable.append(REPLACEMENT);
248 throw new RuntimeException(e);
250 Throwable th= new NotUtf8Exception("incomplete UTF8 sequence");
251 LOG.warn(th.toString());
254 return _appendable.toString();