/** * @throws org.sonar.api.utils.text.WriterException on any failure */ @Override public void close() { try { stream.close(); } catch (Exception e) { throw rethrow(e); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
/** * Example to writeJson using StreamingAPI */ private static void writeJson() throws IOException { OutputStream outputStream = new ByteArrayOutputStream(); JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8")); writer.beginObject(); // main object writer.name("message"); writer.value("Hi"); writer.name("place"); // save object Place writer.beginObject(); writer.name("name"); writer.value("World!"); writer.endObject(); writer.endObject(); writer.close(); System.out.println(outputStream.toString()); // print "{"message":"Hi","place":{"name":"World!"}}" } }
private void serializeFileAmount(Gson gson, String recordingPath) { try { JsonWriter writer = new JsonWriter(new FileWriter(recordingPath + FILE_AMOUNT)); gson.toJson(recordAndReplayUtils.getFileAmount(), Integer.class, writer); writer.close(); logger.info("File Amount Serialization completed!"); } catch (Exception e) { logger.error("Error while serializing file amount:", e); } }
private void serializeAttackEventExtraRecorder(Gson gson, String recordingPath) { try { JsonWriter writer = new JsonWriter(new FileWriter(recordingPath + DIRECTION_ORIGIN_LIST)); gson.toJson(directionAndOriginPosRecorderList.getList(), ArrayList.class, writer); writer.close(); directionAndOriginPosRecorderList.reset(); logger.info("AttackEvent extras serialization completed!"); } catch (Exception e) { logger.error("Error while serializing AttackEvent extras:", e); } }
private void serializeCharacterStateEventPositonMap(Gson gson, String recordingPath) { try { JsonWriter writer = new JsonWriter(new FileWriter(recordingPath + STATE_EVENT_POSITION)); gson.toJson(characterStateEventPositionMap.getIdToData(), HashMap.class, writer); writer.close(); characterStateEventPositionMap.reset(); logger.info("CharacterStateEvent positions Serialization completed!"); } catch (Exception e) { logger.error("Error while serializing CharacterStateEvent positions:", e); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
/** * Finish the generation of meta.json file and save it on local media. * * @return {@link Path} to the local meta.json produced. * @throws IOException if unable to write to file or if JSON is not valid */ public MetaFileWriterBuilder.UploadStep endMetaFileGeneration() throws IOException { if (jsonWriter == null) throw new NullPointerException( "endMetaFileGeneration: Json Writer in MetaFileWriter is null. This should not happen!"); jsonWriter.endArray(); jsonWriter.endObject(); jsonWriter.close(); Path tempMetaFilePath = Paths.get( metaProxy.getLocalMetaFileDirectory().toString(), metaFilePath.toFile().getName() + ".tmp"); // Rename the tmp file. tempMetaFilePath.toFile().renameTo(metaFilePath.toFile()); // Set the last modified time to snapshot time as generating manifest file may take some // time. metaFilePath.toFile().setLastModified(snapshotInstant.toEpochMilli()); logger.info("Finished writing to meta file: {}", metaFilePath); return this; }
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public void exportData(HttpServletResponse resp, Principal prin) throws IOException { resp.setContentType(MediaType.APPLICATION_JSON_VALUE); // this writer puts things out onto the wire JsonWriter writer = new JsonWriter(resp.getWriter()); writer.setIndent(" "); try { writer.beginObject(); writer.name("exported-at"); writer.value(dateFormat.format(new Date())); writer.name("exported-from"); writer.value(config.getIssuer()); writer.name("exported-by"); writer.value(prin.getName()); // delegate to the service to do the actual export exporter.exportData(writer); writer.endObject(); // end root writer.close(); } catch (IOException e) { logger.error("Unable to export data", e); } }
writer.flush(); } finally { writer.close();
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
void serializeRecordedEvents(List<RecordedEvent> events, String filePath) { try { JsonWriter writer = new JsonWriter(new FileWriter(filePath)); writer.beginObject(); writer.name("events"); writer.beginArray(); for (RecordedEvent event : events) { writer.beginObject(); writer.name("entityRef_ID").value(event.getEntityId()); writer.name("timestamp").value(event.getTimestamp()); writer.name("index").value(event.getIndex()); writer.name("event_class").value(event.getEvent().getClass().getName()); writer.name("event_data"); writer.beginObject(); writeSpecificEventData(writer, event.getEvent()); writer.endObject(); writer.endObject(); } writer.endArray(); writer.endObject(); writer.close(); } catch (Exception e) { logger.error("Error while serializing events:", e); } }
@Override public final void finalize(int id) { try { if (argumentsObjectStarted) { if (needObject()) { getWriter().endObject(); getWriter().endArray(); } } getWriter().name(ID).value(id); getWriter().endObject(); getWriter().close(); } catch (IOException e) { throw new RuntimeException(e); } }
final List<Void> readsDone = finishedReads.get(); jsonWriter.endArray(); jsonWriter.close(); if (LOGGER.isInfoEnabled()) { final int readCount = readsDone.size();
writeVertices(writer, vertices); writer.endObject(); writer.close();
/** * Ends encoding the current array. * * @return this writer. */ public JsonWriter endArray() throws IOException { return close(EMPTY_ARRAY, NONEMPTY_ARRAY, "]"); }
/** * Ends encoding the current object. * * @return this writer. */ public JsonWriter endObject() throws IOException { return close(EMPTY_OBJECT, NONEMPTY_OBJECT, "}"); }
@Override public void close() throws IOException { writer.close(); } }
@Override public void close() throws IOException { eventWriter.close(); getWriter().close(); }
@Override public void close() throws IOException { jsonWriter.endArray(); jsonWriter.flush(); jsonWriter.close(); } }