3535 *
3636 * Not thread-safe. Assumed to be used single-threaded
3737 */
38- public class FrameHeaderFlyweight
39- {
38+ public class FrameHeaderFlyweight {
4039
4140 private FrameHeaderFlyweight () {}
4241
@@ -62,14 +61,10 @@ private FrameHeaderFlyweight() {}
6261
6362 public static final int FLAGS_REQUEST_CHANNEL_F = 0b0010_0000_0000_0000;
6463
65- static
66- {
67- if (INCLUDE_FRAME_LENGTH )
68- {
64+ static {
65+ if (INCLUDE_FRAME_LENGTH ) {
6966 FRAME_LENGTH_FIELD_OFFSET = 0 ;
70- }
71- else
72- {
67+ } else {
7368 FRAME_LENGTH_FIELD_OFFSET = -BitUtil .SIZE_OF_INT ;
7469 }
7570
@@ -81,8 +76,7 @@ private FrameHeaderFlyweight() {}
8176 FRAME_HEADER_LENGTH = PAYLOAD_OFFSET ;
8277 }
8378
84- public static int computeFrameHeaderLength (final FrameType frameType , int metadataLength , final int dataLength )
85- {
79+ public static int computeFrameHeaderLength (final FrameType frameType , int metadataLength , final int dataLength ) {
8680 return PAYLOAD_OFFSET + computeMetadataLength (metadataLength ) + dataLength ;
8781 }
8882
@@ -92,10 +86,9 @@ public static int encodeFrameHeader(
9286 final int frameLength ,
9387 final int flags ,
9488 final FrameType frameType ,
95- final int streamId )
96- {
97- if (INCLUDE_FRAME_LENGTH )
98- {
89+ final int streamId
90+ ) {
91+ if (INCLUDE_FRAME_LENGTH ) {
9992 mutableDirectBuffer .putInt (offset + FRAME_LENGTH_FIELD_OFFSET , frameLength , ByteOrder .BIG_ENDIAN );
10093 }
10194
@@ -110,13 +103,12 @@ public static int encodeMetadata(
110103 final MutableDirectBuffer mutableDirectBuffer ,
111104 final int frameHeaderStartOffset ,
112105 final int metadataOffset ,
113- final ByteBuffer metadata )
114- {
106+ final ByteBuffer metadata
107+ ) {
115108 int length = 0 ;
116109 final int metadataLength = metadata .remaining ();
117110
118- if (0 < metadataLength )
119- {
111+ if (0 < metadataLength ) {
120112 int flags = mutableDirectBuffer .getShort (frameHeaderStartOffset + FLAGS_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
121113 flags |= FLAGS_M ;
122114 mutableDirectBuffer .putShort (frameHeaderStartOffset + FLAGS_FIELD_OFFSET , (short )flags , ByteOrder .BIG_ENDIAN );
@@ -132,13 +124,12 @@ public static int encodeMetadata(
132124 public static int encodeData (
133125 final MutableDirectBuffer mutableDirectBuffer ,
134126 final int dataOffset ,
135- final ByteBuffer data )
136- {
127+ final ByteBuffer data
128+ ) {
137129 int length = 0 ;
138130 final int dataLength = data .remaining ();
139131
140- if (0 < dataLength )
141- {
132+ if (0 < dataLength ) {
142133 mutableDirectBuffer .putBytes (dataOffset , data , dataLength );
143134 length += dataLength ;
144135 }
@@ -154,14 +145,13 @@ public static int encode(
154145 int flags ,
155146 final FrameType frameType ,
156147 final ByteBuffer metadata ,
157- final ByteBuffer data )
158- {
148+ final ByteBuffer data
149+ ) {
159150 final int frameLength = computeFrameHeaderLength (frameType , metadata .remaining (), data .remaining ());
160151
161152 final FrameType outFrameType ;
162153
163- switch (frameType )
164- {
154+ switch (frameType ) {
165155 case COMPLETE :
166156 outFrameType = FrameType .RESPONSE ;
167157 flags |= FLAGS_RESPONSE_C ;
@@ -182,114 +172,95 @@ public static int encode(
182172 return length ;
183173 }
184174
185- public static int flags (final DirectBuffer directBuffer , final int offset )
186- {
175+ public static int flags (final DirectBuffer directBuffer , final int offset ) {
187176 return directBuffer .getShort (offset + FLAGS_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
188177 }
189178
190- public static FrameType frameType (final DirectBuffer directBuffer , final int offset )
191- {
179+ public static FrameType frameType (final DirectBuffer directBuffer , final int offset ) {
192180 FrameType result = FrameType .from (directBuffer .getShort (offset + TYPE_FIELD_OFFSET , ByteOrder .BIG_ENDIAN ));
193181
194- if (FrameType .RESPONSE == result )
195- {
182+ if (FrameType .RESPONSE == result ) {
196183 final int flags = flags (directBuffer , offset );
197184 final int dataLength = dataLength (directBuffer , offset , 0 );
198185
199- if ( FLAGS_RESPONSE_C == (flags & FLAGS_RESPONSE_C ) && 0 < dataLength )
200- {
186+ boolean complete = FLAGS_RESPONSE_C == (flags & FLAGS_RESPONSE_C );
187+ if ( complete && 0 < dataLength ) {
201188 result = FrameType .NEXT_COMPLETE ;
202- }
203- else if (FLAGS_RESPONSE_C == (flags & FLAGS_RESPONSE_C ))
204- {
189+ } else if (complete ) {
205190 result = FrameType .COMPLETE ;
206- }
207- else
208- {
191+ } else {
209192 result = FrameType .NEXT ;
210193 }
211194 }
212195
213196 return result ;
214197 }
215198
216- public static int streamId (final DirectBuffer directBuffer , final int offset )
217- {
199+ public static int streamId (final DirectBuffer directBuffer , final int offset ) {
218200 return directBuffer .getInt (offset + STREAM_ID_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
219201 }
220202
221- public static ByteBuffer sliceFrameData (final DirectBuffer directBuffer , final int offset , final int length )
222- {
203+ public static ByteBuffer sliceFrameData (final DirectBuffer directBuffer , final int offset , final int length ) {
223204 final int dataLength = dataLength (directBuffer , offset , length );
224205 final int dataOffset = dataOffset (directBuffer , offset );
225206 ByteBuffer result = NULL_BYTEBUFFER ;
226207
227- if (0 < dataLength )
228- {
208+ if (0 < dataLength ) {
229209 result = preservingSlice (directBuffer .byteBuffer (), dataOffset , dataOffset + dataLength );
230210 }
231211
232212 return result ;
233213 }
234214
235- public static ByteBuffer sliceFrameMetadata (final DirectBuffer directBuffer , final int offset , final int length )
236- {
215+ public static ByteBuffer sliceFrameMetadata (final DirectBuffer directBuffer , final int offset , final int length ) {
237216 final int metadataLength = Math .max (0 , metadataFieldLength (directBuffer , offset ) - BitUtil .SIZE_OF_INT );
238217 final int metadataOffset = metadataOffset (directBuffer , offset ) + BitUtil .SIZE_OF_INT ;
239218 ByteBuffer result = NULL_BYTEBUFFER ;
240219
241- if (0 < metadataLength )
242- {
220+ if (0 < metadataLength ) {
243221 result = preservingSlice (directBuffer .byteBuffer (), metadataOffset , metadataOffset + metadataLength );
244222 }
245223
246224 return result ;
247225 }
248226
249- private static int frameLength (final DirectBuffer directBuffer , final int offset , final int externalFrameLength )
250- {
227+ private static int frameLength (final DirectBuffer directBuffer , final int offset , final int externalFrameLength ) {
251228 int frameLength = externalFrameLength ;
252229
253- if (INCLUDE_FRAME_LENGTH )
254- {
230+ if (INCLUDE_FRAME_LENGTH ) {
255231 frameLength = directBuffer .getInt (offset + FRAME_LENGTH_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
256232 }
257233
258234 return frameLength ;
259235 }
260236
261- private static int computeMetadataLength (final int metadataPayloadLength )
262- {
237+ private static int computeMetadataLength (final int metadataPayloadLength ) {
263238 return metadataPayloadLength + ((0 == metadataPayloadLength ) ? 0 : BitUtil .SIZE_OF_INT );
264239 }
265240
266- private static int metadataFieldLength (final DirectBuffer directBuffer , final int offset )
267- {
241+ private static int metadataFieldLength (final DirectBuffer directBuffer , final int offset ) {
268242 int metadataLength = 0 ;
269243
270- if ( FLAGS_M == ( FLAGS_M & directBuffer .getShort (offset + FLAGS_FIELD_OFFSET , ByteOrder .BIG_ENDIAN )))
271- {
244+ short flags = directBuffer .getShort (offset + FLAGS_FIELD_OFFSET , ByteOrder .BIG_ENDIAN );
245+ if ( FLAGS_M == ( FLAGS_M & flags )) {
272246 metadataLength = directBuffer .getInt (metadataOffset (directBuffer , offset ), ByteOrder .BIG_ENDIAN ) & 0xFFFFFF ;
273247 }
274248
275249 return metadataLength ;
276250 }
277251
278- private static int dataLength (final DirectBuffer directBuffer , final int offset , final int externalLength )
279- {
252+ private static int dataLength (final DirectBuffer directBuffer , final int offset , final int externalLength ) {
280253 final int frameLength = frameLength (directBuffer , offset , externalLength );
281254 final int metadataLength = metadataFieldLength (directBuffer , offset );
282255
283256 return offset + frameLength - metadataLength - payloadOffset (directBuffer , offset );
284257 }
285258
286- private static int payloadOffset (final DirectBuffer directBuffer , final int offset )
287- {
259+ private static int payloadOffset (final DirectBuffer directBuffer , final int offset ) {
288260 final FrameType frameType = FrameType .from (directBuffer .getShort (offset + TYPE_FIELD_OFFSET , ByteOrder .BIG_ENDIAN ));
289261 int result = offset + PAYLOAD_OFFSET ;
290262
291- switch (frameType )
292- {
263+ switch (frameType ) {
293264 case SETUP :
294265 result = SetupFrameFlyweight .payloadOffset (directBuffer , offset );
295266 break ;
@@ -317,13 +288,11 @@ private static int payloadOffset(final DirectBuffer directBuffer, final int offs
317288 return result ;
318289 }
319290
320- private static int metadataOffset (final DirectBuffer directBuffer , final int offset )
321- {
291+ private static int metadataOffset (final DirectBuffer directBuffer , final int offset ) {
322292 return payloadOffset (directBuffer , offset );
323293 }
324294
325- private static int dataOffset (final DirectBuffer directBuffer , final int offset )
326- {
295+ private static int dataOffset (final DirectBuffer directBuffer , final int offset ) {
327296 return payloadOffset (directBuffer , offset ) + metadataFieldLength (directBuffer , offset );
328297 }
329298}
0 commit comments