@@ -50,8 +50,8 @@ public class FrameFlyweight
50
50
private static final byte CURRENT_VERSION = 0 ;
51
51
52
52
private static final int FLAGS_I = 0b1000_000;
53
- private static final int FLAGS_B = 0b0100_000;
54
- private static final int FLAGS_E = 0b0010_000;
53
+ private static final int FLAGS_M = 0b0100_000;
54
+ private static final int FLAGS_F = 0b0010_000;
55
55
private static final int FLAGS_C = 0b0001_000;
56
56
57
57
static
@@ -170,15 +170,8 @@ public String framePayload(final ByteBuffer byteBuffer, final int length)
170
170
{
171
171
frameBuffer .wrap (byteBuffer );
172
172
173
- int frameLength = length ;
174
-
175
- if (INCLUDE_FRAME_LENGTH )
176
- {
177
- frameLength = frameBuffer .getInt (FRAME_LENGTH_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
178
- }
179
-
180
- final int dataLength = frameLength - DATA_OFFSET ;
181
- int dataOffset = DATA_OFFSET ;
173
+ final int dataLength = dataLength (byteBuffer , length );
174
+ final int dataOffset = dataOffset (byteBuffer );
182
175
183
176
// byteArray used as a re-usable temporary for generating payload String
184
177
ensureByteArrayCapacity (dataLength );
@@ -187,6 +180,31 @@ public String framePayload(final ByteBuffer byteBuffer, final int length)
187
180
return new String (byteArray , 0 , dataLength );
188
181
}
189
182
183
+ public ByteBuffer sliceFramePayload (final ByteBuffer byteBuffer , final int length )
184
+ {
185
+ frameBuffer .wrap (byteBuffer );
186
+
187
+ final int dataLength = dataLength (byteBuffer , length );
188
+ final int dataOffset = dataOffset (byteBuffer );
189
+
190
+ return slice (byteBuffer , dataOffset , dataOffset + dataLength );
191
+ }
192
+
193
+ // really should be an interface to ByteBuffer... sigh
194
+ // TODO: move to some utility package
195
+ public static ByteBuffer slice (final ByteBuffer byteBuffer , final int position , final int limit )
196
+ {
197
+ final int savedPosition = byteBuffer .position ();
198
+ final int savedLimit = byteBuffer .limit ();
199
+
200
+ byteBuffer .limit (limit ).position (position );
201
+
202
+ final ByteBuffer result = byteBuffer .slice ();
203
+
204
+ byteBuffer .limit (savedLimit ).position (savedPosition );
205
+ return byteBuffer .slice ();
206
+ }
207
+
190
208
private void ensureByteArrayCapacity (final int length )
191
209
{
192
210
if (byteArray .length < length )
@@ -195,4 +213,22 @@ private void ensureByteArrayCapacity(final int length)
195
213
}
196
214
}
197
215
216
+ private int dataLength (final ByteBuffer byteBuffer , final int length )
217
+ {
218
+ frameBuffer .wrap (byteBuffer );
219
+
220
+ int frameLength = length ;
221
+
222
+ if (INCLUDE_FRAME_LENGTH )
223
+ {
224
+ frameLength = frameBuffer .getInt (FRAME_LENGTH_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
225
+ }
226
+
227
+ return frameLength - DATA_OFFSET ;
228
+ }
229
+
230
+ private int dataOffset (final ByteBuffer byteBuffer )
231
+ {
232
+ return DATA_OFFSET ;
233
+ }
198
234
}
0 commit comments