1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.common.message;
20
21 import java.nio.ByteBuffer;
22 import java.nio.CharBuffer;
23 import java.nio.charset.StandardCharsets;
24
25 import org.eclipse.jetty.util.BufferUtil;
26 import org.eclipse.jetty.util.Utf8Appendable;
27
28
29
30
31 public class Utf8CharBuffer extends Utf8Appendable
32 {
33
34
35
36
37
38
39
40 public static Utf8CharBuffer wrap(ByteBuffer buffer)
41 {
42 return new Utf8CharBuffer(buffer.asCharBuffer());
43 }
44
45 private final CharBuffer buffer;
46
47 private Utf8CharBuffer(CharBuffer buffer)
48 {
49 super(buffer);
50 this.buffer = buffer;
51 }
52
53 public void append(char[] cbuf, int offset, int size)
54 {
55 append(BufferUtil.toDirectBuffer(new String(cbuf,offset,size),StandardCharsets.UTF_8));
56 }
57
58 public void append(int c)
59 {
60 buffer.append((char)c);
61 }
62
63 public void clear()
64 {
65 buffer.position(0);
66 buffer.limit(buffer.capacity());
67 }
68
69 public ByteBuffer getByteBuffer()
70 {
71
72 int limit = buffer.limit();
73 int position = buffer.position();
74
75
76 buffer.limit(buffer.position());
77 buffer.position(0);
78
79
80 ByteBuffer bb = StandardCharsets.UTF_8.encode(buffer);
81
82
83 buffer.limit(limit);
84 buffer.position(position);
85
86 return bb;
87 }
88
89 @Override
90 public int length()
91 {
92 return buffer.capacity();
93 }
94
95 public int remaining()
96 {
97 return buffer.remaining();
98 }
99
100 @Override
101 public String toString()
102 {
103 StringBuilder str = new StringBuilder();
104 str.append("Utf8CharBuffer@").append(hashCode());
105 str.append("[p=").append(buffer.position());
106 str.append(",l=").append(buffer.limit());
107 str.append(",c=").append(buffer.capacity());
108 str.append(",r=").append(buffer.remaining());
109 str.append("]");
110 return str.toString();
111 }
112 }