View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.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   * A CharBuffer wrapped with the Utf8Appendable logic.
31   */
32  public class Utf8CharBuffer extends Utf8Appendable
33  {
34      private static final Charset UTF8 = StringUtil.__UTF8_CHARSET;
35  
36      /**
37       * Convenience method to wrap a ByteBuffer with a {@link Utf8CharBuffer}
38       * 
39       * @param buffer
40       *            the buffer to wrap
41       * @return the Utf8ByteBuffer for the provided ByteBuffer
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          // remember settings
75          int limit = buffer.limit();
76          int position = buffer.position();
77  
78          // flip to flush
79          buffer.limit(buffer.position());
80          buffer.position(0);
81  
82          // get byte buffer
83          ByteBuffer bb = UTF8.encode(buffer);
84  
85          // restor settings
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 }