Skip to content

close JsonWriters #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Fixes
* Log accessory names instead of futures. [#150](https://github.com/hap-java/HAP-Java/issues/150)
* Fix rotation speed data type (BREAKING API CHANGE). According to HAP specification it must be float
* Close JsonWriters [#149](https://github.com/hap-java/HAP-Java/issues/149)

# HAP-Java 2.0.0
* major refactoring to support optional characteristics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;

public class AccessoryController {

Expand Down Expand Up @@ -64,9 +65,9 @@ public HttpResponse listing() throws Exception {
.add("services", serviceArrayBuilders.get(accessory.getId())));
}

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Json.createWriter(baos)
.write(Json.createObjectBuilder().add("accessories", accessories).build());
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonWriter jsonWriter = Json.createWriter(baos)) {
jsonWriter.write(Json.createObjectBuilder().add("accessories", accessories).build());
return new HapJsonResponse(baos.toByteArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public HttpResponse get(HttpRequest request) throws Exception {
"Accessory " + aid + " has no characteristics or does not exist. Request: " + uri);
}
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Json.createWriter(baos)
.write(
Json.createObjectBuilder().add("characteristics", characteristics.build()).build());
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonWriter jsonWriter = Json.createWriter(baos)) {
jsonWriter.write(
Json.createObjectBuilder().add("characteristics", characteristics.build()).build());
return new HapJsonResponse(baos.toByteArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;

public class EventController {

Expand All @@ -24,8 +25,9 @@ public HttpResponse getMessage(int accessoryId, int iid, EventableCharacteristic

JsonObject data = Json.createObjectBuilder().add("characteristics", characteristics).build();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Json.createWriter(baos).write(data);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonWriter jsonWriter = Json.createWriter(baos)) {
jsonWriter.write(data);
byte[] dataBytes = baos.toByteArray();

return new EventResponse(dataBytes);
Expand All @@ -44,9 +46,9 @@ public HttpResponse getMessage(ArrayList<PendingNotification> notifications) thr
}

JsonObject data = Json.createObjectBuilder().add("characteristics", characteristics).build();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Json.createWriter(baos).write(data);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonWriter jsonWriter = Json.createWriter(baos)) {
jsonWriter.write(data);
byte[] dataBytes = baos.toByteArray();

return new EventResponse(dataBytes);
Expand Down