List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:com.viddu.handlebars.HandlebarsFileUtil.java
/** * Utility method to get contents of the File. * //from w w w. ja v a 2 s . co m * @param inputFile * @return * @throws IOException */ public static String getFileContents(File inputFile) throws IOException { FileInputStream stream = new FileInputStream(inputFile); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:Main.java
public static String calcHash(File file, String algo) throws Exception { MessageDigest digester = MessageDigest.getInstance(algo); FileInputStream is = new FileInputStream(file); DigestInputStream dis;/* w w w . java 2 s . c om*/ try { dis = new DigestInputStream(is, digester); for (byte[] buffer = new byte[1024 * 4]; dis.read(buffer) >= 0;) { // just read it } } finally { is.close(); } byte[] digest = digester.digest(); StringBuffer hash = new StringBuffer(digest.length * 2); for (int i = 0; i < digest.length; i++) { int b = digest[i] & 0xFF; if (b < 0x10) { hash.append('0'); } hash.append(Integer.toHexString(b)); } return hash.toString(); }
From source file:Main.java
public static byte[] getBytesFromFile(File file) { byte[] buffer = null; try {/*from www. j a v a 2 s. co m*/ if (file.exists()) { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
From source file:com.linkedin.databus.bootstrap.utils.BootstrapDropSource.java
@SuppressWarnings("static-access") public static int parseArgs(String[] args) throws IOException { CommandLineParser cliParser = new GnuParser(); Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen") .create(HELP_OPT_CHAR);/*from w w w . j a v a2 s. c om*/ Option sourceIdOption = OptionBuilder.withLongOpt(SOURCE_ID_OPT_LONG_NAME) .withDescription("Source ID for which tables need to be dropped").hasArg().withArgName("Source ID") .create(SOURCE_ID_OPT_CHAR); Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME) .withDescription("Bootstrap producer properties to use").hasArg().withArgName("property_file") .create(BOOTSTRAP_DB_PROP_OPT_CHAR); Option cmdLinePropsOption = OptionBuilder.withLongOpt(CMD_LINE_PROPS_OPT_LONG_NAME) .withDescription("Cmd line override of config properties. Semicolon separated.").hasArg() .withArgName("Semicolon_separated_properties").create(CMD_LINE_PROPS_OPT_CHAR); Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME) .withDescription("Log4j properties to use").hasArg().withArgName("property_file") .create(LOG4J_PROPS_OPT_CHAR); Options options = new Options(); options.addOption(helpOption); options.addOption(sourceIdOption); options.addOption(dbOption); options.addOption(cmdLinePropsOption); options.addOption(log4jPropsOption); CommandLine cmd = null; try { cmd = cliParser.parse(options, args); } catch (ParseException pe) { LOG.error("Bootstrap Physical Config: failed to parse command-line options.", pe); throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe); } if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) { String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR); PropertyConfigurator.configure(log4jPropFile); LOG.info("Using custom logging settings from file " + log4jPropFile); } else { PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n"); ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout); Logger.getRootLogger().removeAllAppenders(); Logger.getRootLogger().addAppender(defaultAppender); LOG.info("Using default logging settings"); } if (cmd.hasOption(HELP_OPT_CHAR)) { printCliHelp(options); System.exit(0); } if (!cmd.hasOption(SOURCE_ID_OPT_CHAR)) throw new RuntimeException("Source ID is not provided"); if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR)) throw new RuntimeException("Bootstrap config is not provided"); String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR); LOG.info("Loading bootstrap DB config from properties file " + propFile); _sBootstrapConfigProps = new Properties(); FileInputStream f = new FileInputStream(propFile); try { _sBootstrapConfigProps.load(f); } finally { f.close(); } if (cmd.hasOption(CMD_LINE_PROPS_OPT_CHAR)) { String cmdLinePropString = cmd.getOptionValue(CMD_LINE_PROPS_OPT_CHAR); updatePropsFromCmdLine(_sBootstrapConfigProps, cmdLinePropString); } return Integer.parseInt(cmd.getOptionValue(SOURCE_ID_OPT_CHAR)); }
From source file:Main.java
public static byte[] getBytesFromFile(String fileFullPath) { byte[] buffer = null; try {/*from w ww.ja v a 2 s . co m*/ File file = new File(fileFullPath); if (file.exists()) { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
From source file:Main.java
static void addDir(File dirObj, ZipOutputStream out) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { addDir(files[i], out);//from ww w .j av a 2s . c om continue; } FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); System.out.println(" Adding: " + files[i].getAbsolutePath()); out.putNextEntry(new ZipEntry(files[i].getAbsolutePath())); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } }
From source file:com.openmeap.util.SSLUtils.java
public static KeyStore loadKeyStore(String keyStoreFileName, String password) throws CertificateException, IOException, NoSuchAlgorithmException, KeyStoreException { KeyStore ks = null;/*from w w w. j a v a2 s .c om*/ FileInputStream fis = null; try { fis = new FileInputStream(keyStoreFileName); ks = loadKeyStore(fis, password); } finally { if (fis != null) { fis.close(); } } return ks; }
From source file:com.amazonaws.kinesis.dataviz.kinesisclient.KinesisApplication.java
/** * @param propertiesFile//from w ww. j a va 2s . com * @throws IOException Thrown when we run into issues reading properties */ private static void loadProperties(String propertiesFile) throws IOException { FileInputStream inputStream = new FileInputStream(propertiesFile); Properties properties = new Properties(); try { properties.load(inputStream); } finally { inputStream.close(); } String appNameOverride = properties.getProperty(ConfigKeys.APPLICATION_NAME_KEY); if (appNameOverride != null) { applicationName = appNameOverride; } LOG.info("Using application name " + applicationName); String streamNameOverride = properties.getProperty(ConfigKeys.STREAM_NAME_KEY); if (streamNameOverride != null) { streamName = streamNameOverride; } LOG.info("Using stream name " + streamName); String kinesisEndpointOverride = properties.getProperty(ConfigKeys.KINESIS_ENDPOINT_KEY); if (kinesisEndpointOverride != null) { kinesisEndpoint = kinesisEndpointOverride; } LOG.info("Using Kinesis endpoint " + kinesisEndpoint); String initialPositionOverride = properties.getProperty(ConfigKeys.INITIAL_POSITION_IN_STREAM_KEY); if (initialPositionOverride != null) { initialPositionInStream = InitialPositionInStream.valueOf(initialPositionOverride); } LOG.info("Using initial position " + initialPositionInStream.toString() + " (if a checkpoint is not found)."); String redisEndpointOverride = properties.getProperty(ConfigKeys.REDIS_ENDPOINT); if (redisEndpointOverride != null) { redisEndpoint = redisEndpointOverride; } LOG.info("Using Redis endpoint " + redisEndpoint); String redisPortOverride = properties.getProperty(ConfigKeys.REDIS_PORT); if (redisPortOverride != null) { try { redisPort = Integer.parseInt(redisPortOverride); } catch (Exception e) { } } LOG.info("Using Redis port " + redisPort); }
From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.PeriodicResourceUpdater.java
static boolean filesEqual(final File a, final File b) throws IOException { if (!a.exists() && !b.exists()) { return true; }// www . j ava 2 s . c om if (!a.exists() || !b.exists()) { return false; } if (a.length() != b.length()) { return false; } FileInputStream fis = new FileInputStream(a); final String md5a = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); fis = new FileInputStream(b); final String md5b = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); if (md5a.equals(md5b)) { return true; } return false; }
From source file:Main.java
/** * Copy file from oldPath to newPath// w ww . j a v a2 s . co m * * @param oldPath * @param newPath */ public static void copyFile(String oldPath, String newPath) { try { int byteread = 0; File oldfile = new File(oldPath); File newfile = new File(newPath); if (newfile.exists()) { newfile.delete(); } newfile.createNewFile(); if (oldfile.exists()) { FileInputStream inStream = new FileInputStream(oldPath); FileOutputStream outStream = new FileOutputStream(newPath); byte[] buffer = new byte[1024]; while ((byteread = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, byteread); } inStream.close(); outStream.close(); } } catch (Exception e) { e.printStackTrace(); } }