本文整理了Java中okio.Buffer.readString()
方法的一些代码示例,展示了Buffer.readString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.readString()
方法的具体详情如下:
包路径:okio.Buffer
类名称:Buffer
方法名:readString
暂无
代码示例来源:origin: jgilfelt/chuck
private String readFromBuffer(Buffer buffer, Charset charset) {
long bufferSize = buffer.size();
long maxBytes = Math.min(bufferSize, maxContentLength);
String body = "";
try {
body = buffer.readString(maxBytes, charset);
} catch (EOFException e) {
body += context.getString(R.string.chuck_body_unexpected_eof);
}
if (bufferSize > maxContentLength) {
body += context.getString(R.string.chuck_body_content_truncated);
}
return body;
}
代码示例来源:origin: jeasonlzy/okhttp-OkGo
private void bodyToString(Request request) {
try {
Request copy = request.newBuilder().build();
RequestBody body = copy.body();
if (body == null) return;
Buffer buffer = new Buffer();
body.writeTo(buffer);
Charset charset = getCharset(body.contentType());
log("\tbody:" + buffer.readString(charset));
} catch (Exception e) {
OkLogger.printStackTrace(e);
}
}
}
代码示例来源:origin: JessYanCoding/MVPArms
/**
* 解析请求服务器的请求参数
*
* @param request {@link Request}
* @return 解析后的请求信息
* @throws UnsupportedEncodingException
*/
public static String parseParams(Request request) throws UnsupportedEncodingException {
try {
RequestBody body = request.newBuilder().build().body();
if (body == null) return "";
Buffer requestbuffer = new Buffer();
body.writeTo(requestbuffer);
Charset charset = Charset.forName("UTF-8");
MediaType contentType = body.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
String json = requestbuffer.readString(charset);
if (UrlEncoderUtils.hasUrlEncoded(json)) {
json = URLDecoder.decode(json, convertCharset(charset));
}
return CharacterHandler.jsonFormat(json);
} catch (IOException e) {
e.printStackTrace();
return "{\"error\": \"" + e.getMessage() + "\"}";
}
}
代码示例来源:origin: JessYanCoding/MVPArms
/**
* 解析服务器响应的内容
*
* @param responseBody {@link ResponseBody}
* @param encoding 编码类型
* @param clone 克隆后的服务器响应内容
* @return 解析后的响应结果
*/
private String parseContent(ResponseBody responseBody, String encoding, Buffer clone) {
Charset charset = Charset.forName("UTF-8");
MediaType contentType = responseBody.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {//content 使用 gzip 压缩
return ZipHelper.decompressForGzip(clone.readByteArray(), convertCharset(charset));//解压
} else if (encoding != null && encoding.equalsIgnoreCase("zlib")) {//content 使用 zlib 压缩
return ZipHelper.decompressToStringForZlib(clone.readByteArray(), convertCharset(charset));//解压
} else {//content 没有被压缩, 或者使用其他未知压缩方式
return clone.readString(charset);
}
}
代码示例来源:origin: spring-projects/spring-framework
private MockResponse putRequest(RecordedRequest request, String expectedRequestContent) {
assertTrue("Invalid request content-length",
Integer.parseInt(request.getHeader("Content-Length")) > 0);
String requestContentType = request.getHeader("Content-Type");
assertNotNull("No content-type", requestContentType);
Charset charset = StandardCharsets.ISO_8859_1;
if (requestContentType.contains("charset=")) {
String charsetName = requestContentType.split("charset=")[1];
charset = Charset.forName(charsetName);
}
assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
return new MockResponse().setResponseCode(202);
}
代码示例来源:origin: square/okhttp
logger.log(buffer.readString(charset));
logger.log("--> END " + request.method()
+ " (" + requestBody.contentLength() + "-byte body)");
logger.log(buffer.clone().readString(charset));
代码示例来源:origin: spring-projects/spring-framework
private MockResponse patchRequest(RecordedRequest request, String expectedRequestContent,
String contentType, byte[] responseBody) {
assertEquals("PATCH", request.getMethod());
assertTrue("Invalid request content-length",
Integer.parseInt(request.getHeader("Content-Length")) > 0);
String requestContentType = request.getHeader("Content-Type");
assertNotNull("No content-type", requestContentType);
Charset charset = StandardCharsets.ISO_8859_1;
if (requestContentType.contains("charset=")) {
String charsetName = requestContentType.split("charset=")[1];
charset = Charset.forName(charsetName);
}
assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
Buffer buf = new Buffer();
buf.write(responseBody);
return new MockResponse().setResponseCode(201)
.setHeader("Content-Length", responseBody.length)
.setHeader("Content-Type", contentType)
.setBody(buf);
}
代码示例来源:origin: spring-projects/spring-framework
private MockResponse postRequest(RecordedRequest request, String expectedRequestContent,
String location, String contentType, byte[] responseBody) {
assertEquals(1, request.getHeaders().values("Content-Length").size());
assertTrue("Invalid request content-length",
Integer.parseInt(request.getHeader("Content-Length")) > 0);
String requestContentType = request.getHeader("Content-Type");
assertNotNull("No content-type", requestContentType);
Charset charset = StandardCharsets.ISO_8859_1;
if (requestContentType.contains("charset=")) {
String charsetName = requestContentType.split("charset=")[1];
charset = Charset.forName(charsetName);
}
assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
Buffer buf = new Buffer();
buf.write(responseBody);
return new MockResponse()
.setHeader("Location", baseUrl + location)
.setHeader("Content-Type", contentType)
.setHeader("Content-Length", responseBody.length)
.setBody(buf)
.setResponseCode(201);
}
代码示例来源:origin: smuyyh/BookReader
logger.log(buffer.readString(charset));
logger.log(buffer.clone().readString(charset));
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
logger.log(buffer.readString(charset));
logger.log("--> END " + request.method()
+ " (" + requestBody.contentLength() + "-byte body)");
logger.log(buffer.clone().readString(charset));
代码示例来源:origin: com.squareup.okhttp3/logging-interceptor
logger.log(buffer.readString(charset));
logger.log("--> END " + request.method()
+ " (" + requestBody.contentLength() + "-byte body)");
logger.log(buffer.clone().readString(charset));
代码示例来源:origin: influxdata/influxdb-java
private String createJsonObject(final Map<String, Object> parameterMap) throws IOException {
Buffer b = new Buffer();
JsonWriter writer = JsonWriter.of(b);
writer.beginObject();
for (Entry<String, Object> pair : parameterMap.entrySet()) {
String name = pair.getKey();
Object value = pair.getValue();
if (value instanceof Number) {
Number number = (Number) value;
writer.name(name).value(number);
} else if (value instanceof String) {
writer.name(name).value((String) value);
} else if (value instanceof Boolean) {
writer.name(name).value((Boolean) value);
} else {
writer.name(name).value(String.valueOf(value));
}
}
writer.endObject();
return b.readString(Charset.forName("utf-8"));
}
代码示例来源:origin: huxq17/tractor
@Override public String readString(Charset charset) {
try {
return readString(size, charset);
} catch (EOFException e) {
throw new AssertionError(e);
}
}
代码示例来源:origin: huxq17/tractor
@Override public String readString(long byteCount, Charset charset) throws IOException {
require(byteCount);
if (charset == null) throw new IllegalArgumentException("charset == null");
return buffer.readString(byteCount, charset);
}
代码示例来源:origin: cn-ljb/rxjava_for_android
logger.log(buffer.readString(charset));
logger.log(buffer.clone().readString(charset));
代码示例来源:origin: Tickaroo/tikxml
/**
* Converts the buffers content to a String
* @param buffer
* @return
*/
public static String bufferToString(Buffer buffer) {
return buffer.readString(Charset.defaultCharset());
}
}
代码示例来源:origin: czp3009/bilibili-api
static JsonObject getJsonInBody(Response response) throws IOException {
ResponseBody responseBody = response.body();
BufferedSource bufferedSource = responseBody.source();
bufferedSource.request(Long.MAX_VALUE);
Buffer buffer = bufferedSource.buffer();
return JSON_PARSER.parse(
//必须要 clone 一次, 否则将导致流关闭
buffer.clone().readString(StandardCharsets.UTF_8)
).getAsJsonObject();
}
}
代码示例来源:origin: mrmike/Ok2Curl
@Test
public void testWhenLongResult_writeOnlyLimitedBytes() throws Exception {
final Buffer limited = new Buffer();
final BufferedSink source = Okio.buffer(new LimitedSink(limited, 10L));
source
.write(ByteString.encodeUtf8("0123456789012345678901234567890123456789")); // 40B
source.flush();
assertEquals("0123456789", limited.readString(Charset.forName("UTF-8")));
}
代码示例来源:origin: mrmike/Ok2Curl
@Test
public void testWhenShorterResult_writeOnlyThatData() throws Exception {
final Buffer limited = new Buffer();
final BufferedSink source = Okio.buffer(new LimitedSink(limited, 10L));
source
.write(ByteString.encodeUtf8("01234")); // 5B
source.flush();
assertEquals("01234", limited.readString(Charset.forName("UTF-8")));
}
}
代码示例来源:origin: Longi94/JavaSteam
@Test
public void testPostCall() throws IOException, InterruptedException {
WebAPI api = config.getWebAPI("TestInterface");
KeyValue result = api.call("POST", "TestFunction");
assertEquals("stringvalue", result.get("name").asString());
assertEquals("stringvalue", result.get("name").getValue());
RecordedRequest request = server.takeRequest();
assertEquals("/TestInterface/TestFunction/v1", request.getPath());
assertEquals("POST", request.getMethod());
assertEquals("format=vdf", request.getBody().readString(Charset.forName("UTF-8")));
}
内容来源于网络,如有侵权,请联系作者删除!