List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:com.twinsoft.convertigo.engine.AttachmentManager.java
static public AttachmentDetails getAttachment(Element eAttachment) { try {//www .j a v a 2 s.c o m if ("attachment".equals(eAttachment.getTagName()) && "attachment".equals(eAttachment.getAttribute("type"))) { String attr; final String name = eAttachment.getAttribute("name"); final String contentType = eAttachment.getAttribute("content-type"); final byte[][] data = new byte[1][]; if ((attr = eAttachment.getAttribute("local-url")) != null && attr.length() > 0) { FileInputStream fis = null; try { fis = new FileInputStream(attr); fis.read(data[0] = new byte[fis.available()]); } finally { if (fis != null) fis.close(); } } else if ((attr = eAttachment.getAttribute("encoding")) != null && attr.length() > 0) { if ("base64".equals(attr)) { data[0] = Base64.decodeBase64(eAttachment.getTextContent()); } } if (data[0] != null) { return new AttachmentDetails() { public byte[] getData() { return data[0]; } public String getName() { return name; } public String getContentType() { return contentType; } }; } } } catch (Exception e) { Engine.logEngine.error("failed to make AttachmentDetails", e); } return null; }
From source file:com.linkedin.databus.bootstrap.utils.BootstrapAvroFileSeederMain.java
@SuppressWarnings("static-access") public static void 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 ww w. j a v a2 s. co m Option sourcesOption = OptionBuilder.withLongOpt(PHYSICAL_CONFIG_OPT_LONG_NAME) .withDescription("Bootstrap producer properties to use").hasArg().withArgName("property_file") .create(PHYSICAL_CONFIG_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 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(sourcesOption); options.addOption(dbOption); options.addOption(log4jPropsOption); CommandLine cmd = null; try { cmd = cliParser.parse(options, args); } catch (ParseException pe) { LOG.fatal("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); Logger.getRootLogger().setLevel(Level.INFO); //using info as the default log level LOG.info("Using default logging settings. Log Level is :" + Logger.getRootLogger().getLevel()); } if (cmd.hasOption(HELP_OPT_CHAR)) { printCliHelp(options); System.exit(0); } if (!cmd.hasOption(PHYSICAL_CONFIG_OPT_CHAR)) throw new RuntimeException("Sources Config is not provided; use --help for usage"); if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR)) throw new RuntimeException("Bootstrap config is not provided; use --help for usage"); _sSourcesConfigFile = cmd.getOptionValue(PHYSICAL_CONFIG_OPT_CHAR); String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR); LOG.info("Loading bootstrap DB config from properties file " + propFile); _sBootstrapConfigProps = new Properties(); FileInputStream fis = new FileInputStream(propFile); try { _sBootstrapConfigProps.load(fis); } finally { fis.close(); } }
From source file:Main.java
public static long getFileSizes(File f) { FileInputStream fis = null; try {/*w w w .j ava2 s .co m*/ fis = new FileInputStream(f); return fis.available(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fis != null) fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return -1; }
From source file:org.apache.coheigea.cxf.fediz.tomcat.sso.OIDCTest.java
@BeforeClass public static void init() throws Exception { System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "info"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "info"); System.setProperty("org.apache.commons.logging.simplelog.log.org.springframework.webflow", "info"); System.setProperty("org.apache.commons.logging.simplelog.log.org.springframework.security.web", "info"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.cxf.fediz", "info"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.cxf", "info"); idpHttpsPort = System.getProperty("idp.https.port"); Assert.assertNotNull("Property 'idp.https.port' null", idpHttpsPort); rpHttpsPort = System.getProperty("rp.https.port"); Assert.assertNotNull("Property 'rp.https.port' null", rpHttpsPort); oidcHttpsPort = System.getProperty("oidc.https.port"); Assert.assertNotNull("Property 'oidc.https.port' null", oidcHttpsPort); // Start IdP + OIDC servers idpServer = startServer(Server.IDP, idpHttpsPort); oidcServer = startServer(Server.OIDC, oidcHttpsPort); // Log onto OIDC + create a new client loginToClientsPage(rpHttpsPort, oidcHttpsPort, idpHttpsPort); // Substitute in Client ID + Secret into RP configuration String currentDir = new File(".").getCanonicalPath(); File rpConfig = new File( currentDir + File.separator + "target/tomcat/rp/webapps/cxfWebapp/WEB-INF/cxf-service.xml"); FileInputStream inputStream = new FileInputStream(rpConfig); String content = IOUtils.toString(inputStream, "UTF-8"); inputStream.close(); content = content.replaceAll("consumer-id", storedClientId); content = content.replaceAll("this-is-a-secret", storedClientSecret); rpConfig = new File( currentDir + File.separator + "target/tomcat/rp/webapps/cxfWebapp/WEB-INF/cxf-service.xml"); try (FileOutputStream outputStream = new FileOutputStream(rpConfig)) { IOUtils.write(content, outputStream, "UTF-8"); }/*from w w w.ja v a 2s . c o m*/ // Start RP server rpServer = startServer(Server.RP, rpHttpsPort); }
From source file:com.nieyue.weixin.ssl.ClientCustomSSL.java
/** * ?/* w w w . j ava 2 s . c o m*/ * @return * @throws Exception */ public static CloseableHttpClient getCloseableHttpClient() throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream( new File(ClientCustomSSL.class.getResource("").getPath() + "apiclient_cert.p12")); //? //FileInputStream instream = new FileInputStream("src/com/nieyue/weixin/ssl/apiclient_cert.p12"); try { keyStore.load(instream, ThirdParty.GetValueByKey(ThirdParty.WEIXIN_YAYAO_MCH_ID).toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs @SuppressWarnings("deprecation") SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, ThirdParty.GetValueByKey(ThirdParty.WEIXIN_YAYAO_MCH_ID).toCharArray()) .build(); // Allow TLSv1 protocol only @SuppressWarnings("deprecation") SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); return httpclient; }
From source file:com.taksmind.karma.functions.scheduled.Log.java
public static void uploadFile() throws IOException { try {//from w w w. j a v a 2 s . co m logDirectory = new File("logs"); if (!logDirectory.isDirectory()) { logDirectory.mkdir(); } date = sdf.parse(sdf.format(new Date())).toString().replaceAll(" ", "") .replaceAll("[0-9]+:[0-9]+:[0-9]+CDT", ""); Properties properties = new Properties(); //creates configuration object properties.load(new FileInputStream(Main.configuration)); ftpServer = properties.getProperty("ftpServer"); ftpUser = properties.getProperty("ftpUser"); ftpPassword = properties.getProperty("ftpPassword"); client = new FTPClient(); client.connect(ftpServer); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); System.err.println("FTP server refused connection."); } if (client.login(ftpUser, ftpPassword)) { client.enterLocalPassiveMode(); String localFile = logDirectory.getAbsolutePath() + "/" + date + ".log"; String remoteFile = "/downloads/logs/" + date + ".log"; if (Main.debug) { System.out.println(localFile); System.out.println(remoteFile); } FileInputStream fis = new FileInputStream(new File(localFile)); if (client.storeFile(remoteFile, fis)) { System.out.println("File uploaded successfully."); } else { System.err.println("Uploading file failed.."); } fis.close(); client.logout(); client.disconnect(); } else { System.out.println("Could not authenticate with FTP server.."); client.logout(); } } catch (ParseException e) { System.err.println("Error parsing log file"); e.printStackTrace(); } catch (SocketException e) { System.err.println("some exception happened."); e.printStackTrace(); } catch (IOException e) { System.err.println("can not open or upload file."); e.printStackTrace(); } finally { if (client.isConnected()) { client.disconnect(); } } }
From source file:info.usbo.skypetwitter.Run.java
public static int load_file() { // TWITTER/*from ww w . ja v a2 s. co m*/ try { FileInputStream fis = new FileInputStream(work_dir + "\\twitter_ids.data"); ObjectInputStream ois = new ObjectInputStream(fis); twitter_ids = (ArrayList) ois.readObject(); ois.close(); fis.close(); } catch (IOException ioe) { ioe.printStackTrace(); return 0; } catch (ClassNotFoundException c) { System.out.println("Class not found"); c.printStackTrace(); return 0; } // VK try { FileInputStream fis = new FileInputStream(work_dir + "\\vk_ids.data"); ObjectInputStream ois = new ObjectInputStream(fis); vk_ids = (ArrayList) ois.readObject(); ois.close(); fis.close(); } catch (IOException ioe) { ioe.printStackTrace(); return 0; } catch (ClassNotFoundException c) { System.out.println("Class not found"); c.printStackTrace(); return 0; } return 1; }
From source file:Main.java
public static byte[] createMD5(FileInputStream is) throws IOException { MessageDigest digester = null; try {/*from www . j a v a 2s. com*/ digester = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[16 * 1024]; // 16k int length = 0; while ((length = is.read(buffer)) > 0) { digester.update(buffer, 0, length); } } catch (Exception e) { e.printStackTrace(); } finally { is.close(); } if (digester != null) { return digester.digest(); } return null; }
From source file:Main.java
public static String getFileAsString(File file) { try {/*w w w.java 2 s .c o m*/ FileInputStream fin = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(fin, "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { if (sb.length() > 0) { sb.append("\n"); } sb.append(line); } reader.close(); fin.close(); return sb.toString(); } catch (Exception e) { return null; } }