public virtual void test_from_SupplierExceptionOnCreate() { CheckedSupplier <Stream> supplier = () => { throw new IOException(); }; assertThrows(typeof(UncheckedIOException), () => ArrayByteSource.from(supplier)); }
public virtual void test_from_SupplierExceptionOnRead() { CheckedSupplier <Stream> supplier = () => { return(new InputStreamAnonymousInnerClass(this)); }; assertThrows(typeof(UncheckedIOException), () => ArrayByteSource.from(supplier)); }
/// <summary> /// Wraps a block of code, converting checked exceptions to unchecked. /// <pre> /// Unchecked.wrap(() -> { /// // any code that throws a checked exception /// } /// </pre> /// <para> /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// @param <T> the type of the result </param> /// <param name="block"> the code block to wrap </param> /// <returns> the result of invoking the block </returns> /// <exception cref="UncheckedIOException"> if an IO exception occurs </exception> /// <exception cref="RuntimeException"> if an exception occurs </exception> public static T wrap <T>(CheckedSupplier <T> block) { try { return(block()); } catch (Exception ex) { throw propagate(ex); } }
/// <summary> /// Creates an instance from an input stream. /// <para> /// This method use the supplier to open the input stream, extract the bytes and close the stream. /// It is intended that invoking the supplier opens the stream. /// It is not intended that an already open stream is supplied. /// /// </para> /// </summary> /// <param name="inputStreamSupplier"> the supplier of the input stream </param> /// <returns> the byte source </returns> /// <exception cref="UncheckedIOException"> if an IO error occurs </exception> public static ArrayByteSource from(CheckedSupplier <Stream> inputStreamSupplier) { return(Unchecked.wrap(() => { using (Stream @in = inputStreamSupplier()) { sbyte[] bytes = Unchecked.wrap(() => ByteStreams.toByteArray(@in)); return new ArrayByteSource(bytes); } })); }
//------------------------------------------------------------------------- /// <summary> /// Converts checked exceptions to unchecked based on the {@code Supplier} interface. /// <para> /// This wraps the specified supplier returning an instance that handles checked exceptions. /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// @param <R> the result type of the supplier </param> /// <param name="supplier"> the supplier to be decorated </param> /// <returns> the supplier instance that handles checked exceptions </returns> public static System.Func <R> supplier <R>(CheckedSupplier <R> supplier) { return(() => { try { return supplier(); } catch (Exception ex) { throw propagate(ex); } }); }