Skip to content

Commit

Permalink
Merge pull request #12 from square/jwilson/froyo
Browse files Browse the repository at this point in the history
Support Froyo.
  • Loading branch information
rjrjr committed Aug 2, 2012
2 parents cb31fab + 66220f8 commit 434c4c6
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 99 deletions.
8 changes: 6 additions & 2 deletions src/main/java/libcore/io/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package libcore.io;

import libcore.util.Charsets;
import java.io.UnsupportedEncodingException;
import libcore.util.EmptyArray;

/**
Expand Down Expand Up @@ -156,6 +156,10 @@ public static String encode(byte[] in) {
out[index++] = '=';
break;
}
return new String(out, 0, index, Charsets.US_ASCII);
try {
return new String(out, 0, index, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/libcore/net/MimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private MimeUtils() {
* @return True iff there is a mimeType entry in the map.
*/
public static boolean hasMimeType(String mimeType) {
if (mimeType == null || mimeType.isEmpty()) {
if (mimeType == null || mimeType.length() == 0) {
return false;
}
return MIME_TYPE_TO_EXTENSION_MAP.containsKey(mimeType);
Expand All @@ -446,7 +446,7 @@ public static boolean hasMimeType(String mimeType) {
* @return The MIME type for the given extension or null iff there is none.
*/
public static String guessMimeTypeFromExtension(String extension) {
if (extension == null || extension.isEmpty()) {
if (extension == null || extension.length() == 0) {
return null;
}
return EXTENSION_TO_MIME_TYPE_MAP.get(extension);
Expand All @@ -458,7 +458,7 @@ public static String guessMimeTypeFromExtension(String extension) {
* @return True iff there is an extension entry in the map.
*/
public static boolean hasExtension(String extension) {
if (extension == null || extension.isEmpty()) {
if (extension == null || extension.length() == 0) {
return false;
}
return EXTENSION_TO_MIME_TYPE_MAP.containsKey(extension);
Expand All @@ -472,7 +472,7 @@ public static boolean hasExtension(String extension) {
* @return The extension for the given MIME type or null iff there is none.
*/
public static String guessExtensionFromMimeType(String mimeType) {
if (mimeType == null || mimeType.isEmpty()) {
if (mimeType == null || mimeType.length() == 0) {
return null;
}
return MIME_TYPE_TO_EXTENSION_MAP.get(mimeType);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/libcore/net/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import javax.net.ssl.SSLSocketFactory;
import libcore.io.IoUtils;
import libcore.net.spdy.SpdyConnection;
import libcore.util.Charsets;
import libcore.util.Libcore;
import libcore.util.Objects;

Expand Down Expand Up @@ -229,7 +228,7 @@ public SSLSocket setupSecureSocket(SSLSocketFactory sslSocketFactory,
HttpConnectionPool.INSTANCE.share(this);
} else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
throw new IOException("Unexpected NPN transport "
+ new String(selectedProtocol, Charsets.ISO_8859_1));
+ new String(selectedProtocol, "ISO-8859-1"));
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/libcore/net/http/HttpResponseCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.CacheRequest;
import java.net.CacheResponse;
Expand Down Expand Up @@ -84,10 +85,12 @@ public HttpResponseCache(File directory, long maxSize) throws IOException {
private String uriToKey(URI uri) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] md5bytes = messageDigest.digest(uri.toString().getBytes(Charsets.UTF_8));
byte[] md5bytes = messageDigest.digest(uri.toString().getBytes("UTF-8"));
return IntegralToString.bytesToHexString(md5bytes, false);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}

Expand Down Expand Up @@ -381,7 +384,7 @@ public Entry(InputStream in) throws IOException {

if (isHttps()) {
String blank = Streams.readAsciiLine(in);
if (!blank.isEmpty()) {
if (blank.length() != 0) {
throw new IOException("expected \"\" but was \"" + blank + "\"");
}
cipherSuite = Streams.readAsciiLine(in);
Expand Down Expand Up @@ -472,7 +475,7 @@ private Certificate[] readCertArray(InputStream in) throws IOException {
Certificate[] result = new Certificate[length];
for (int i = 0; i < result.length; i++) {
String line = Streams.readAsciiLine(in);
byte[] bytes = Base64.decode(line.getBytes(Charsets.US_ASCII));
byte[] bytes = Base64.decode(line.getBytes("US-ASCII"));
result[i] = certificateFactory.generateCertificate(
new ByteArrayInputStream(bytes));
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/libcore/net/http/HttpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.CookieHandler;
import java.net.URL;
import libcore.io.Streams;
import libcore.util.Charsets;
import libcore.util.Libcore;

final class HttpTransport implements Transport {
Expand Down Expand Up @@ -126,7 +125,7 @@ public void writeRequestHeaders() throws IOException {

int contentLength = httpEngine.requestHeaders.getContentLength();
RawHeaders headersToSend = getNetworkRequestHeaders();
byte[] bytes = headersToSend.toHeaderString().getBytes(Charsets.ISO_8859_1);
byte[] bytes = headersToSend.toHeaderString().getBytes("ISO-8859-1");

if (contentLength != -1 && bytes.length + contentLength <= MAX_REQUEST_BUFFER_LENGTH) {
requestOut = new BufferedOutputStream(socketOut, bytes.length + contentLength);
Expand Down Expand Up @@ -196,7 +195,7 @@ private RawHeaders getTunnelNetworkRequestHeaders() {
private void readHeaders(RawHeaders headers) throws IOException {
// parse the result headers until the first blank line
String line;
while (!(line = Streams.readAsciiLine(socketIn)).isEmpty()) {
while ((line = Streams.readAsciiLine(socketIn)).length() != 0) {
headers.addLine(line);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/libcore/net/http/HttpURLConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.net.Proxy;
import java.net.SocketPermission;
import java.net.URL;
import libcore.util.Charsets;
import java.security.Permission;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -444,7 +443,7 @@ private String getAuthorizationCredentials(RawHeaders responseHeaders, String ch

// base64 encode the username and password
String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
byte[] bytes = usernameAndPassword.getBytes(Charsets.ISO_8859_1);
byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
String encoded = Base64.encode(bytes);
return challenge.scheme + " " + encoded;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/libcore/net/http/RawHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public List<String> toNameValueBlock() {
String value = namesAndValues.get(i + 1);

// TODO: promote this check to where names and values are created
if (name.isEmpty() || value.isEmpty()
if (name.length() == 0 || value.length() == 0
|| name.indexOf('\0') != -1 || value.indexOf('\0') != -1) {
throw new IllegalArgumentException("Unexpected header: " + name + ": " + value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/libcore/net/http/ResponseHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package libcore.net.http;

import java.net.HttpURLConnection;
import libcore.util.ResponseSource;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
Expand All @@ -27,6 +26,7 @@
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import libcore.util.Objects;
import libcore.util.ResponseSource;

/**
* Parsed HTTP response headers.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/libcore/net/http/SpdyTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package libcore.net.http;

import libcore.net.spdy.SpdyConnection;
import libcore.net.spdy.SpdyStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.CacheRequest;
import java.util.List;
import libcore.net.spdy.SpdyConnection;
import libcore.net.spdy.SpdyStream;

final class SpdyTransport implements Transport {
private final HttpEngine httpEngine;
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/libcore/net/spdy/SpdyReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,7 +35,7 @@
*/
final class SpdyReader {
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final byte[] DICTIONARY = (""
private static final String DICTIONARY_STRING = ""
+ "optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-"
+ "languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi"
+ "f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser"
Expand All @@ -47,7 +48,15 @@ final class SpdyReader {
+ "ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe"
+ "pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic"
+ "ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1"
+ ".1statusversionurl\0").getBytes(UTF_8);
+ ".1statusversionurl\0";
public static final byte[] DICTIONARY;
static {
try {
DICTIONARY = DICTIONARY_STRING.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}

public final DataInputStream in;
public int flags;
Expand Down Expand Up @@ -184,7 +193,7 @@ private List<String> readNameValueBlock(int length) throws IOException {
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.isEmpty() || values.isEmpty()) {
if (name.length() == 0 || values.length() == 0) {
throw new IOException(); // TODO: PROTOCOL ERROR
}
entries.add(name);
Expand All @@ -206,6 +215,6 @@ private String readString() throws DataFormatException, IOException {
int length = nameValueBlockIn.readShort();
byte[] bytes = new byte[length];
Streams.readFully(nameValueBlockIn, bytes);
return new String(bytes, 0, length, UTF_8);
return new String(bytes, 0, length, "UTF-8");
}
}
2 changes: 1 addition & 1 deletion src/main/java/libcore/net/spdy/SpdyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void send404(SpdyStream stream, String path) throws IOException {
);
OutputStream out = stream.reply(responseHeaders);
String text = "Not found: " + path;
out.write(text.getBytes());
out.write(text.getBytes("UTF-8"));
out.close();
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/libcore/net/spdy/SpdyStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import static java.nio.ByteOrder.BIG_ENDIAN;
import java.util.List;
import libcore.io.Streams;
import libcore.util.Libcore;

import static java.nio.ByteOrder.BIG_ENDIAN;

/**
* A logical bidirectional stream.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/libcore/net/spdy/SpdyWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void writeNameValueBlockToBuffer() throws IOException {
nameValueBlockOut.writeShort(numberOfPairs);
for (String s : nameValueBlock) {
nameValueBlockOut.writeShort(s.length());
nameValueBlockOut.write(s.getBytes(SpdyReader.UTF_8));
nameValueBlockOut.write(s.getBytes("UTF-8"));
}
nameValueBlockOut.flush();
}
Expand Down
Loading

0 comments on commit 434c4c6

Please sign in to comment.