summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/main/java/org/simpleframework/http/socket/DataConverter.java
blob: 5713fd63911d7ed5ed78ce4779ea36d2904e2a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * DataConverter.java February 2014
 *
 * Copyright (C) 2014, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.http.socket;

/**
 * The <code>DataConverter</code> object is used to convert binary data
 * to text data and vice versa. According to RFC 6455 a particular text 
 * frame might include a partial UTF-8 sequence; however, the whole 
 * message MUST contain valid UTF-8.
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.http.socket.DataFrame
 */
public class DataConverter {
   
   /**
    * This is the character encoding used to convert the text data.
    */
   private final String charset;

   /**
    * Constructor for the <code>DataConverter</code> object. By default
    * this uses UTF-8 character encoding to convert text data as this
    * is what is required for RFC 6455 section 5.6.
    */
   public DataConverter() {
      this("UTF-8");
   }
   
   /**
    * Constructor for the <code>DataConverter</code> object. This can be
    * used to specific a character encoding other than UTF-8. However it
    * is not recommended as RFC 6455 section 5.6 suggests the frame must
    * contain valid UTF-8 data.
    * 
    * @param charset the character encoding to be used
    */
   public DataConverter(String charset) {
      this.charset = charset;
   }

   /**
    * This method is used to convert text using the character encoding
    * specified when constructing the converter. Typically this will use
    * UTF-8 as required by RFC 6455.
    * 
    * @param text this is the string to convert to a byte array
    * 
    * @return a byte array decoded using the specified encoding
    */
   public byte[] convert(String text) {
      try {
         return text.getBytes(charset);
      } catch(Exception e) {
         throw new IllegalStateException("Could not encode text as " + charset, e);
      }  
   }
   
   /**
    * This method is used to convert data using the character encoding
    * specified when constructing the converter. Typically this will use
    * UTF-8 as required by RFC 6455.
    * 
    * @param text this is the byte array to convert to a string
    * 
    * @return a string encoded using the specified encoding
    */
   public String convert(byte[] binary) {
      try {
         return new String(binary, charset);
      } catch(Exception e) {
         throw new IllegalStateException("Could not decode data as " + charset, e);
      }
   }
   
   /**
    * This method is used to convert data using the character encoding
    * specified when constructing the converter. Typically this will use
    * UTF-8 as required by RFC 6455.
    * 
    * @param text this is the byte array to convert to a string
    * @param offset the is the offset to read the bytes from
    * @param size this is the number of bytes to be used
    * 
    * @return a string encoded using the specified encoding
    */
   public String convert(byte[] binary, int offset, int size) {
      try {
         return new String(binary, offset, size, charset);
      } catch(Exception e) {
         throw new IllegalStateException("Could not decode data as " + charset, e);
      }
   }
}