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.nio.ByteBuffer;
23 import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
26 * Stateful parser for lines of UTF8 formatted text, looking for <code>"\n"</code> as a line termination character.
28 * For use with new IO framework that is based on ByteBuffer parsing.
30 public class Utf8LineParser
40 private Utf8StringBuilder utf;
42 public Utf8LineParser()
44 this.state = State.START;
48 * Parse a ByteBuffer (could be a partial buffer), and return once a complete line of UTF8 parsed text has been reached.
51 * the buffer to parse (could be an incomplete buffer)
52 * @return the line of UTF8 parsed text, or null if no line end termination has been reached within the {@link ByteBuffer#remaining() remaining} bytes of
53 * the provided ByteBuffer. (In the case of a null, a subsequent ByteBuffer with a line end termination should be provided)
54 * @throws NotUtf8Exception
55 * if the input buffer has bytes that do not conform to UTF8 validation (validation performed by {@link Utf8StringBuilder}
57 public String parse(ByteBuffer buf)
60 while (buf.remaining() > 0)
66 return utf.toString();
69 // have not reached end of line (yet)
73 private boolean parseByte(byte b)
78 utf = new Utf8StringBuilder();
82 // not waiting on more UTF sequence parts.
83 if (utf.isUtf8SequenceComplete() && ((b == '\r') || (b == '\n')))
93 // we've reached the end