static void gcm_oneReadByteCorrupt() throws Exception { System.out.println("Running gcm_oneReadByteCorrupt test"); // Encrypt 100 bytes with AES/GCM/PKCS5Padding byte[] ct = encryptedText("GCM", 100); // Corrupt the encrypted message ct = corruptGCM(ct); // Create stream for decryption CipherInputStream in = getStream("GCM", ct); try { in.read(); System.out.println(" Fail. No exception thrown."); } catch (IOException e) { Throwable ec = e.getCause(); if (ec instanceof AEADBadTagException) { System.out.println(" Pass."); } else { System.out.println(" Fail: " + ec.getMessage()); throw new RuntimeException(ec); } } }
/** * @param logger The logger to log exceptions on * @param message A message to use for logging exceptions * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default ObjLongConsumerWithThrowable<T, E> withLogging(final Logger logger, final String message) { return (final T v1, final long v2) -> { try { acceptWithThrowable(v1, v2); } catch (final Throwable throwable) { logger.error(message, v1, v2, throwable); throw throwable; } }; }
@Override public final void match( @Nonnull Consumer<Left<L>> left, @Nonnull Consumer<Right<Throwable>> right, @Nonnull Consumer<Neither> neither, @Nonnull Consumer<Both<L, Throwable>> both) { left.accept(this); }
@Override public final <R_> R_ map( @Nonnull Function<Left<L>, R_> left, @Nonnull Function<Right<Throwable>, R_> right, @Nonnull Function<Neither, R_> neither, @Nonnull Function<Both<L, Throwable>, R_> both) { return left.apply(this); }
@Override public final void match( @Nonnull Consumer<Left<Object>> left, @Nonnull Consumer<Right<Throwable>> right, @Nonnull Consumer<Neither> neither, @Nonnull Consumer<Both<Object, Throwable>> both) { neither.accept(this); }
@Override public final <R_> R_ map( @Nonnull Function<Left<Object>, R_> left, @Nonnull Function<Right<Throwable>, R_> right, @Nonnull Function<Neither, R_> neither, @Nonnull Function<Both<Object, Throwable>, R_> both) { return neither.apply(this); }
static void gcm_AEADBadTag() throws Exception { Cipher c; byte[] read = new byte[200]; System.out.println("Running gcm_AEADBadTag"); // Encrypt 100 bytes with AES/GCM/PKCS5Padding byte[] ct = encryptedText("GCM", 100); // Corrupt the encrypted message ct = corruptGCM(ct); // Create stream for decryption CipherInputStream in = getStream("GCM", ct); try { int size = in.read(read); throw new RuntimeException("Fail: CipherInputStream.read() " + "returned " + size + " and didn't throw an exception."); } catch (IOException e) { Throwable ec = e.getCause(); if (ec instanceof AEADBadTagException) { System.out.println(" Pass."); } else { System.out.println(" Fail: " + ec.getMessage()); throw new RuntimeException(ec); } } finally { in.close(); } }
static void cbc_readAllIllegalBlockSize() throws Exception { byte[] read = new byte[200]; System.out.println("Running cbc_readAllIllegalBlockSize test"); // Encrypt 96 byte with AES/CBC/PKCS5Padding byte[] ct = encryptedText("CBC", 96); // Create a stream with only 95 bytes of encrypted data CipherInputStream in = getStream("CBC", ct, 95); try { int s, size = 0; while ((s = in.read(read)) != -1) { size += s; } throw new RuntimeException("Fail: No IllegalBlockSizeException. " + "CipherInputStream.read() returned " + size); } catch (IOException e) { Throwable ec = e.getCause(); if (ec instanceof IllegalBlockSizeException) { System.out.println(" Pass."); } else { System.out.println(" Fail: " + ec.getMessage()); throw new RuntimeException(ec); } } }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoubleToIntFunctionWithThrowable<E> onException(final Consumer<Throwable> consumer) { return (final double v1) -> { try { return applyAsIntWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable); throw throwable; } }; }
/** * @param logger The logger to log exceptions on * @param message A message to use for logging exceptions * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoublePredicateWithThrowable<E> withLogging(final Logger logger, final String message) { return (final double v1) -> { try { return testWithThrowable(v1); } catch (final Throwable throwable) { logger.error(message, v1, throwable); throw throwable; } }; }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default LongToIntFunction thatReturnsOnCatch(final int defaultReturnValue) { return (final long v1) -> { try { return applyAsIntWithThrowable(v1); } catch(final Throwable throwable) { return defaultReturnValue; } }; }
/** * Overridden method of ObjLongConsumerWithThrowable that will call acceptWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @param v2 parameter to overridden method */ @Override default void accept(final T v1, final long v2) { try { acceptWithThrowable(v1, v2); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * Overridden method of LongToIntFunctionWithThrowable that will call applyAsIntWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @return the value */ @Override default int applyAsInt(final long v1) { try { return applyAsIntWithThrowable(v1); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * Overridden method of DoubleConsumerWithThrowable that will call acceptWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method */ @Override default void accept(final double v1) { try { acceptWithThrowable(v1); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * @throws E if an exception E has been thrown, it is rethrown by this method * @return An interface that is only returned if no exception has been thrown. */ default LongConsumer thatUnsafelyThrowsUnchecked() throws E { return (final long v1) -> { try { acceptWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); } }; }
/** * Overridden method of SupplierWithThrowable that will call getWithThrowable, but catching any exceptions. * * @return the value */ @Override default T get() { try { return getWithThrowable(); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * Overridden method of ToLongBiFunctionWithThrowable that will call applyAsLongWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @param v2 parameter to overridden method * @return the value */ @Override default long applyAsLong(final T v1, final U v2) { try { return applyAsLongWithThrowable(v1, v2); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoublePredicateWithThrowable<E> onException(final Consumer<Throwable> consumer) { return (final double v1) -> { try { return testWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable); throw throwable; } }; }
/** * Overridden method of ObjIntConsumerWithThrowable that will call acceptWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @param v2 parameter to overridden method */ @Override default void accept(final T v1, final int v2) { try { acceptWithThrowable(v1, v2); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * Overridden method of PredicateWithThrowable that will call testWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @return the value */ @Override default boolean test(final T v1) { try { return testWithThrowable(v1); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default Predicate<T> thatReturnsOnCatch(final boolean defaultReturnValue) { return (final T v1) -> { try { return testWithThrowable(v1); } catch(final Throwable throwable) { return defaultReturnValue; } }; }
/** * Overridden method of DoubleToLongFunctionWithThrowable that will call applyAsLongWithThrowable, but catching any exceptions. * * @param v1 parameter to overridden method * @return the value */ @Override default long applyAsLong(final double v1) { try { return applyAsLongWithThrowable(v1); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * @param logger The logger to log exceptions on * @param message A message to use for logging exceptions * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default PredicateWithThrowable<T, E> withLogging(final Logger logger, final String message) { return (final T v1) -> { try { return testWithThrowable(v1); } catch (final Throwable throwable) { logger.error(message, v1, throwable); throw throwable; } }; }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default PredicateWithThrowable<T, E> onException(final Consumer<Throwable> consumer) { return (final T v1) -> { try { return testWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable); throw throwable; } }; }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default ConsumerWithThrowable<T, E> onException(final Consumer<Throwable> consumer) { return (final T v1) -> { try { acceptWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable); throw throwable; } }; }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoubleToIntFunctionWithThrowable<E> onException(final java.util.function.BiConsumer<Throwable, Object[]> consumer) { return (final double v1) -> { try { return applyAsIntWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable, new Object[]{v1}); throw throwable; } }; }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default LongBinaryOperator thatReturnsOnCatch(final long defaultReturnValue) { return (final long v1, final long v2) -> { try { return applyAsLongWithThrowable(v1, v2); } catch(final Throwable throwable) { return defaultReturnValue; } }; }
/** * @throws E if an exception E has been thrown, it is rethrown by this method * @return An interface that is only returned if no exception has been thrown. */ default LongBinaryOperator thatUnsafelyThrowsUnchecked() throws E { return (final long v1, final long v2) -> { try { return applyAsLongWithThrowable(v1, v2); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return 0; } }; }
/** * @throws E if an exception E has been thrown, it is rethrown by this method * @return An interface that is only returned if no exception has been thrown. */ default ObjIntConsumer<T> thatUnsafelyThrowsUnchecked() throws E { return (final T v1, final int v2) -> { try { acceptWithThrowable(v1, v2); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); } }; }
/** * @return An interface that will wrap the result in an optional, and return an empty optional when an exception occurs. */ default IntFunction<java.util.Optional<R>> thatReturnsOptional() { return (final int v1) -> { try { return java.util.Optional.ofNullable(applyWithThrowable(v1)); } catch(Throwable throwable) { return java.util.Optional.empty(); } }; }
/** * @return An interface that completely ignores exceptions. Consider using this method withLogging() as well. */ default ObjLongConsumer<T> thatThrowsNothing() { return (final T v1, final long v2) -> { try { acceptWithThrowable(v1, v2); } catch(Throwable ignored) {} }; }
/** * Overridden method of LongSupplierWithThrowable that will call getAsLongWithThrowable, but catching any exceptions. * * @return the value */ @Override default long getAsLong() { try { return getAsLongWithThrowable(); } catch (final RuntimeException | Error exception) { throw exception; } catch (final Throwable throwable) { throw new SuppressedException(throwable); } }
/** * @param logger The logger to log exceptions on * @param message A message to use for logging exceptions * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoubleToIntFunctionWithThrowable<E> withLogging(final Logger logger, final String message) { return (final double v1) -> { try { return applyAsIntWithThrowable(v1); } catch (final Throwable throwable) { logger.error(message, v1, throwable); throw throwable; } }; }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default IntPredicateWithThrowable<E> onException(final Consumer<Throwable> consumer) { return (final int v1) -> { try { return testWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable); throw throwable; } }; }
/** * @param consumer An exception consumer. * @return An interface that will log all exceptions to given logger */ @SuppressWarnings("Duplicates") default DoubleToLongFunctionWithThrowable<E> onException(final java.util.function.BiConsumer<Throwable, Object[]> consumer) { return (final double v1) -> { try { return applyAsLongWithThrowable(v1); } catch (final Throwable throwable) { consumer.accept(throwable, new Object[]{v1}); throw throwable; } }; }