package org.glassfish.grizzly.http2.frames;
import java.util.HashMap;
import java.util.Map;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.ThreadCache;
import org.glassfish.grizzly.memory.CompositeBuffer;
import org.glassfish.grizzly.memory.MemoryManager;
public class extends HeaderBlockHead {
private static final ThreadCache.CachedTypeIndex<HeadersFrame> = ThreadCache.obtainIndex(HeadersFrame.class, 8);
public static final int = 1;
public static final byte = 0x1;
public static final byte = 0x20;
static final Map<Integer, String> = new HashMap<>(8);
static {
FLAG_NAMES_MAP.putAll(HeaderBlockHead.FLAG_NAMES_MAP);
FLAG_NAMES_MAP.put((int) END_STREAM, "END_STREAM");
FLAG_NAMES_MAP.put((int) PRIORITIZED, "PRIORITIZED");
}
private boolean ;
private int ;
private int ;
private int ;
private () {
}
public static HeadersFrame (final int flags, final int streamId, final Buffer buffer) {
final HeadersFrame frame = create();
frame.setFlags(flags);
frame.setStreamId(streamId);
if (frame.isFlagSet(PADDED)) {
frame.padLength = buffer.get() & 0xFF;
}
if (frame.isFlagSet(PRIORITIZED)) {
final int dependency = buffer.getInt();
frame.exclusive = (dependency & 1L << 31) != 0;
frame.streamDependency = dependency & 0x7FFFFFFF;
frame.weight = buffer.get() & 0xff;
}
frame.compressedHeaders = buffer.split(buffer.position());
frame.compressedHeadersLen = frame.compressedHeaders.remaining();
frame.setFrameBuffer(buffer);
return frame;
}
static HeadersFrame () {
HeadersFrame frame = ThreadCache.takeFromCache(CACHE_IDX);
if (frame == null) {
frame = new HeadersFrame();
}
return frame;
}
public static HeadersFrameBuilder () {
return new HeadersFrameBuilder();
}
public HeadersFrame () {
if (isPadded()) {
clearFlag(PADDED);
compressedHeaders.limit(compressedHeaders.limit() - padLength);
padLength = 0;
onPayloadUpdated();
}
return this;
}
public int () {
return streamDependency;
}
public boolean () {
return exclusive;
}
public int () {
return weight;
}
public boolean () {
return isFlagSet(END_STREAM);
}
public boolean () {
return isFlagSet(PRIORITIZED);
}
@Override
public String () {
final StringBuilder sb = new StringBuilder();
sb.append("HeadersFrame {").append(headerToString()).append(", streamDependency=").append(streamDependency).append(", exclusive=").append(exclusive)
.append(", weight=").append(weight).append(", padLength=").append(padLength).append(", compressedHeaders=").append(compressedHeaders)
.append('}');
return sb.toString();
}
@Override
public void () {
if (DONT_RECYCLE) {
return;
}
padLength = 0;
streamDependency = 0;
weight = 0;
super.recycle();
ThreadCache.putToCache(CACHE_IDX, this);
}
@Override
public int () {
return TYPE;
}
@Override
public Buffer (final MemoryManager memoryManager) {
final boolean isPadded = isFlagSet(PADDED);
final boolean isPrioritySet = isFlagSet(PRIORITIZED);
final int extraHeaderLen = (isPadded ? 1 : 0) + (isPrioritySet ? 5 : 0);
final Buffer buffer = memoryManager.allocate(FRAME_HEADER_SIZE + extraHeaderLen);
serializeFrameHeader(buffer);
if (isPadded) {
buffer.put((byte) (padLength & 0xff));
}
if (isPrioritySet) {
buffer.putInt(streamDependency);
buffer.put((byte) (weight & 0xff));
}
buffer.trim();
final CompositeBuffer cb = CompositeBuffer.newBuffer(memoryManager, buffer, compressedHeaders);
cb.allowBufferDispose(true);
cb.allowInternalBuffersDispose(true);
return cb;
}
@Override
protected int () {
final boolean isPadded = isFlagSet(PADDED);
final boolean isPrioritySet = isFlagSet(PRIORITIZED);
return (isPadded ? 1 : 0) + (isPrioritySet ? 5 : 0) + compressedHeadersLen;
}
@Override
protected Map<Integer, String> () {
return FLAG_NAMES_MAP;
}
public static class extends HeaderBlockHeadBuilder<HeadersFrameBuilder> {
private int ;
private int ;
private int ;
protected () {
}
public HeadersFrameBuilder (boolean endStream) {
if (endStream) {
setFlag(HeadersFrame.END_STREAM);
}
return this;
}
@Override
public HeadersFrameBuilder (boolean isPadded) {
if (isPadded) {
setFlag(HeadersFrame.PADDED);
}
return this;
}
public HeadersFrameBuilder (boolean isPrioritized) {
if (isPrioritized) {
setFlag(HeadersFrame.PRIORITIZED);
}
return this;
}
@Override
public HeadersFrameBuilder (int padLength) {
this.padLength = padLength;
return this;
}
public HeadersFrameBuilder (int streamDependency) {
this.streamDependency = streamDependency;
return this;
}
public HeadersFrameBuilder (int weight) {
this.weight = weight;
return this;
}
@Override
public HeadersFrame () {
final HeadersFrame frame = HeadersFrame.create();
setHeaderValuesTo(frame);
frame.compressedHeaders = compressedHeaders;
frame.compressedHeadersLen = compressedHeaders.remaining();
frame.padLength = padLength;
frame.streamDependency = streamDependency;
frame.weight = weight;
return frame;
}
@Override
protected HeadersFrameBuilder () {
return this;
}
}
}