All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.codec.http2.encode.Http1FieldPreEncoder Maven / Gradle / Ivy

There is a newer version: 4.0.3.2
Show newest version
package com.firefly.codec.http2.encode;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.Arrays;

import com.firefly.codec.http2.model.HttpHeader;
import com.firefly.codec.http2.model.HttpVersion;

public class Http1FieldPreEncoder implements HttpFieldPreEncoder {

	@Override
	public HttpVersion getHttpVersion() {
		return HttpVersion.HTTP_1_0;
	}

	@Override
	public byte[] getEncodedField(HttpHeader header, String headerString, String value) {
		if (header != null) {
			int cbl = header.getBytesColonSpace().length;
			byte[] bytes = Arrays.copyOf(header.getBytesColonSpace(), cbl + value.length() + 2);
			System.arraycopy(value.getBytes(UTF_8), 0, bytes, cbl, value.length());
			bytes[bytes.length - 2] = (byte) '\r';
			bytes[bytes.length - 1] = (byte) '\n';
			return bytes;
		}

		byte[] n = headerString.getBytes(UTF_8);
		byte[] v = value.getBytes(UTF_8);
		byte[] bytes = Arrays.copyOf(n, n.length + 2 + v.length + 2);
		bytes[n.length] = (byte) ':';
		bytes[n.length] = (byte) ' ';
		bytes[bytes.length - 2] = (byte) '\r';
		bytes[bytes.length - 1] = (byte) '\n';
		return bytes;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy