Skip to content

Commit 9f01cf4

Browse files
committed
Issue menacher#27 Fix for client side, few variable renaming.
1 parent 54c95fe commit 9f01cf4

2 files changed

Lines changed: 45 additions & 89 deletions

File tree

jetclient/src/main/java/org/menacheri/jetclient/util/NettyUtils.java

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.menacheri.jetclient.util;
22

3+
import static org.jboss.netty.buffer.ChannelBuffers.copiedBuffer;
4+
35
import java.net.InetSocketAddress;
6+
import java.nio.ByteOrder;
47
import java.nio.charset.Charset;
58
import java.util.NoSuchElementException;
69

@@ -10,8 +13,6 @@
1013
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
1114
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
1215
import 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;
1516
import org.jboss.netty.util.CharsetUtil;
1617
import org.menacheri.convert.Transform;
1718

@@ -23,8 +24,6 @@
2324
*/
2425
public 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()

jetserver/src/main/java/org/menacheri/jetserver/util/NettyUtils.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ public static String[] readStrings(ChannelBuffer buffer, int numOfStrings)
110110
* strlength-strbytes combination.
111111
* @param numOfStrings
112112
* The number of strings to be read. Should not be negative or 0
113-
* @param charSet
113+
* @param charset
114114
* The Charset say 'UTF-8' in which the decoding needs to be
115115
* done.
116116
*
117117
* @return the strings read from the buffer as an array.
118118
*/
119119
public static String[] readStrings(ChannelBuffer buffer, int numOfStrings,
120-
Charset charSet)
120+
Charset charset)
121121
{
122122
String[] strings = new String[numOfStrings];
123123
for (int i = 0; i < numOfStrings; i++)
124124
{
125-
String theStr = readString(buffer,charSet);
125+
String theStr = readString(buffer,charset);
126126
if(null == theStr) break;
127127
strings[i] = theStr;
128128
}
@@ -153,19 +153,19 @@ public static String readString(ChannelBuffer buffer)
153153
* @param buffer
154154
* The Netty buffer containing at least one unsigned short
155155
* followed by a string of similar length.
156-
* @param charSet
156+
* @param charset
157157
* The Charset say 'UTF-8' in which the decoding needs to be
158158
* done.
159159
* @return Returns the String or throws {@link IndexOutOfBoundsException} if
160160
* the length is greater than expected.
161161
*/
162-
public static String readString(ChannelBuffer buffer, Charset charSet)
162+
public static String readString(ChannelBuffer buffer, Charset charset)
163163
{
164164
String readString = null;
165165
if (null != buffer && buffer.readableBytes() > 2)
166166
{
167167
int length = buffer.readUnsignedShort();
168-
readString = readString(buffer, length, charSet);
168+
readString = readString(buffer, length, charset);
169169
}
170170
return readString;
171171
}
@@ -195,29 +195,29 @@ public static String readString(ChannelBuffer buffer, int length)
195195
/**
196196
* Read a string from a channel buffer with the specified length. It resets
197197
* the reader index of the buffer to the end of the string. Defaults to
198-
* UTF-8 encoding in case charSet passed in is null
198+
* UTF-8 encoding in case charset passed in is null
199199
*
200200
* @param buffer
201201
* The Netty buffer containing the String.
202202
* @param length
203203
* The number of bytes in the String.
204-
* @param charSet
204+
* @param charset
205205
* The Charset say 'UTF-8' in which the decoding needs to be
206206
* done.
207207
* @return Returns the read string.
208208
*/
209209
public static String readString(ChannelBuffer buffer, int length,
210-
Charset charSet)
210+
Charset charset)
211211
{
212212
String str = null;
213-
if (null == charSet)
213+
if (null == charset)
214214
{
215-
charSet = CharsetUtil.UTF_8;
215+
charset = CharsetUtil.UTF_8;
216216
}
217217
try
218218
{
219219
ChannelBuffer stringBuffer = buffer.readSlice(length);
220-
str = stringBuffer.toString(charSet);
220+
str = stringBuffer.toString(charset);
221221
}
222222
catch (Exception e)
223223
{
@@ -251,22 +251,22 @@ public static ChannelBuffer writeStrings(String... msgs)
251251
* of Hello><Hello as appropriate charset binary><Length of world><World as
252252
* UTF-8 binary>
253253
*
254-
* @param charSet
254+
* @param charset
255255
* The Charset say 'UTF-8' in which the encoding needs to be
256256
* done.
257257
* @param msgs
258258
* The messages to be written.
259259
* @return {@link ChannelBuffer} with format
260260
* length-stringbinary-length-stringbinary
261261
*/
262-
public static ChannelBuffer writeStrings(Charset charSet, String... msgs)
262+
public static ChannelBuffer writeStrings(Charset charset, String... msgs)
263263
{
264264
ChannelBuffer buffer = null;
265265
for (String msg : msgs)
266266
{
267267
if (null == buffer)
268268
{
269-
buffer = writeString(msg,charSet);
269+
buffer = writeString(msg,charset);
270270
}
271271
else
272272
{
@@ -297,7 +297,7 @@ public static ChannelBuffer writeString(String msg)
297297
/**
298298
* Creates a channel buffer of which the first 2 bytes contain the length of
299299
* the string in bytes and the remaining is the actual string in binary with
300-
* specified format. Defaults to UTF-8 encoding in case charSet passed in is
300+
* specified format. Defaults to UTF-8 encoding in case charset passed in is
301301
* null
302302
*
303303
* @param msg
@@ -317,8 +317,7 @@ public static ChannelBuffer writeString(String msg, Charset charset)
317317
{
318318
charset = CharsetUtil.UTF_8;
319319
}
320-
stringBuffer = copiedBuffer(ByteOrder.BIG_ENDIAN, (String) msg,
321-
charset);
320+
stringBuffer = copiedBuffer(ByteOrder.BIG_ENDIAN, msg, charset);
322321
int length = stringBuffer.readableBytes();
323322
ChannelBuffer lengthBuffer = ChannelBuffers.buffer(2);
324323
lengthBuffer.writeShort(length);

0 commit comments

Comments
 (0)