下面列出了 io.netty.handler.codec.base64.Base64 # decode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
ByteBuf readPrivateKey(InputStream in) throws KeyException {
String content;
try {
content = readContent(in);
} catch (IOException e) {
throw new KeyException("failed to read key input stream", e);
}
Matcher m = KEY_PATTERN.matcher(content);
if (!m.find()) {
throw new KeyException(
"could not find a PKCS #8 private key in input stream"
+ " (see https://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
return der;
}
/**
* Decodes the settings header and returns a {@link Http2Settings} object.
*/
private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader)
throws Http2Exception {
ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8);
try {
// Decode the SETTINGS payload.
ByteBuf payload = Base64.decode(header, URL_SAFE);
// Create an HTTP/2 frame for the settings.
ByteBuf frame = createSettingsFrame(ctx, payload);
// Decode the SETTINGS frame and return the settings object.
return decodeSettings(ctx, frame);
} finally {
header.release();
}
}
static ByteBuf readPrivateKey(InputStream in) throws KeyException {
String content;
try {
content = readContent(in);
} catch (IOException e) {
throw new KeyException("failed to read key input stream", e);
}
Matcher m = KEY_PATTERN.matcher(content);
if (!m.find()) {
throw new KeyException("could not find a PKCS #8 private key in input stream" +
" (see http://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
return der;
}
static ByteBuf readPrivateKey(File file) throws KeyException {
String content;
try {
content = readContent(file);
} catch (IOException e) {
throw new KeyException("failed to read a file: " + file, e);
}
Matcher m = KEY_PATTERN.matcher(content);
if (!m.find()) {
throw new KeyException("found no private key: " + file);
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
return der;
}
private void prepareImage() {
if (base64String == null) {
delete(resourceLocation);
this.dynamicImage = null;
return;
}
ByteBuf localByteBuf1 = Unpooled.copiedBuffer(base64String, Charsets.UTF_8);
ByteBuf localByteBuf2 = null;
BufferedImage localBufferedImage;
try {
localByteBuf2 = Base64.decode(localByteBuf1);
localBufferedImage = read(new ByteBufInputStream(localByteBuf2));
Validate.validState(localBufferedImage.getWidth() == width, "Must be " + width + " pixels wide");
Validate.validState(localBufferedImage.getHeight() == height, "Must be " + height + " pixels high");
} catch (Exception e) {
The5zigMod.logger.error("Could not prepare base64 renderer image", e);
delete(resourceLocation);
dynamicImage = null;
return;
} finally {
localByteBuf1.release();
if (localByteBuf2 != null) {
localByteBuf2.release();
}
}
if (this.dynamicImage == null) {
this.dynamicImage = The5zigMod.getVars().loadDynamicImage(resourceLocation.callGetResourcePath(), localBufferedImage);
}
}
private void prepareImage() {
if (base64String == null) {
delete(resourceLocation);
this.dynamicImage = null;
return;
}
ByteBuf localByteBuf1 = Unpooled.copiedBuffer(base64String, Charsets.UTF_8);
ByteBuf localByteBuf2 = null;
BufferedImage localBufferedImage;
try {
localByteBuf2 = Base64.decode(localByteBuf1);
localBufferedImage = read(new ByteBufInputStream(localByteBuf2));
Validate.validState(localBufferedImage.getWidth() == width, "Must be " + width + " pixels wide");
Validate.validState(localBufferedImage.getHeight() == height, "Must be " + height + " pixels high");
} catch (Exception e) {
The5zigMod.logger.error("Could not prepare base64 renderer image", e);
delete(resourceLocation);
dynamicImage = null;
return;
} finally {
localByteBuf1.release();
if (localByteBuf2 != null) {
localByteBuf2.release();
}
}
if (this.dynamicImage == null) {
this.dynamicImage = The5zigMod.getVars().loadDynamicImage(resourceLocation.getResourcePath(), localBufferedImage);
}
}
public ByteBuf[] readCertificates(InputStream in) throws CertificateException {
String content;
try {
content = readContent(in);
} catch (IOException e) {
throw new CertificateException("failed to read certificate input stream", e);
}
List<ByteBuf> certs = new ArrayList<ByteBuf>();
Matcher m = CERT_PATTERN.matcher(content);
int start = 0;
for (; ; ) {
if (!m.find(start)) {
break;
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
certs.add(der);
start = m.end();
}
if (certs.isEmpty()) {
throw new CertificateException("found no certificates in input stream");
}
return certs.toArray(new ByteBuf[0]);
}
private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
assertThat(req.method(), is(HttpMethod.CONNECT));
if (testMode != TestMode.INTERMEDIARY) {
ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
}
ctx.pipeline().remove(HttpObjectAggregator.class);
ctx.pipeline().get(HttpServerCodec.class).removeInboundHandler();
boolean authzSuccess = false;
if (username != null) {
CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
if (authz != null) {
String[] authzParts = authz.toString().split(" ", 2);
ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
ByteBuf authzBuf = Base64.decode(authzBuf64);
String expectedAuthz = username + ':' + password;
authzSuccess = "Basic".equals(authzParts[0]) &&
expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));
authzBuf64.release();
authzBuf.release();
}
} else {
authzSuccess = true;
}
return authzSuccess;
}
static ByteBuf[] readCertificates(InputStream in) throws CertificateException {
String content;
try {
content = readContent(in);
} catch (IOException e) {
throw new CertificateException("failed to read certificate input stream", e);
}
List<ByteBuf> certs = new ArrayList<ByteBuf>();
Matcher m = CERT_PATTERN.matcher(content);
int start = 0;
for (;;) {
if (!m.find(start)) {
break;
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
certs.add(der);
start = m.end();
}
if (certs.isEmpty()) {
throw new CertificateException("found no certificates in input stream");
}
return certs.toArray(new ByteBuf[certs.size()]);
}
static ByteBuf[] readCertificates(File file) throws CertificateException {
String content;
try {
content = readContent(file);
} catch (IOException e) {
throw new CertificateException("failed to read a file: " + file, e);
}
List<ByteBuf> certs = new ArrayList<ByteBuf>();
Matcher m = CERT_PATTERN.matcher(content);
int start = 0;
for (;;) {
if (!m.find(start)) {
break;
}
ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
ByteBuf der = Base64.decode(base64);
base64.release();
certs.add(der);
start = m.end();
}
if (certs.isEmpty()) {
throw new CertificateException("found no certificates: " + file);
}
return certs.toArray(new ByteBuf[certs.size()]);
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}
public static FullHttpResponse theBase64Image1pxGif() {
ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
return response;
}