/** Forcibly stop consuming the stream. */ public void stopConsuming() { if (stream != null) { stopped = true; IoSupport.closeQuietly(stream); IoSupport.closeQuietly(reader); } }
/** * Closes the processes input, output and error streams. * * @param process * the process * @return <code>true</code> if the streams were closed */ public static boolean closeQuietly(final Process process) { if (process != null) { closeQuietly(process.getInputStream()); closeQuietly(process.getErrorStream()); closeQuietly(process.getOutputStream()); return true; } return false; }
/** Handle multiple streams inside <code>f</code> and ensure that they get closed properly. */ public static <A> A withStreams(InputStream[] in, OutputStream[] out, Function2<InputStream[], OutputStream[], A> f) { try { return f.apply(in, out); } finally { for (Closeable a : in) { IoSupport.closeQuietly(a); } for (Closeable a : out) { IoSupport.closeQuietly(a); } } }
/** * Handle a closeable resource inside <code>f</code> and ensure it gets closed properly. */ public static <A, B extends Closeable> A withResource(B b, Function<B, A> f) { try { return f.apply(b); } finally { IoSupport.closeQuietly(b); } }
/** * Handle a closeable resource inside <code>f</code> and ensure it gets closed properly. */ public static <A, B extends Closeable> A withResource(B b, Fn<? super B, ? extends A> f) { try { return f.apply(b); } finally { IoSupport.closeQuietly(b); } }
/** * Handle a stream inside <code>f</code> and ensure that <code>s</code> gets closed properly. * * @deprecated use {@link #withResource(java.io.Closeable, org.opencastproject.util.data.Function)} instead */ @Deprecated public static <A> A withStream(InputStream s, Function<InputStream, A> f) { try { return f.apply(s); } finally { IoSupport.closeQuietly(s); } }
/** * Handle a stream inside <code>f</code> and ensure that <code>s</code> gets closed properly. * * @deprecated use {@link #withResource(java.io.Closeable, org.opencastproject.util.data.Function)} instead */ @Deprecated public static <A> A withStream(OutputStream s, Function<OutputStream, A> f) { try { return f.apply(s); } finally { IoSupport.closeQuietly(s); } }
/** * Handle a stream inside <code>e</code> and ensure that <code>s</code> gets closed properly. * * @return true, if the file exists, false otherwise */ public static boolean withFile(File file, Effect2<OutputStream, File> e) { OutputStream s = null; try { s = new FileOutputStream(file); e.apply(s, file); return true; } catch (FileNotFoundException ignore) { return false; } finally { IoSupport.closeQuietly(s); } }
/** * Serializes the document to a XML string * * @param document * the document * @return the serialized XML string * @throws IOException * if there is an error transforming the dom to a stream */ public static String toXmlString(Document document) throws IOException { InputStream inputStream = null; try { inputStream = serializeDocument(document); return IOUtils.toString(inputStream, "UTF-8"); } finally { IoSupport.closeQuietly(inputStream); } }
/** * Loads a workflow definition from the given input stream. * * @param in * the input stream * @return the workflow definition * @throws WorkflowParsingException * if creating the workflow definition fails */ public static WorkflowDefinition parseWorkflowDefinition(InputStream in) throws WorkflowParsingException { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(in), WorkflowDefinitionImpl.class).getValue(); } catch (Exception e) { throw new WorkflowParsingException(e); } finally { IoSupport.closeQuietly(in); } }
/** * Loads a set of workflow instances from the given input stream. * * @param in * the input stream * @return the set of workflow instances * @throws WorkflowParsingException * if creating the workflow instance set fails */ public static WorkflowSet parseWorkflowSet(InputStream in) throws WorkflowParsingException { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(in), WorkflowSetImpl.class).getValue(); } catch (Exception e) { throw new WorkflowParsingException(e); } finally { IoSupport.closeQuietly(in); } }
public static String decrypt(Key key, String encryptedText) { final ByteArrayOutputStream decrypted; try { final Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); final byte[] decodeBuffer = new byte[1024]; final ByteArrayInputStream encrypted = new ByteArrayInputStream(decodeFromHex(encryptedText)); decrypted = new ByteArrayOutputStream(); final CipherInputStream cipherStream = new CipherInputStream(encrypted, cipher); try { int n; while ((n = cipherStream.read(decodeBuffer)) > 0) { decrypted.write(decodeBuffer, 0, n); } } finally { IoSupport.closeQuietly(decrypted); IoSupport.closeQuietly(cipherStream); IoSupport.closeQuietly(encrypted); } } catch (Exception e) { throw new RuntimeException("Error decrypting text", e); } return decrypted.toString(); }
/** * Writes an xml representation to an input stream and return it. * * @param document * the document * @return the input stream containing the serialized xml representation * @throws IOException * if there is an error transforming the dom to a stream */ public static InputStream serializeDocument(Document document) throws IOException { ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); XmlUtil.toXml(document, out); return new ByteArrayInputStream(out.toByteArray()); } finally { IoSupport.closeQuietly(out); } }
/** * Loads workflow statistics from the given input stream. * * @param in * the input stream * @return the workflow statistics * @throws WorkflowParsingException * if creating the workflow statistics fails */ public static WorkflowStatistics parseWorkflowStatistics(InputStream in) throws WorkflowParsingException { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(in), WorkflowStatistics.class).getValue(); } catch (Exception e) { throw new WorkflowParsingException(e); } finally { IoSupport.closeQuietly(in); } }
/** * Loads workflow definitions from the given input stream. * * @param in * @return the list of workflow definitions */ public static List<WorkflowDefinition> parseWorkflowDefinitions(InputStream in) throws WorkflowParsingException { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); WorkflowDefinitionImpl[] impls = unmarshaller.unmarshal(new StreamSource(in), WorkflowDefinitionImpl[].class) .getValue(); List<WorkflowDefinition> list = new ArrayList<WorkflowDefinition>(); for (WorkflowDefinitionImpl impl : impls) { list.add(impl); } return list; } catch (Exception e) { throw new WorkflowParsingException(e); } finally { IoSupport.closeQuietly(in); } }
/** * Attention: The media package reference is not being cloned so that calling <code>getMediaPackage()</code> on the * clone yields null. */ @Override public Object clone() { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = null; try { Marshaller marshaller = MediaPackageImpl.context.createMarshaller(); marshaller.marshal(this, out); Unmarshaller unmarshaller = MediaPackageImpl.context.createUnmarshaller(); in = new ByteArrayInputStream(out.toByteArray()); return unmarshaller.unmarshal(in); } catch (JAXBException e) { throw new RuntimeException(e.getLinkedException() != null ? e.getLinkedException() : e); } finally { IoSupport.closeQuietly(in); } }
/** * Loads a workflow instance from the given input stream. * * @param in * the input stream * @return the workflow instance * @throws WorkflowParsingException * if creating the workflow instance fails */ public static WorkflowInstanceImpl parseWorkflowInstance(InputStream in) throws WorkflowParsingException { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); WorkflowInstanceImpl workflow = unmarshaller.unmarshal(new StreamSource(in), WorkflowInstanceImpl.class) .getValue(); workflow.init(); return workflow; } catch (Exception e) { throw new WorkflowParsingException(e); } finally { IoSupport.closeQuietly(in); } }
/** * Reads the media package from the input stream. * * @param xml * the input stream * @return the deserialized media package */ public static MediaPackageImpl valueOf(InputStream xml) throws MediaPackageException { try { Unmarshaller unmarshaller = context.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(xml), MediaPackageImpl.class).getValue(); } catch (JAXBException e) { throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e); } finally { IoSupport.closeQuietly(xml); } }
/** * Writes the contents variable to the {@code File} located at the filename. * * @param filename * The {@code File} of the local file you wish to write to. * @param contents * The contents of the file you wish to create. */ public static void writeUTF8File(String filename, String contents) throws IOException { FileWriter out = new FileWriter(filename); try { out.write(contents); } finally { closeQuietly(out); } }
/** * Handle a stream inside <code>f</code> and ensure that <code>s</code> gets closed properly. * <p> * <strong>Please note:</strong> The outcome of <code>f</code> is wrapped into a some. Therefore <code>f</code> is * <em>not</em> allowed to return <code>null</code>. Use an <code>Option</code> instead and flatten the overall * result. * * @return none, if the file does not exist */ public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } }