11package org .menacheri .jetclient .util ;
22
3+ import static org .jboss .netty .buffer .ChannelBuffers .copiedBuffer ;
4+
35import java .net .InetSocketAddress ;
6+ import java .nio .ByteOrder ;
47import java .nio .charset .Charset ;
58import java .util .NoSuchElementException ;
69
1013import org .jboss .netty .handler .codec .serialization .ClassResolvers ;
1114import org .jboss .netty .handler .codec .serialization .ObjectDecoder ;
1215import org .jboss .netty .handler .codec .serialization .ObjectEncoder ;
13- import org .jboss .netty .handler .codec .string .StringDecoder ;
14- import org .jboss .netty .handler .codec .string .StringEncoder ;
1516import org .jboss .netty .util .CharsetUtil ;
1617import org .menacheri .convert .Transform ;
1718
2324 */
2425public class NettyUtils
2526{
26- private static final StringDecoderWrapper STRING_DECODER = new StringDecoderWrapper ();
27- private static final StringEncoderWrapper STRING_ENCODER = new StringEncoderWrapper ();
2827 private static final ObjectDecoderWrapper OBJECT_DECODER = new ObjectDecoderWrapper ();
2928
3029 public static final String NETTY_CHANNEL = "NETTY_CHANNEL" ;
@@ -79,19 +78,19 @@ public static String[] readStrings(ChannelBuffer buffer, int numOfStrings)
7978 * strlength-strbytes combination.
8079 * @param numOfStrings
8180 * The number of strings to be read. Should not be negative or 0
82- * @param charSet
81+ * @param charset
8382 * The Charset say 'UTF-8' in which the decoding needs to be
8483 * done.
8584 *
8685 * @return the strings read from the buffer as an array.
8786 */
8887 public static String [] readStrings (ChannelBuffer buffer , int numOfStrings ,
89- Charset charSet )
88+ Charset charset )
9089 {
9190 String [] strings = new String [numOfStrings ];
9291 for (int i =0 ;i <numOfStrings ;i ++)
9392 {
94- String theStr = readString (buffer ,charSet );
93+ String theStr = readString (buffer ,charset );
9594 if (null == theStr ) break ;
9695 strings [i ] = theStr ;
9796 }
@@ -122,19 +121,19 @@ public static String readString(ChannelBuffer buffer)
122121 * @param buffer
123122 * The Netty buffer containing at least one unsigned short
124123 * followed by a string of similar length.
125- * @param charSet
124+ * @param charset
126125 * The Charset say 'UTF-8' in which the decoding needs to be
127126 * done.
128127 * @return Returns the String or throws {@link IndexOutOfBoundsException} if
129128 * the length is greater than expected.
130129 */
131- public static String readString (ChannelBuffer buffer , Charset charSet )
130+ public static String readString (ChannelBuffer buffer , Charset charset )
132131 {
133132 String readString = null ;
134133 if (null != buffer && buffer .readableBytes () > 2 )
135134 {
136135 int length = buffer .readUnsignedShort ();
137- readString = readString (buffer , length , charSet );
136+ readString = readString (buffer , length , charset );
138137 }
139138 return readString ;
140139 }
@@ -157,32 +156,29 @@ public static String readString(ChannelBuffer buffer, int length)
157156 /**
158157 * Read a string from a channel buffer with the specified length. It resets
159158 * the reader index of the buffer to the end of the string. Defaults to
160- * UTF-8 encoding in case charSet passed in is null
159+ * UTF-8 encoding in case charset passed in is null
161160 *
162161 * @param buffer
163162 * The Netty buffer containing the String.
164163 * @param length
165164 * The number of bytes in the String.
166- * @param charSet
165+ * @param charset
167166 * The Charset say 'UTF-8' in which the decoding needs to be
168167 * done.
169168 * @return Returns the read string.
170169 */
171170 public static String readString (ChannelBuffer buffer , int length ,
172- Charset charSet )
171+ Charset charset )
173172 {
174- ChannelBuffer stringBuffer = buffer .readSlice (length );
175173 String str = null ;
174+ if (null == charset )
175+ {
176+ charset = CharsetUtil .UTF_8 ;
177+ }
176178 try
177179 {
178- if (null == charSet || CharsetUtil .UTF_8 .equals (charSet ))
179- {
180- str = STRING_DECODER .decode (stringBuffer );
181- }
182- else
183- {
184- str = new StringDecoderWrapper (charSet ).decode (stringBuffer );
185- }
180+ ChannelBuffer stringBuffer = buffer .readSlice (length );
181+ str = stringBuffer .toString (charset );
186182 }
187183 catch (Exception e )
188184 {
@@ -214,22 +210,22 @@ public static ChannelBuffer writeStrings(String... msgs)
214210 * of Hello><Hello as appropriate charset binary><Length of world><World as
215211 * UTF-8 binary>
216212 *
217- * @param charSet
213+ * @param charset
218214 * The Charset say 'UTF-8' in which the encoding needs to be
219215 * done.
220216 * @param msgs
221217 * The messages to be written.
222218 * @return {@link ChannelBuffer} with format
223219 * length-stringbinary-length-stringbinary
224220 */
225- public static ChannelBuffer writeStrings (Charset charSet , String ... msgs )
221+ public static ChannelBuffer writeStrings (Charset charset , String ... msgs )
226222 {
227223 ChannelBuffer buffer = null ;
228224 for (String msg : msgs )
229225 {
230226 if (null == buffer )
231227 {
232- buffer = writeString (msg , charSet );
228+ buffer = writeString (msg , charset );
233229 }
234230 else
235231 {
@@ -259,34 +255,31 @@ public static ChannelBuffer writeString(String msg)
259255 /**
260256 * Creates a channel buffer of which the first 2 bytes contain the length of
261257 * the string in bytes and the remaining is the actual string in binary with
262- * specified format. Defaults to UTF-8 encoding in case charSet passed in is
258+ * specified format. Defaults to UTF-8 encoding in case charset passed in is
263259 * null
264260 *
265261 * @param msg
266262 * The string to be written.
267- * @param charSet
263+ * @param charset
268264 * The Charset say 'UTF-8' in which the encoding needs to be
269265 * done.
270266 * @return
271267 */
272- public static ChannelBuffer writeString (String msg , Charset charSet )
268+ public static ChannelBuffer writeString (String msg , Charset charset )
273269 {
274270 ChannelBuffer buffer = null ;
275271 try
276272 {
277273 ChannelBuffer stringBuffer = null ;
278- if (null == charSet || CharsetUtil . UTF_8 . equals ( charSet ) )
274+ if (null == charset )
279275 {
280- stringBuffer = STRING_ENCODER .encode (msg );
281- }
282- else
283- {
284- stringBuffer = new StringEncoderWrapper (charSet ).encode (msg );
276+ charset = CharsetUtil .UTF_8 ;
285277 }
278+ stringBuffer = copiedBuffer (ByteOrder .BIG_ENDIAN , msg , charset );
286279 int length = stringBuffer .readableBytes ();
287280 ChannelBuffer lengthBuffer = ChannelBuffers .buffer (2 );
288281 lengthBuffer .writeShort (length );
289- buffer = ChannelBuffers .wrappedBuffer (lengthBuffer ,stringBuffer );
282+ buffer = ChannelBuffers .wrappedBuffer (lengthBuffer , stringBuffer );
290283 }
291284 catch (Exception e )
292285 {
@@ -459,42 +452,6 @@ public static ChannelBuffer writeSocketAddress(
459452 return socketAddressBuffer ;
460453 }
461454
462- public static class StringDecoderWrapper extends StringDecoder
463- {
464- public StringDecoderWrapper (Charset charSet )
465- {
466- super (charSet );
467- }
468-
469- public StringDecoderWrapper ()
470- {
471- }
472-
473- public String decode (ChannelBuffer buffer ) throws Exception
474- {
475- String message = (String ) super .decode (null , null , buffer );
476- return message ;
477- }
478- }
479-
480- public static class StringEncoderWrapper extends StringEncoder
481- {
482- public StringEncoderWrapper (Charset charSet )
483- {
484- super (charSet );
485- }
486-
487- public StringEncoderWrapper ()
488- {
489- }
490-
491- protected ChannelBuffer encode (Object msg ) throws Exception
492- {
493- ChannelBuffer strBuffer = (ChannelBuffer )super .encode (null , null , msg );
494- return strBuffer ;
495- }
496- }
497-
498455 public static class ObjectDecoderWrapper extends ObjectDecoder
499456 {
500457 public ObjectDecoderWrapper ()
0 commit comments