-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from bugsnag/simplify-json-stream
Simplify JsonStream, avoid unboxing errors, propagate exceptions more sanely, add test
- Loading branch information
Showing
21 changed files
with
165 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/androidTest/java/com/bugsnag/android/JsonStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bugsnag.android; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class JsonStreamTest extends BugsnagTestCase { | ||
public void testSaneValues() throws JSONException, IOException { | ||
StringWriter writer = new StringWriter(); | ||
JsonStream stream = new JsonStream(writer); | ||
|
||
Long nullLong = null; | ||
Boolean nullBoolean = null; | ||
String nullString = null; | ||
Integer nullInteger = null; | ||
Float nullFloat = null; | ||
Double nullDouble = null; | ||
|
||
stream.beginObject(); | ||
stream.name("nullLong").value(nullLong); | ||
stream.name("nullBoolean").value(nullBoolean); | ||
stream.name("nullString").value(nullString); | ||
stream.name("nullInteger").value(nullInteger); | ||
stream.name("nullFloat").value(nullFloat); | ||
stream.name("nullDouble").value(nullDouble); | ||
stream.name("string").value("string"); | ||
stream.name("int").value(123); | ||
stream.name("long").value(123l); | ||
stream.name("float").value(123.45f); | ||
stream.endObject(); | ||
|
||
JSONObject json = new JSONObject(writer.toString()); | ||
assertTrue(json.isNull("nullLong")); | ||
assertTrue(json.isNull("nullBoolean")); | ||
assertTrue(json.isNull("nullString")); | ||
assertTrue(json.isNull("nullInteger")); | ||
assertTrue(json.isNull("nullFloat")); | ||
assertTrue(json.isNull("nullDouble")); | ||
assertEquals("string", json.getString("string")); | ||
assertEquals(123, json.getInt("int")); | ||
assertEquals(123l, json.getLong("long")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bugsnag.android; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
import java.net.HttpURLConnection; | ||
import java.net.URLConnection; | ||
|
||
class IOUtils { | ||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; | ||
private static final int EOF = -1; | ||
|
||
public static void closeQuietly(final Closeable closeable) { | ||
try { | ||
if (closeable != null) { | ||
closeable.close(); | ||
} | ||
} catch (final IOException ioe) { | ||
// ignore | ||
} | ||
} | ||
|
||
public static void close(final URLConnection conn) { | ||
if (conn instanceof HttpURLConnection) { | ||
((HttpURLConnection) conn).disconnect(); | ||
} | ||
} | ||
|
||
public static int copy(final Reader input, final Writer output) throws IOException { | ||
char[] buffer = new char[DEFAULT_BUFFER_SIZE]; | ||
long count = 0; | ||
int n = 0; | ||
while (EOF != (n = input.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
count += n; | ||
} | ||
|
||
if (count > Integer.MAX_VALUE) { | ||
return -1; | ||
} | ||
|
||
return (int) count; | ||
} | ||
} |
Oops, something went wrong.