Datasets:
ArXiv:
License:
Download data/val/jesusbmx/HttpCli/src/httpcli/RequestBody.java from RepoFusion/Stack-Repo: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/datasets/RepoFusion/Stack-Repo/resolve/main/data/val/jesusbmx/HttpCli/src/httpcli/RequestBody.java
- Command line
-
hf download hf://datasets/RepoFusion/Stack-Repo/data/val/jesusbmx/HttpCli/src/httpcli/RequestBody.java
-
curl -L -o RequestBody.java https://huggingface.co/datasets/RepoFusion/Stack-Repo/resolve/main/data/val/jesusbmx/HttpCli/src/httpcli/RequestBody.java
2.56 kB
| package httpcli; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.nio.charset.Charset; | |
| import httpcli.io.IOUtils; | |
| public abstract class RequestBody { | |
| public abstract String contentType(Charset charset) throws IOException; | |
| public abstract long contentLength(Charset charset) throws IOException; | |
| public abstract void writeTo(OutputStream out, Charset charset) throws IOException; | |
| public static RequestBody create(final String contentType, final String content) { | |
| return new RequestBody() { | |
| public String contentType(Charset charset) throws IOException { | |
| return generateContentType(contentType, charset); | |
| } | |
| public long contentLength(Charset charset) throws IOException { | |
| return content.getBytes(charset).length; | |
| } | |
| public void writeTo(OutputStream out, Charset charset) throws IOException { | |
| out.write(content.getBytes(charset)); | |
| } | |
| }; | |
| } | |
| public static RequestBody create(final String contentType, final byte[] content, | |
| final boolean encoding) { | |
| return new RequestBody() { | |
| public String contentType(Charset charset) throws IOException { | |
| return (encoding) ? generateContentType(contentType, charset) : contentType; | |
| } | |
| public long contentLength(Charset charset) throws IOException { | |
| return content.length; | |
| } | |
| public void writeTo(OutputStream out, Charset charset) throws IOException { | |
| out.write(content); | |
| } | |
| }; | |
| } | |
| public static RequestBody create(final String contentType, final File content, | |
| final boolean encoding) { | |
| return new RequestBody() { | |
| public String contentType(Charset charset) throws IOException { | |
| return (encoding) ? generateContentType(contentType, charset) : contentType; | |
| } | |
| public long contentLength(Charset charset) throws IOException { | |
| return content.length(); | |
| } | |
| public void writeTo(OutputStream out, Charset charset) throws IOException { | |
| IOUtils.copy(content, out); | |
| } | |
| }; | |
| } | |
| public static String generateContentType(String contentType, Charset charset) { | |
| if (!contentType.contains("; charset=")) { | |
| String charsetName = charset.name(); | |
| return new StringBuilder(contentType.length() + 10 + charsetName.length()) | |
| .append(contentType).append("; charset=").append(charsetName) | |
| .toString(); | |
| } | |
| return contentType; | |
| } | |
| } | |