1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-22 15:42:00 +03:00

Merge branch 'okhttp_15' of https://github.com/square/okhttp into okhttp_15

This commit is contained in:
Jake Wharton
2014-03-09 23:00:41 -07:00
4 changed files with 41 additions and 18 deletions

View File

@@ -19,6 +19,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
@@ -58,16 +59,24 @@ public final class ByteString {
/** Returns a new byte string containing the {@code UTF-8} bytes of {@code s}. */
public static ByteString encodeUtf8(String s) {
ByteString byteString = new ByteString(s.getBytes(Util.UTF_8));
byteString.utf8 = s;
return byteString;
try {
ByteString byteString = new ByteString(s.getBytes(Util.UTF_8));
byteString.utf8 = s;
return byteString;
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
/** Constructs a new {@code String} by decoding the bytes as {@code UTF-8}. */
public String utf8() {
String result = utf8;
// We don't care if we double-allocate in racy code.
return result != null ? result : (utf8 = new String(data, Util.UTF_8));
try {
return result != null ? result : (utf8 = new String(data, Util.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
/**

View File

@@ -76,7 +76,9 @@ public final class InflaterSource implements Source {
}
if (sourceExhausted) throw new EOFException("source exhausted prematurely");
} catch (DataFormatException e) {
throw new IOException(e);
IOException io = new IOException();
io.initCause(e);
throw io;
}
}
}

View File

@@ -18,6 +18,7 @@ package com.squareup.okhttp.internal.okio;
import java.io.EOFException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@@ -253,19 +254,27 @@ public final class OkBuffer implements BufferedSource, BufferedSink, Cloneable {
Segment head = this.head;
if (head.pos + byteCount > head.limit) {
// If the string spans multiple segments, delegate to readBytes().
return new String(readBytes(byteCount), UTF_8);
try {
return new String(readBytes(byteCount), UTF_8);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
String result = new String(head.data, head.pos, (int) byteCount, UTF_8);
head.pos += byteCount;
this.size -= byteCount;
try {
String result = new String(head.data, head.pos, (int) byteCount, UTF_8);
head.pos += byteCount;
this.size -= byteCount;
if (head.pos == head.limit) {
this.head = head.pop();
SegmentPool.INSTANCE.recycle(head);
if (head.pos == head.limit) {
this.head = head.pop();
SegmentPool.INSTANCE.recycle(head);
}
return result;
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return result;
}
@Override public String readUtf8Line(boolean throwOnEof) throws EOFException {
@@ -369,8 +378,12 @@ public final class OkBuffer implements BufferedSource, BufferedSink, Cloneable {
/** Encodes {@code string} as UTF-8 and appends the bytes to this. */
@Override public OkBuffer writeUtf8(String string) {
byte[] data = string.getBytes(UTF_8);
return write(data, 0, data.length);
try {
byte[] data = string.getBytes(UTF_8);
return write(data, 0, data.length);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
@Override public OkBuffer write(byte[] source) {

View File

@@ -15,11 +15,10 @@
*/
package com.squareup.okhttp.internal.okio;
import java.nio.charset.Charset;
final class Util {
/** A cheap and type-safe constant for the UTF-8 Charset. */
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final String UTF_8 = "UTF-8";
private Util() {
}