@@ -113,15 +113,35 @@ public static byte[] serializeSavedPropertiesToByteArray() throws IOException {
113113 public static native String getVMTemporaryDirectory ();
114114
115115 /**
116- * Decodes the exception encoded in {@code buffer } and throws it.
116+ * Decodes the exception encoded in {@code errorOrBuffer } and throws it.
117117 *
118- * @param buffer a native byte buffer containing an exception encoded by
118+ * @param errorOrBuffer an error code or a native byte errorOrBuffer containing an exception encoded by
119+ * {@link #encodeThrowable}. Error code values and their meanings are:
120+ *
121+ * <pre>
122+ * 0: native memory for the errorOrBuffer could not be allocated
123+ * -1: an OutOfMemoryError was thrown while encoding the exception
124+ * -2: some other throwable was thrown while encoding the exception
125+ * </pre>
126+ * @param errorOrBuffer a native byte errorOrBuffer containing an exception encoded by
119127 * {@link #encodeThrowable}
128+ * @param inJVMHeap [@code true} if executing in the JVM heap, {@code false} otherwise
120129 */
121- public static void decodeAndThrowThrowable (long buffer ) throws Throwable {
122- int encodingLength = U .getInt (buffer );
130+ public static void decodeAndThrowThrowable (long errorOrBuffer , boolean inJVMHeap ) throws Throwable {
131+ if (errorOrBuffer >= -2L && errorOrBuffer <= 0 ) {
132+ String context = String .format ("while encoding an exception to translate it %s the JVM heap" ,
133+ inJVMHeap ? "to" : "from" );
134+ if (errorOrBuffer == 0 ) {
135+ throw new InternalError ("native errorOrBuffer could not be allocated " + context );
136+ }
137+ if (errorOrBuffer == -1L ) {
138+ throw new OutOfMemoryError ("OutOfMemoryError occurred " + context );
139+ }
140+ throw new InternalError ("unexpected problem occurred " + context );
141+ }
142+ int encodingLength = U .getInt (errorOrBuffer );
123143 byte [] encoding = new byte [encodingLength ];
124- U .copyMemory (null , buffer + 4 , encoding , Unsafe .ARRAY_BYTE_BASE_OFFSET , encodingLength );
144+ U .copyMemory (null , errorOrBuffer + 4 , encoding , Unsafe .ARRAY_BYTE_BASE_OFFSET , encodingLength );
125145 throw TranslatedException .decodeThrowable (encoding );
126146 }
127147
0 commit comments