Skip to content

Commit 114c86b

Browse files
committed
added method to slice data payload
1 parent d3de9fd commit 114c86b

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

src/main/java/io/reactivesocket/Frame.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public String getData() {
5353
return frameFlyweight.framePayload(byteBuffer, 0);
5454
}
5555

56+
/**
57+
* Return {@link ByteBuffer} that is a {@link ByteBuffer#slice()} for the frame data
58+
*
59+
* @return ByteBuffer containing the data
60+
*/
61+
public ByteBuffer sliceData()
62+
{
63+
return FRAME_HANDLER.get().sliceFramePayload(byteBuffer, 0);
64+
}
65+
5666
/**
5767
* Return frame stream identifier
5868
*

src/main/java/io/reactivesocket/FrameFlyweight.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public class FrameFlyweight
5050
private static final byte CURRENT_VERSION = 0;
5151

5252
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;
5555
private static final int FLAGS_C = 0b0001_000;
5656

5757
static
@@ -170,15 +170,8 @@ public String framePayload(final ByteBuffer byteBuffer, final int length)
170170
{
171171
frameBuffer.wrap(byteBuffer);
172172

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);
182175

183176
// byteArray used as a re-usable temporary for generating payload String
184177
ensureByteArrayCapacity(dataLength);
@@ -187,6 +180,31 @@ public String framePayload(final ByteBuffer byteBuffer, final int length)
187180
return new String(byteArray, 0, dataLength);
188181
}
189182

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+
190208
private void ensureByteArrayCapacity(final int length)
191209
{
192210
if (byteArray.length < length)
@@ -195,4 +213,22 @@ private void ensureByteArrayCapacity(final int length)
195213
}
196214
}
197215

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+
}
198234
}

0 commit comments

Comments
 (0)