|
| 1 | +package org.schabi.newpipe.error; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.ObjectOutputStream; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * Ensures that a Exception is serializable. |
| 16 | + * This is |
| 17 | + */ |
| 18 | +public final class EnsureExceptionSerializable { |
| 19 | + private static final String TAG = "EnsureExSerializable"; |
| 20 | + |
| 21 | + private EnsureExceptionSerializable() { |
| 22 | + // No instance |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Ensures that an exception is serializable. |
| 27 | + * <br/> |
| 28 | + * If that is not the case a {@link WorkaroundNotSerializableException} is created. |
| 29 | + * |
| 30 | + * @param exception |
| 31 | + * @return if an exception is not serializable a new {@link WorkaroundNotSerializableException} |
| 32 | + * otherwise the exception from the parameter |
| 33 | + */ |
| 34 | + public static Exception ensureSerializable(@NonNull final Exception exception) { |
| 35 | + return checkIfSerializable(exception) |
| 36 | + ? exception |
| 37 | + : WorkaroundNotSerializableException.create(exception); |
| 38 | + } |
| 39 | + |
| 40 | + public static boolean checkIfSerializable(@NonNull final Exception exception) { |
| 41 | + try { |
| 42 | + // Check by creating a new ObjectOutputStream which does the serialization |
| 43 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 44 | + ObjectOutputStream oos = new ObjectOutputStream(bos) |
| 45 | + ) { |
| 46 | + oos.writeObject(exception); |
| 47 | + oos.flush(); |
| 48 | + |
| 49 | + bos.toByteArray(); |
| 50 | + } |
| 51 | + |
| 52 | + return true; |
| 53 | + } catch (final IOException ex) { |
| 54 | + Log.d(TAG, "Exception is not serializable", ex); |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static class WorkaroundNotSerializableException extends Exception { |
| 60 | + protected WorkaroundNotSerializableException( |
| 61 | + final Throwable notSerializableException, |
| 62 | + final Throwable cause) { |
| 63 | + super(notSerializableException.toString(), cause); |
| 64 | + setStackTrace(notSerializableException.getStackTrace()); |
| 65 | + } |
| 66 | + |
| 67 | + protected WorkaroundNotSerializableException(final Throwable notSerializableException) { |
| 68 | + super(notSerializableException.toString()); |
| 69 | + setStackTrace(notSerializableException.getStackTrace()); |
| 70 | + } |
| 71 | + |
| 72 | + public static WorkaroundNotSerializableException create( |
| 73 | + @NonNull final Exception notSerializableException |
| 74 | + ) { |
| 75 | + // Build a list of the exception + all causes |
| 76 | + final List<Throwable> throwableList = new ArrayList<>(); |
| 77 | + |
| 78 | + int pos = 0; |
| 79 | + Throwable throwableToProcess = notSerializableException; |
| 80 | + |
| 81 | + while (throwableToProcess != null) { |
| 82 | + throwableList.add(throwableToProcess); |
| 83 | + |
| 84 | + pos++; |
| 85 | + throwableToProcess = throwableToProcess.getCause(); |
| 86 | + } |
| 87 | + |
| 88 | + // Reverse list so that it starts with the last one |
| 89 | + Collections.reverse(throwableList); |
| 90 | + |
| 91 | + // Build exception stack |
| 92 | + WorkaroundNotSerializableException cause = null; |
| 93 | + for (final Throwable t : throwableList) { |
| 94 | + cause = cause == null |
| 95 | + ? new WorkaroundNotSerializableException(t) |
| 96 | + : new WorkaroundNotSerializableException(t, cause); |
| 97 | + } |
| 98 | + |
| 99 | + return cause; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments