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.Charset;
24
25 import org.eclipse.jetty.util.BufferUtil;
26 import org.eclipse.jetty.util.StringUtil;
27 import org.eclipse.jetty.util.Utf8Appendable;
28
29
30
31
32 public class Utf8CharBuffer extends Utf8Appendable
33 {
34 private static final Charset UTF8 = StringUtil.__UTF8_CHARSET;
35
36
37
38
39
40
41
42
43 public static Utf8CharBuffer wrap(ByteBuffer buffer)
44 {
45 return new Utf8CharBuffer(buffer.asCharBuffer());
46 }
47
48 private final CharBuffer buffer;
49
50 private Utf8CharBuffer(CharBuffer buffer)
51 {
52 super(buffer);
53 this.buffer = buffer;
54 }
55
56 public void append(char[] cbuf, int offset, int size)
57 {
58 append(BufferUtil.toDirectBuffer(new String(cbuf,offset,size),UTF8));
59 }
60
61 public void append(int c)
62 {
63 buffer.append((char)c);
64 }
65
66 public void clear()
67 {
68 buffer.position(0);
69 buffer.limit(buffer.capacity());
70 }
71
72 public ByteBuffer getByteBuffer()
73 {
74
75 int limit = buffer.limit();
76 int position = buffer.position();
77
78
79 buffer.limit(buffer.position());
80 buffer.position(0);
81
82
83 ByteBuffer bb = UTF8.encode(buffer);
84
85
86 buffer.limit(limit);
87 buffer.position(position);
88
89 return bb;
90 }
91
92 @Override
93 public int length()
94 {
95 return buffer.capacity();
96 }
97
98 public int remaining()
99 {
100 return buffer.remaining();
101 }
102
103 @Override
104 public String toString()
105 {
106 StringBuilder str = new StringBuilder();
107 str.append("Utf8CharBuffer@").append(hashCode());
108 str.append("[p=").append(buffer.position());
109 str.append(",l=").append(buffer.limit());
110 str.append(",c=").append(buffer.capacity());
111 str.append(",r=").append(buffer.remaining());
112 str.append("]");
113 return str.toString();
114 }
115 }