cleanup + formatting

This commit is contained in:
Hannes Janetzek 2013-06-15 16:45:50 +02:00
parent 48369f6baf
commit 85e7cee412

View File

@ -31,7 +31,6 @@ import android.util.Log;
public class LwHttp { public class LwHttp {
private static final String TAG = LwHttp.class.getName(); private static final String TAG = LwHttp.class.getName();
//private static final boolean DEBUG = false;
private final static byte[] HEADER_HTTP_OK = "200 OK".getBytes(); private final static byte[] HEADER_HTTP_OK = "200 OK".getBytes();
private final static byte[] HEADER_CONTENT_TYPE = "Content-Type".getBytes(); private final static byte[] HEADER_CONTENT_TYPE = "Content-Type".getBytes();
@ -58,12 +57,10 @@ public class LwHttp {
private final boolean mInflateContent; private final boolean mInflateContent;
private final byte[] mContentType; private final byte[] mContentType;
//private final String mExtension;
private int mContentLength = -1; private int mContentLength = -1;
public LwHttp(URL url, String contentType, String extension, boolean deflate) { public LwHttp(URL url, String contentType, String extension, boolean deflate) {
//mExtension = extension;
mContentType = contentType.getBytes(); mContentType = contentType.getBytes();
mInflateContent = deflate; mInflateContent = deflate;
@ -101,36 +98,12 @@ public class LwHttp {
} }
@Override @Override
public synchronized int read(byte[] buffer, int offset, int byteCount) throws IOException { public synchronized int read(byte[] buffer, int offset, int byteCount)
throws IOException {
return super.read(buffer, offset, byteCount); return super.read(buffer, offset, byteCount);
} }
} }
// public void setServer(URL url) {
//
// int port = url.getPort();
// if (port < 0)
// port = 80;
//
// String host = url.getHost();
// String path = url.getPath();
// Log.d(TAG, "open database: " + host + " " + port + " " + path);
//
// REQUEST_GET_START = ("GET " + path).getBytes();
//
// REQUEST_GET_END = ("." + mExtension + " HTTP/1.1" +
// "\nHost: " + host +
// "\nConnection: Keep-Alive" +
// "\n\n").getBytes();
//
// mHost = host;
// mPort = port;
//
// mRequestBuffer = new byte[1024];
// System.arraycopy(REQUEST_GET_START, 0,
// mRequestBuffer, 0, REQUEST_GET_START.length);
// }
public void close() { public void close() {
if (mSocket != null) { if (mSocket != null) {
try { try {
@ -150,6 +123,7 @@ public class LwHttp {
byte[] buf = buffer; byte[] buf = buffer;
boolean first = true; boolean first = true;
boolean ok = true;
int read = 0; int read = 0;
int pos = 0; int pos = 0;
@ -159,41 +133,46 @@ public class LwHttp {
mContentLength = -1; mContentLength = -1;
// header cannot be larger than BUFFER_SIZE for this to work // header cannot be larger than BUFFER_SIZE for this to work
for (; pos < read || (len = is.read(buf, read, BUFFER_SIZE - read)) >= 0; len = 0) { for (; (pos < read) || ((read < BUFFER_SIZE) &&
read += len; (len = is.read(buf, read, BUFFER_SIZE - read)) >= 0); len = 0) {
read += len;
// end of header lines // end of header lines
while (end < read && (buf[end] != '\n')) while (end < read && (buf[end] != '\n'))
end++; end++;
if (buf[end] == '\n') { if (buf[end] != '\n')
if (first) { continue;
// check only for OK
if (!ok) {
// ignore until end of header
} else if (first) {
first = false; first = false;
if (!check(HEADER_HTTP_OK, 6, buf, pos + 9, end)) { // check only for OK ("HTTP/1.? ".length == 9)
String line = new String(buf, pos, end - pos - 1); if (!check(HEADER_HTTP_OK, buf, pos + 9, end))
Log.d(TAG, ">" + line + "< "); ok = false;
return null;
}
} else if (end - pos == 1) { } else if (end - pos == 1) {
// check empty line (header end) // empty line (header end)
end += 1; end += 1;
break; break;
} else if (check(HEADER_CONTENT_TYPE, 12, buf, pos, end)) { } else if (check(HEADER_CONTENT_TYPE, buf, pos, end)) {
if (!check(mContentType, mContentType.length, buf, pos + 14, end)) if (!check(mContentType, buf, pos + HEADER_CONTENT_TYPE.length + 2, end))
return null; ok = false;
} else if (check(HEADER_CONTENT_LENGTH, 14, buf, pos, end)) { } else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) {
mContentLength = parseInt(pos + 16, end-1, buf); mContentLength = parseInt(pos + HEADER_CONTENT_LENGTH.length + 2, end - 1, buf);
} }
//String line = new String(buf, pos, end - pos - 1); if (!ok) {
//Log.d(TAG, ">" + line + "< " + mContentLength); String line = new String(buf, pos, end - pos - 1);
Log.d(TAG, ">" + line + "< ");
}
pos += (end - pos) + 1; pos += (end - pos) + 1;
end = pos; end = pos;
} }
}
if (!ok)
return null;
// back to start of content // back to start of content
is.reset(); is.reset();
@ -214,7 +193,7 @@ public class LwHttp {
try { try {
mSocket.close(); mSocket.close();
} catch (IOException e) { } catch (IOException e) {
Log.wtf(TAG, e);
} }
// Log.d(TAG, "not alive - recreate connection " + mMaxReq); // Log.d(TAG, "not alive - recreate connection " + mMaxReq);
@ -227,13 +206,12 @@ public class LwHttp {
mMaxReq = RESPONSE_EXPECTED_LIVES; mMaxReq = RESPONSE_EXPECTED_LIVES;
// Log.d(TAG, "create connection"); // Log.d(TAG, "create connection");
} else { } else {
// FIXME not sure if this is correct way to drain socket
int avail = mResponseStream.available(); int avail = mResponseStream.available();
if (avail > 0) { if (avail > 0) {
Log.d(TAG, "Consume left-over bytes: " + avail); Log.d(TAG, "Consume left-over bytes: " + avail);
while ((avail = mResponseStream.available()) > 0) while ((avail = mResponseStream.available()) > 0)
mResponseStream.read(buffer); mResponseStream.read(buffer);
Log.d(TAG, "Consumed bytes");
} }
} }
@ -306,6 +284,7 @@ public class LwHttp {
return pos + i; return pos + i;
} }
// parse (positive) integer from byte array // parse (positive) integer from byte array
protected static int parseInt(int pos, int end, byte[] buf) { protected static int parseInt(int pos, int end, byte[] buf) {
int val = 0; int val = 0;
@ -315,9 +294,11 @@ public class LwHttp {
return val; return val;
} }
private static boolean check(byte[] string, int length, byte[] buffer, private static boolean check(byte[] string, byte[] buffer,
int position, int available) { int position, int available) {
int length = string.length;
if (available - position < length) if (available - position < length)
return false; return false;
@ -335,6 +316,7 @@ public class LwHttp {
public int getContentLength() { public int getContentLength() {
return mContentLength; return mContentLength;
} }
/** /**
* Write custom tile url * Write custom tile url
* *
@ -347,5 +329,4 @@ public class LwHttp {
return 0; return 0;
} }
} }