View Javadoc

1   //
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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * A CharBuffer wrapped with the Utf8Appendable logic.
30   */
31  public class Utf8CharBuffer extends Utf8Appendable
32  {
33      /**
34       * Convenience method to wrap a ByteBuffer with a {@link Utf8CharBuffer}
35       * 
36       * @param buffer
37       *            the buffer to wrap
38       * @return the Utf8ByteBuffer for the provided ByteBuffer
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          // remember settings
72          int limit = buffer.limit();
73          int position = buffer.position();
74  
75          // flip to flush
76          buffer.limit(buffer.position());
77          buffer.position(0);
78  
79          // get byte buffer
80          ByteBuffer bb = StandardCharsets.UTF_8.encode(buffer);
81  
82          // restor settings
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 }