Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
HttpComponents Client Source Code Files
HttpComponents Client Source Code Files are provided in the
source package file, httpcomponents-client-5.2-src.zip.
You can download httpcomponents-client-5.2-src.zip as described in the previous tutorial and go to the "httpclient5/src" sub-folder to view Source Code files.
You can also browse HttpComponents Client Source Code below:
✍: FYIcenter.com
⏎ org/apache/hc/client5/http/utils/ByteArrayBuilder.java
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.utils;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
/**
* Builder class for sequences of bytes.
*
* @since 5.0
*/
public final class ByteArrayBuilder {
private CharsetEncoder charsetEncoder;
private ByteBuffer buffer;
public ByteArrayBuilder() {
}
public ByteArrayBuilder(final int initialCapacity) {
this.buffer = ByteBuffer.allocate(initialCapacity);
}
public int capacity() {
return this.buffer != null ? this.buffer.capacity() : 0;
}
static ByteBuffer ensureFreeCapacity(final ByteBuffer buffer, final int capacity) {
if (buffer == null) {
return ByteBuffer.allocate(capacity);
}
if (buffer.remaining() < capacity) {
final ByteBuffer newBuffer = ByteBuffer.allocate(buffer.position() + capacity);
buffer.flip();
newBuffer.put(buffer);
return newBuffer;
}
return buffer;
}
static ByteBuffer encode(
final ByteBuffer buffer, final CharBuffer in, final CharsetEncoder encoder) throws CharacterCodingException {
final int capacity = (int) (in.remaining() * encoder.averageBytesPerChar());
ByteBuffer out = ensureFreeCapacity(buffer, capacity);
while (in.hasRemaining()) {
CoderResult result = encoder.encode(in, out, true);
if (result.isError()) {
result.throwException();
}
if (result.isUnderflow()) {
result = encoder.flush(out);
}
if (result.isUnderflow()) {
break;
}
if (result.isOverflow()) {
out = ensureFreeCapacity(out, capacity);
}
}
return out;
}
public void ensureFreeCapacity(final int freeCapacity) {
this.buffer = ensureFreeCapacity(this.buffer, freeCapacity);
}
private void doAppend(final CharBuffer charBuffer) {
if (this.charsetEncoder == null) {
this.charsetEncoder = StandardCharsets.US_ASCII.newEncoder()
.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
this.charsetEncoder.reset();
try {
this.buffer = encode(this.buffer, charBuffer, this.charsetEncoder);
} catch (final CharacterCodingException ex) {
// Should never happen
throw new IllegalStateException("Unexpected character coding error", ex);
}
}
public ByteArrayBuilder charset(final Charset charset) {
if (charset == null) {
this.charsetEncoder = null;
} else {
this.charsetEncoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
return this;
}
public ByteArrayBuilder append(final byte[] b, final int off, final int len) {
if (b == null) {
return this;
}
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) < 0) || ((off + len) > b.length)) {
throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
}
ensureFreeCapacity(len);
this.buffer.put(b, off, len);
return this;
}
public ByteArrayBuilder append(final byte[] b) {
if (b == null) {
return this;
}
return append(b, 0, b.length);
}
public ByteArrayBuilder append(final CharBuffer charBuffer) {
if (charBuffer == null) {
return this;
}
doAppend(charBuffer);
return this;
}
public ByteArrayBuilder append(final char[] b, final int off, final int len) {
if (b == null) {
return this;
}
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) < 0) || ((off + len) > b.length)) {
throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
}
return append(CharBuffer.wrap(b, off, len));
}
public ByteArrayBuilder append(final char[] b) {
if (b == null) {
return this;
}
return append(b, 0, b.length);
}
public ByteArrayBuilder append(final String s) {
if (s == null) {
return this;
}
return append(CharBuffer.wrap(s));
}
public ByteBuffer toByteBuffer() {
return this.buffer != null ? this.buffer.duplicate() : ByteBuffer.allocate(0);
}
public byte[] toByteArray() {
if (this.buffer == null) {
return new byte[] {};
}
this.buffer.flip();
final byte[] b = new byte[this.buffer.remaining()];
this.buffer.get(b);
this.buffer.clear();
return b;
}
public void reset() {
if (this.charsetEncoder != null) {
this.charsetEncoder.reset();
}
if (this.buffer != null) {
this.buffer.clear();
}
}
@Override
public String toString() {
return this.buffer != null ? this.buffer.toString() : "null";
}
}
⏎ org/apache/hc/client5/http/utils/ByteArrayBuilder.java
Or download all them as a single archive file:
File name: httpclient5-5.2-fyi.zip File size: 625318 bytes Release date: 2022-11-10 Download
⇒ Download and Install HttpComponents Core Binary Package
⇐ Download and Install HttpComponents Client Source Package
2023-03-26, ≈131🔥, 1💬
Popular Posts:
What Is jaxb-api-2.1.6.jar? Java Architecture for XML Binding (JAXB) is a Java API that allows Java ...
Apache ZooKeeper is an open-source server which enables highly reliable distributed coordination. Ap...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
JDK 11 jdk.aot.jmod is the JMOD file for JDK 11 Ahead-of-Time (AOT) Compiler module. JDK 11 AOT Comp...
What JAR files are required to run dom\Counter.java provided in the Apache Xerces package? You can f...