Allow json readers to be closed from another thread

This commit is contained in:
Jeremy Lakeman 2016-08-01 11:40:21 +09:30
parent 3b7e8abb99
commit 9e1b996232
4 changed files with 16 additions and 21 deletions

View File

@ -20,16 +20,17 @@
package org.servalproject.json;
import java.lang.StringBuilder;
import java.lang.NumberFormatException;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
public class JSONTokeniser {
PushbackReader reader;
final InputStream underlyingStream;
final PushbackReader reader;
Object pushedToken;
private static final boolean DUMP_JSON_TO_STDERR = false;
@ -96,15 +97,9 @@ public class JSONTokeniser {
}
// Can accept any PushbackReader, because we only need one character of unread().
public JSONTokeniser(PushbackReader pbrd)
{
reader = pbrd;
}
public JSONTokeniser(Reader rd)
{
reader = new PushbackReader(rd);
public JSONTokeniser(InputStream stream) throws UnsupportedEncodingException {
underlyingStream = stream;
reader = new PushbackReader(new InputStreamReader(stream, "UTF-8"));
}
private int _read() throws IOException
@ -498,6 +493,7 @@ public class JSONTokeniser {
public void close() throws IOException
{
this.underlyingStream.close();
this.reader.close();
}

View File

@ -83,7 +83,7 @@ public class KeyringCommon
if (!conn.getContentType().equals("application/json"))
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
if (status.http_status_code >= 300) {
status.json = new JSONTokeniser(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
status.json = new JSONTokeniser(conn.getErrorStream());
decodeRestfulStatus(status);
}
if (status.http_status_code == HttpURLConnection.HTTP_FORBIDDEN)
@ -112,7 +112,7 @@ public class KeyringCommon
Status status = receiveResponse(conn, expected_response_codes);
if (!conn.getContentType().equals("application/json"))
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
status.json = new JSONTokeniser(new InputStreamReader(status.input_stream, "UTF-8"));
status.json = new JSONTokeniser(status.input_stream);
return status;
}

View File

@ -28,7 +28,6 @@ import org.servalproject.servaldna.ServalDInterfaceException;
import org.servalproject.servaldna.SubscriberId;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
@ -49,14 +48,14 @@ public class MeshMSCommon
switch (conn.getResponseCode()) {
case HttpURLConnection.HTTP_NOT_FOUND:
case 419: // Authentication Timeout, for missing secret
JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
JSONTokeniser json = new JSONTokeniser(conn.getErrorStream());
Status status = decodeRestfulStatus(json);
throwRestfulResponseExceptions(status, conn.getURL());
throw new ServalDInterfaceException("unexpected MeshMS status = " + status.meshms_status_code + ", \"" + status.meshms_status_message + "\"");
}
for (int code: expected_response_codes) {
if (conn.getResponseCode() == code) {
JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getInputStream(), "UTF-8"));
JSONTokeniser json = new JSONTokeniser(conn.getInputStream());
return json;
}
}

View File

@ -89,7 +89,7 @@ public class RhizomeCommon
if (!conn.getContentType().equals("application/json"))
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
if (status.http_status_code >= 300) {
JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
JSONTokeniser json = new JSONTokeniser(conn.getErrorStream());
decodeRestfulStatus(status, json);
}
switch (status.http_status_code) {
@ -131,7 +131,7 @@ public class RhizomeCommon
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
if (status.input_stream == null)
throw new ServalDInterfaceException("unexpected HTTP response: " + status.http_status_code + " " + status.http_status_message);
return new JSONTokeniser(new InputStreamReader(status.input_stream, "UTF-8"));
return new JSONTokeniser(status.input_stream);
}
protected static void decodeHeaderBundleStatus(Status status, HttpURLConnection conn) throws ServalDInterfaceException