/** * Serialize a profile measurement's value. * * The value produced by a Profile definition can be any numeric data type. The data * type depends on how the profile is defined by the user. The user should be able to * choose the data type that is most suitable for their use case. * * @param value The value to serialize. */ public static byte[] toBytes(Object value) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Output output = new Output(bos); kryo.get().writeClassAndObject(output, value); output.flush(); bos.flush(); return bos.toByteArray(); } catch(Throwable t) { LOG.error("Unable to serialize: " + value + " because " + t.getMessage(), t); throw new IllegalStateException("Unable to serialize " + value + " because " + t.getMessage(), t); } }
/** * Serialize a profile measurement's value. * * The value produced by a Profile definition can be any numeric data type. The data * type depends on how the profile is defined by the user. The user should be able to * choose the data type that is most suitable for their use case. * * @param value The value to serialize. */ public static byte[] toBytes(Object value) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Output output = new Output(bos); kryo.get().writeClassAndObject(output, value); output.flush(); bos.flush(); return bos.toByteArray(); } catch(Throwable t) { LOG.error("Unable to serialize: {} because {}", value, t.getMessage(), t); throw new IllegalStateException("Unable to serialize " + value + " because " + t.getMessage(), t); } }
/** * @see java.io.OutputStream#flush() */ @Override public void flush() throws IOException { this.writer.flush(); this.outputStream.flush(); }
/** * @see java.io.OutputStream#flush() */ @Override public void flush() throws IOException { this.writer.flush(); this.outputStream.flush(); }
@Override public void flush() throws IOException { if (tempMemoryBuffer != null) { tempMemoryBuffer.flush(); } else { bufOut.flush(); fout.flush(); } }
protected static void writeNiFiPropertiesFile(ByteArrayOutputStream nifiPropertiesOutputStream, String destPath) throws IOException { final Path nifiPropertiesPath = Paths.get(destPath, "nifi.properties"); try (FileOutputStream nifiProperties = new FileOutputStream(nifiPropertiesPath.toString())) { nifiPropertiesOutputStream.writeTo(nifiProperties); } finally { if (nifiPropertiesOutputStream != null) { nifiPropertiesOutputStream.flush(); nifiPropertiesOutputStream.close(); } } }
/** * Returns a copy of the JSON stream contained in the provided Message Context. * * @param messageContext Axis2 Message context that contains a JSON payload. * @return {@link java.io.InputStream} */ private static InputStream copyOfJsonPayload(MessageContext messageContext, boolean closable) { if (messageContext == null) { logger.error("#copyOfJsonPayload. Cannot copy JSON stream from message context. [null]."); return null; } InputStream jsonStream = jsonStream(messageContext, true); if (jsonStream == null) { logger.error("#copyOfJsonPayload. Cannot copy JSON stream from message context. [null] stream."); return null; } org.apache.commons.io.output.ByteArrayOutputStream out = new org.apache.commons.io.output.ByteArrayOutputStream(); try { IOUtils.copy(jsonStream, out); out.flush(); return closable ? new ByteArrayInputStream(out.toByteArray()) : toReadOnlyStream(new ByteArrayInputStream(out.toByteArray())); } catch (IOException e) { logger.error("#copyOfJsonPayload. Could not copy the JSON stream from message context. Error>>> " + e.getLocalizedMessage()); } return null; }
/** * Returns a reader that can read from the JSON payload contained in the provided message context as a JavaScript source.<br/> * The reader returns the '(' character at the beginning of the stream and marks the end with the ')' character.<br/> * The reader returned by this method can be directly used with the JavaScript {@link javax.script.ScriptEngine#eval(java.io.Reader)} method. * * @param messageContext Axis2 Message context * @return {@link java.io.InputStreamReader} */ public static Reader newJavaScriptSourceReader(MessageContext messageContext) { InputStream jsonStream = jsonStream(messageContext, true); if (jsonStream == null) { logger.error("#newJavaScriptSourceReader. Could not create a JavaScript source. Error>>> No JSON stream found."); return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { out.write('('); IOUtils.copy(jsonStream, out); out.write(')'); out.flush(); } catch (IOException e) { logger.error("#newJavaScriptSourceReader. Could not create a JavaScript source. Error>>> " + e.getLocalizedMessage()); return null; } return new InputStreamReader(new ByteArrayInputStream(out.toByteArray())); }
try { IOUtils.copy(jsonStream, out); out.flush(); InputStream inputStream = toReadOnlyStream(new ByteArrayInputStream(out.toByteArray())); messageContext.setProperty(inputStreamCache, inputStream);
stream.write(END_ARRAY.getBytes()); stream.flush(); this.stream = stream.toByteArray(); if (logger.isDebugEnabled()) {
baos.flush(); baos.close();
new org.apache.commons.io.output.ByteArrayOutputStream(); element.serialize(xmlStream); xmlStream.flush(); xmlEventReader = xmlInputFactory.createXMLEventReader( new XmlReaderDelegate(xmlInputFactory.createXMLStreamReader(
public static <T extends Value> T writeAndReadFields(Class<T> clazz, T in) throws IOException { // write to byte[] ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputView outputView = new DataOutputViewStreamWrapper(outputStream); in.write(outputView); outputStream.flush(); T out; try { out = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); throw new IOException("Cannot initialize the class: " + clazz); } // read from byte[] ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); DataInputView inputView = new DataInputViewStreamWrapper(inputStream); out.read(inputView); return out; }
bos.flush(); byte[] result = bos.toByteArray(); outstream.close();