public static long checksum(File file) throws java.io.IOException, FileNotFoundException { FileInputStream fis = null; byte[] bytes = new byte[16384]; int len; try { fis = new FileInputStream(file); CRC32 chkSum = new CRC32(); len = fis.read(bytes); while (len != -1) { chkSum.update(bytes, 0, len); len = fis.read(bytes); } return chkSum.getValue(); } finally { quietClose(fis); } }
/** * Calculates the CRC32 checksum of the specified file. * * @param file the file on which to calculate the checksum * @return */ public static long checksum(final File file) throws IOException, FileNotFoundException { FileInputStream fis = null; final byte[] bytes = new byte[16384]; int len; try { fis = new FileInputStream(file); CRC32 chkSum = new CRC32(); len = fis.read(bytes); while (len != -1) { chkSum.update(bytes, 0, len); len = fis.read(bytes); } return chkSum.getValue(); } finally { quietClose(fis); } }
/** * Reads an entire file and returns the bytes. * * @param close * if true, close when finished reading. * @return file bytes. */ public static byte[] readInputStreamBytes(InputStream is, boolean close) throws IOException { byte[] bytes = null; if (is != null) { ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); try { int bytesRead = 0; byte[] buf = new byte[1024]; while ((bytesRead = is.read(buf)) != -1) { bout.write(buf, 0, bytesRead); } // end of while ((read(buf) != -1) bytes = bout.toByteArray(); } finally { if (close) { quietClose(is); } // end of if (is != null) } } return bytes; }
/** * Reads an entire file and returns the bytes. * * @param is * @param close if true, close when finished reading. * @return file bytes. */ public static byte[] readInputStreamBytes(final InputStream is, final boolean close) throws IOException { byte[] bytes = null; if (is != null) { final ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); try { int bytesRead = 0; final byte[] buf = new byte[1024]; while ((bytesRead = is.read(buf)) != -1) { bout.write(buf, 0, bytesRead); } // end of while ((read(buf) != -1) bytes = bout.toByteArray(); } finally { if (close) { quietClose(is); } // end of if (is != null) } } return bytes; }
/** * Calculates the CRC32 checksum of the specified file. * * @param file the file on which to calculate the checksum * @return */ public static long checksum(final File file) throws IOException, FileNotFoundException { FileInputStream fis = null; final byte[] bytes = new byte[16384]; int len; try { fis = new FileInputStream(file); CRC32 chkSum = new CRC32(); len = fis.read(bytes); while (len != -1) { chkSum.update(bytes, 0, len); len = fis.read(bytes); } return chkSum.getValue(); } finally { quietClose(fis); } }
/** * Reads an entire file and returns the bytes. * * @param is * @param close if true, close when finished reading. * @return file bytes. */ public static byte[] readInputStreamBytes(final InputStream is, final boolean close) throws IOException { byte[] bytes = null; if (is != null) { final ByteArrayOutputStream bout = new ByteArrayOutputStream(1024); try { int bytesRead = 0; final byte[] buf = new byte[1024]; while ((bytesRead = is.read(buf)) != -1) { bout.write(buf, 0, bytesRead); } // end of while ((read(buf) != -1) bytes = bout.toByteArray(); } finally { if (close) { quietClose(is); } // end of if (is != null) } } return bytes; }
} finally { quietClose(fis); quietClose(fout);
static Version getFrameworkVersion(ClassLoader loader) throws IOException { InputStream stream = loader.getResourceAsStream("connectors-framework.properties"); try { Properties props = new Properties(); props.load(stream); String version = props.getProperty(PROP_FRAMEWORK_VERSION); if (version == null) { throw new IllegalStateException("connectors-framework.properties does not contain a " + PROP_FRAMEWORK_VERSION + " property"); } if (StringUtil.isBlank(version)) { throw new IllegalStateException("connectors-framework.properties specifies a blank version"); } return Version.parse(version); } finally { IOUtil.quietClose(stream); } }
} finally { quietClose(fis); quietClose(fout);
static Version getFrameworkVersion(ClassLoader loader) throws IOException { InputStream stream = loader.getResourceAsStream("connectors-framework.properties"); try { Properties props = new Properties(); props.load(stream); String version = props.getProperty(PROP_FRAMEWORK_VERSION); if (version == null) { throw new IllegalStateException( "connectors-framework.properties does not contain a " + PROP_FRAMEWORK_VERSION + " property"); } if (StringUtil.isBlank(version)) { throw new IllegalStateException( "connectors-framework.properties specifies a blank version"); } return Version.parse(version); } finally { IOUtil.quietClose(stream); } }
static Version getFrameworkVersion(ClassLoader loader) throws IOException { InputStream stream = loader.getResourceAsStream("connectors-framework.properties"); try { Properties props = new Properties(); props.load(stream); String version = props.getProperty(PROP_FRAMEWORK_VERSION); if (version == null) { throw new IllegalStateException( "connectors-framework.properties does not contain a " + PROP_FRAMEWORK_VERSION + " property"); } if (StringUtil.isBlank(version)) { throw new IllegalStateException( "connectors-framework.properties specifies a blank version"); } return Version.parse(version); } finally { IOUtil.quietClose(stream); } }
/** * Get the resource as a byte array. */ public static byte[] getResourceAsBytes(Class<?> clazz, String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Get the resource as a byte array. * * @param clazz * @param res * @return */ public static byte[] getResourceAsBytes(final Class<?> clazz, final String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. final InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } final byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Read the entire stream into a String and return it. */ public static String getResourceAsString(Class<?> clazz, String res, Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }
/** * Get the resource as a byte array. * * @param clazz * @param res * @return */ public static byte[] getResourceAsBytes(final Class<?> clazz, final String res) { assert clazz != null && StringUtil.isNotBlank(res); // copy bytes from the stream to an array.. final InputStream ins = getResourceAsStream(clazz, res); if (ins == null) { throw new IllegalStateException("Resource not found: " + res); } final byte[] ret = inputStreamToBytes(ins); quietClose(ins); return ret; }
/** * Read the entire stream into a String and return it. * * @param clazz * @param res * @param charset * @return */ public static String getResourceAsString(final Class<?> clazz, final String res, final Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; final InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { final InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }
/** * Read the entire stream into a String and return it. * * @param clazz * @param res * @param charset * @return */ public static String getResourceAsString(final Class<?> clazz, final String res, final Charset charset) { assert clazz != null && StringUtil.isNotBlank(res); String ret = null; final InputStream ins = getResourceAsStream(clazz, res); if (ins != null) { try { final InputStreamReader rdr = new InputStreamReader(ins, charset); ret = readerToString(rdr); } finally { quietClose(ins); } } return ret; }
private boolean isRunningElsewhere(final JobKey jobKey) throws SchedulerException { if (!scheduler.getScheduler().getMetaData().isJobStoreClustered()) { return false; } DataSource dataSource = domainsHolder.getDomains().get(SyncopeConstants.MASTER_DOMAIN); Connection conn = DataSourceUtils.getConnection(dataSource); PreparedStatement stmt = null; ResultSet resultSet = null; try { stmt = conn.prepareStatement( "SELECT 1 FROM " + Constants.DEFAULT_TABLE_PREFIX + "FIRED_TRIGGERS " + "WHERE JOB_NAME = ? AND JOB_GROUP = ?"); stmt.setString(1, jobKey.getName()); stmt.setString(2, jobKey.getGroup()); resultSet = stmt.executeQuery(); return resultSet.next(); } catch (SQLException e) { throw new SchedulerException(e); } finally { IOUtil.quietClose(resultSet); IOUtil.quietClose(stmt); DataSourceUtils.releaseConnection(conn, dataSource); } }
info.setManifest(parser.parse()); } finally { IOUtil.quietClose(in);
@Test public void quietClose() { // test reader ExceptionReader rdr = new ExceptionReader(); IOUtil.quietClose(rdr); assertTrue(rdr.closeCalled); // test input stream ExceptionInputStream ins = new ExceptionInputStream(); IOUtil.quietClose(ins); assertTrue(ins.closeCalled); // test outputstream ExceptionOutputStream os = new ExceptionOutputStream(); IOUtil.quietClose(os); assertTrue(os.closeCalled); // test writer ExceptionWriter wrt = new ExceptionWriter(); IOUtil.quietClose(wrt); assertTrue(wrt.closeCalled); }