public static Response serializeError(Exception ex) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ex); oos.flush(); String info = new String(Base64.encodeBytesToBytes(baos.toByteArray()), MotanConstants.DEFAULT_CHARACTER); return Response.status(Status.EXPECTATION_FAILED).header(EXCEPTION_HEADER, ex.getClass()).entity(info) .build(); } catch (IOException e) { LoggerUtil.error("serialize " + ex.getClass() + " error", e); return Response.status(Status.INTERNAL_SERVER_ERROR).entity("serialization " + ex.getClass() + " error") .build(); } }
/** * Similar to {@link #encodeBytes(byte[])} but returns * a byte array instead of instantiating a String. This is more efficient * if you're working with I/O streams and have large data sets to encode. * * @param source The data to convert * @return The Base64-encoded data as a byte[] (of ASCII characters) * @throws NullPointerException if source array is null * @since 2.3.1 */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (java.io.IOException ex) { assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); } return encoded; }
byte[] encoded = encodeBytesToBytes(source, off, len, options);
/** * Similar to {@link #encodeBytes(byte[])} but returns * a byte array instead of instantiating a String. This is more efficient * if you're working with I/O streams and have large data sets to encode. * * @param source The data to convert * @return The Base64-encoded data as a byte[] (of ASCII characters) * @throws NullPointerException if source array is null * @since 2.3.1 */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (java.io.IOException ex) { assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); } return encoded; }
/** * Similar to {@link #encodeBytes(byte[])} but returns * a byte array instead of instantiating a String. This is more efficient * if you're working with I/O streams and have large data sets to encode. * * @param source The data to convert * @return The Base64-encoded data as a byte[] (of ASCII characters) * @throws NullPointerException if source array is null * @since 2.3.1 */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (java.io.IOException ex) { assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); } return encoded; }
byte[] encoded = encodeBytesToBytes(source, off, len, options);
byte[] encoded = encodeBytesToBytes(source, off, len, options);