2 // ========================================================================
3 // Copyright (c) 1995-2016 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;
24 * Stateful parser for lines of UTF8 formatted text, looking for <code>"\n"</code> as a line termination character.
26 * For use with new IO framework that is based on ByteBuffer parsing.
28 public class Utf8LineParser
38 private Utf8StringBuilder utf;
40 public Utf8LineParser()
42 this.state = State.START;
46 * Parse a ByteBuffer (could be a partial buffer), and return once a complete line of UTF8 parsed text has been reached.
49 * the buffer to parse (could be an incomplete buffer)
50 * @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
51 * the provided ByteBuffer. (In the case of a null, a subsequent ByteBuffer with a line end termination should be provided)
52 * @throws NotUtf8Exception
53 * if the input buffer has bytes that do not conform to UTF8 validation (validation performed by {@link Utf8StringBuilder}
55 public String parse(ByteBuffer buf)
58 while (buf.remaining() > 0)
64 return utf.toString();
67 // have not reached end of line (yet)
71 private boolean parseByte(byte b)
76 utf = new Utf8StringBuilder();
80 // not waiting on more UTF sequence parts.
81 if (utf.isUtf8SequenceComplete() && ((b == '\r') || (b == '\n')))
91 // we've reached the end