/** * Utility method that returns a string which contains the stack trace of the given Exception * object. */ public static String getStacktraceFromException(Throwable e) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(byteOut); e.printStackTrace(writer); writer.close(); String message = new String(byteOut.toByteArray()); return message; }
@Test public void testCopy() throws Exception { String data = "rar"; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = new ByteArrayInputStream(data.getBytes()); TypeHandlerUtils.copy(inputStream, outputStream); Assert.assertEquals(data, new String(outputStream.toByteArray())); }
@Override public byte[] getFollowRequests(UserPublicKey owner) { byte[] dummy = null; FollowRequestData request = new FollowRequestData(owner, dummy); RowData[] requests = request.select(); if (requests == null) return new byte[4]; ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutput dout = new DataOutputStream(bout); try { dout.writeInt(requests.length); for (RowData req : requests) Serialize.serialize(req.data, dout); return bout.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } }
@Override public void updateOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; try { super.updateOperation(operation); Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( "UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " + "WHERE O.OPERATION_ID=?"); bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operation); stmt.setBytes(1, bao.toByteArray()); stmt.setInt(2, operation.getId()); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException( "Error occurred while update policy operation metadata", e); } catch (IOException e) { throw new OperationManagementDAOException( "Error occurred while serializing policy operation object", e); } finally { if (bao != null) { try { bao.close(); } catch (IOException e) { log.warn("Error occurred while closing ByteArrayOutputStream", e); } } if (oos != null) { try { oos.close(); } catch (IOException e) { log.warn("Error occurred while closing ObjectOutputStream", e); } } OperationManagementDAOUtil.cleanupResources(stmt); } }
@Override public byte[] getAllUsernamesGzip() throws IOException { try (PreparedStatement stmt = conn.prepareStatement("select name from users")) { ResultSet rs = stmt.executeQuery(); List<String> list = new ArrayList<>(); while (rs.next()) { String username = rs.getString("name"); list.add(username); } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try (DataOutputStream dout = new DataOutputStream(new GZIPOutputStream(bout))) { for (String uname : list) Serialize.serialize(uname, dout); } return bout.toByteArray(); } catch (SQLException sqe) { throw new IOException(sqe); } }