Added partial string support to Utf8Appendable

This commit is contained in:
Greg Wilkins 2017-10-19 20:43:46 +11:00
parent 7399947d56
commit 729dd41493
2 changed files with 22 additions and 14 deletions

View File

@ -30,6 +30,10 @@ import org.eclipse.jetty.util.Utf8Appendable;
*/
public class Utf8CharBuffer extends Utf8Appendable
{
// TODO this class does not correctly implement the Appendable contract.
// The length is currently the capacity, but it should be the number of characters
// appended! It also violates the jetty standard of leaving buffers in flush mode.
/**
* Convenience method to wrap a ByteBuffer with a {@link Utf8CharBuffer}
*
@ -59,6 +63,13 @@ public class Utf8CharBuffer extends Utf8Appendable
{
buffer.append((char)c);
}
@Override
public void reset()
{
clear();
super.reset();
}
public void clear()
{
@ -96,6 +107,13 @@ public class Utf8CharBuffer extends Utf8Appendable
{
return buffer.remaining();
}
@Override
public String getPartialString()
{
return buffer.toString();
}
@Override
public String toString()
@ -109,4 +127,5 @@ public class Utf8CharBuffer extends Utf8Appendable
str.append("]");
return str.toString();
}
}

View File

@ -30,22 +30,13 @@ import org.eclipse.jetty.util.Utf8StringBuilder;
* A call to {@link #toPartialString(ByteBuffer)} will return the section of the String from the start to the last
* completed UTF8 sequence. Leaving incomplete sequences for a subsequent call to complete.
*/
@Deprecated
public class Utf8PartialBuilder
{
private final StringBuilder str;
private final Utf8Appendable utf8;
private final Utf8StringBuilder utf8 = new Utf8StringBuilder();
public Utf8PartialBuilder()
{
this.str = new StringBuilder();
this.utf8 = new Utf8Appendable(str)
{
@Override
public int length()
{
return str.length();
}
};
}
public String toPartialString(ByteBuffer buf)
@ -56,8 +47,6 @@ public class Utf8PartialBuilder
return "";
}
utf8.append(buf);
String ret = str.toString();
str.setLength(0);
return ret;
return utf8.takePartialString();
}
}