List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:de.uniwue.info2.main.CommandLineInterpreter.java
@SuppressWarnings("static-access") public static void main(String[] args) { /*-------------------------------------------------------- */ /*---------------SETTING TARGET LANGUAGE------------------ */ /*-------------------------------------------------------- */ LanguageFactory languageFactory = new LanguageFactory(); CommandLine line = null;//from w w w . j a va 2 s . c o m CommandLineParser parser = new BasicParser(); Options options = new Options(); // options to display in the help page Options options_short = new Options(); // add help option Option help_option = new Option(HELP_OPTION_SHORT, HELP_OPTION, false, HELP_DESCRIPTION); options.addOption(help_option); options_short.addOption(help_option); // add extended help option Option help2_option = new Option(HELP2_OPTION_SHORT, HELP2_OPTION, false, HELP2_DESCRIPTION); options.addOption(help2_option); options_short.addOption(help2_option); // add optional operations option options.addOption(new Option(OPTIONAL_OPTION_SHORT, OPTIONAL_OPTION, false, OPTIONAL_DESCRIPTION)); options.addOption(new Option(BIG_ENDIAN_OPTION_SHORT, BIG_ENDIAN_OPTION, false, BIG_ENDIAN_DESCRIPTION)); options.addOption( new Option(LITTLE_ENDIAN_OPTION_SHORT, LITTLE_ENDIAN_OPTION, false, LITTLE_ENDIAN_DESCRIPTION)); // add optional operations config option options.addOption(OptionBuilder.withLongOpt(OPTIONAL_FUNCTIONS_CONFIG_OPTION) .withArgName(OPTIONAL_FUNCTIONS_CONFIG_ARGUMENT) .withDescription(OPTIONAL_FUNCTIONS_CONFIG_DESCRIPTION).hasArg() .create(OPTIONAL_FUNCTIONS_CONFIG_SHORT)); // add dsl option Option dsl_option = OptionBuilder.withLongOpt(DSL_OPTION).withArgName(DSL_ARGUMENT) .withDescription(DSL_DESCRIPTION).hasArg().isRequired().create(DSL_OPTION_SHORT); options.addOption(dsl_option); options_short.addOption(dsl_option); // add output-folder option Option output_option = OptionBuilder.withLongOpt(OUTPUT_OPTION).isRequired().withArgName(OUTPUT_ARGUMENT) .withDescription(OUTPUT_DESCRIPTION).hasArg().create(OUTPUT_OPTION_SHORT); options.addOption(output_option); options_short.addOption(output_option); // count possible language-specifications short optionCounter = 1; // get all possible language-specifications from language-factory and iterate through them List<LanguageSpecification> lSpecs = languageFactory.getAvailableLanguageSpecifications_(); for (LanguageSpecification lSpec : lSpecs) { // get all possible unit-specifications for current language and iterate through them List<UnitTestLibrarySpecification> uSpecs = languageFactory .getAvailableUnitTestLibraries_(lSpec.getOptionName()); String languageDescriptionAll = LANGUAGE_SPECIFICATION + lSpec.getLanguageName(); String languageCounter = "s" + INDEX.format(optionCounter++); for (UnitTestLibrarySpecification uSpec : uSpecs) { // get all possible arithmetic-library-specifications for current language and iterate through // them List<ArithmeticLibrarySpecification> aSpecs = languageFactory .getAvailableArithmeticLibraries_(lSpec.getOptionName()); for (ArithmeticLibrarySpecification aSpec : aSpecs) { String languageDescription = "Generate unit-test for " + lSpec.getLanguageName() + "\n*[" + uSpec.getLibraryName() + " - " + uSpec.getVersion() + "]\n*[" + aSpec.getLibraryName() + " - " + aSpec.getVersion() + "]"; // if there is more than one option, generate suitable option-names and add them all to // commandline options if (uSpecs.size() > 1 || aSpecs.size() > 1) { options.addOption(OptionBuilder .withLongOpt(lSpec.getOptionName() + "_" + uSpec.getOptionName() + "_" + aSpec.getOptionName()) .withDescription(languageDescription).hasArg(false) .create("s" + INDEX.format(optionCounter++))); } else { // if there is only one option, use language-name as option-name languageDescriptionAll = languageDescription; } } // add specifications to options options.addOption(OptionBuilder.withLongOpt(lSpec.getOptionName()) .withDescription(languageDescriptionAll).hasArg(false).create(languageCounter)); } } /*-------------------------------------------------------- */ /*-------------------PARSE USER INPUT--------------------- */ /*-------------------------------------------------------- */ try { // manual search for help-arguments for (String arg : args) { arg = arg.trim().replace("-", ""); if (arg.equals(HELP_OPTION_SHORT) || arg.equals(HELP_OPTION)) { printHelp(options_short); return; } if (arg.equals(HELP2_OPTION_SHORT) || arg.equals(HELP2_OPTION)) { printExtendedHelp(options); return; } } // parse arguments line = parser.parse(options, args); File dsl_file = null; File output_folder = null; File optional_config = null; Properties optional_operations = null; Boolean optional = false; Boolean little_endian = null; ArrayList<String> optional_exceptions = new ArrayList<String>(); // if help-option found print help if (line.hasOption(HELP2_OPTION_SHORT) || args.length == 0) { printExtendedHelp(options); return; } // if help-option found print help if (line.hasOption(HELP_OPTION_SHORT) || args.length == 0) { System.out.println("\n"); printHelp(options_short); return; } if (line.hasOption(OPTIONAL_OPTION_SHORT)) { optional = true; } if (line.hasOption(LITTLE_ENDIAN_OPTION)) { little_endian = true; } if (line.hasOption(BIG_ENDIAN_OPTION)) { little_endian = false; } // if dsl-option found, check if file exists and is readable // print help if error occurs if (line.hasOption(DSL_OPTION_SHORT)) { dsl_file = new File(line.getOptionValue(DSL_OPTION_SHORT)); if (!dsl_file.exists()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - DSL-file doesn't exist:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } else if (dsl_file.isDirectory()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - DSL-file is a directory:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } else if (!dsl_file.canRead()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Need read-permission for DSL-file:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } } // if output-option found, check if folder exists and if write-permission was granted // print help if error occurs if (line.hasOption(OUTPUT_OPTION_SHORT)) { output_folder = new File(line.getOptionValue(OUTPUT_OPTION_SHORT)); if (!output_folder.exists()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Output-folder doesn't exist:\n" + output_folder + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } else if (!output_folder.isDirectory()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Output-folder is not a directory:\n" + output_folder + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } else if (!output_folder.canWrite() || !output_folder.canRead()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Missing permissions for output-folder:\n" + output_folder + "\n" + SEPERATOR + "\n"); printHelp(options_short); return; } } if (line.hasOption(OPTIONAL_FUNCTIONS_CONFIG_SHORT)) { optional_config = new File(line.getOptionValue(OPTIONAL_FUNCTIONS_CONFIG_OPTION)); if (!dsl_file.exists()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - config-file doesn't exist:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printExtendedHelp(options); return; } else if (dsl_file.isDirectory()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - config-file is a directory:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printExtendedHelp(options); return; } else if (!dsl_file.canRead()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Need read-permission for config-file:\n" + dsl_file + "\n" + SEPERATOR + "\n"); printExtendedHelp(options); return; } } if (optional_config != null) { optional_operations = new Properties(); BufferedInputStream stream = new BufferedInputStream(new FileInputStream(optional_config)); optional_operations.load(stream); stream.close(); String optional_prop = optional_operations.getProperty("GENERATE_OPTIONAL"); if (optional_prop != null) { if (optional_prop.trim().toLowerCase().equals("true")) { optional = true; } else if (optional_prop.trim().toLowerCase().equals("false")) { optional = false; } else if (!optional_prop.trim().isEmpty()) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Syntax incorrect in config-file:\nUse \"true\" or \"false\" for \"GENERATE_OPTIONAL\"\n" + SEPERATOR + "\n"); printExtendedHelp(options); return; } } String exceptions = optional_operations.getProperty("EXCLUSIONS"); if (exceptions != null) { for (String exc : optional_operations.getProperty("EXCLUSIONS").split(";")) { optional_exceptions.add(exc.trim()); } } } /*-------------------------------------------------------- */ /*-------------------START GENERATING--------------------- */ /*-------------------------------------------------------- */ // instantiate generator for unit-tests TestcaseGenerator mainGenerator = new TestcaseGenerator(dsl_file, output_folder); boolean overrideDefaultSpecs = false; // check if user input contains a language-specifications // if user specified language, set overrideDefaultSpecs to true, so that only given specifications // are used for (int i = 1; i <= optionCounter; i++) { String opt = "s" + INDEX.format(i); if (line.hasOption(opt)) { LanguageSpecification targetSpecification = languageFactory .getLanguageSpecification(options.getOption(opt).getLongOpt()); String output = (GENERATING_DIALOG + targetSpecification.getLanguageName()); // finally generate unit-test for current language-specification boolean successful = mainGenerator.generateUnitTest(targetSpecification, optional, optional_exceptions, little_endian); if (successful) { System.out.println(output + "\n--> Successfully generated."); } else { System.err.println(output + "\n--> ERROR - see logfile"); } overrideDefaultSpecs = true; } } // skip, if user already defined one language-specification // if user did not define language-specification, generate unit-tests for all // possible language-specifications (default) if (!overrideDefaultSpecs) { for (int i = 0; i < lSpecs.size(); i++) { LanguageSpecification specification = languageFactory .getLanguageSpecification(lSpecs.get(i).getOptionName()); String output = INDEX.format(i + 1) + " - " + GENERATING_DIALOG + specification.getLanguageName(); // finally generate unit-test for current language-specification boolean successful = mainGenerator.generateUnitTest(specification, optional, optional_exceptions, little_endian); if (successful) { System.out.println(output + "\n--> Successfully generated."); } else { System.err.println(output + "\n--> ERROR - see logfile"); } } } } catch (ParseException | IOException p) { System.err.println("\n" + SEPERATOR + "\n" + "ERROR - WRONG ARGUMENTS:\n" + p.getMessage() + "\n" + SEPERATOR + "\n"); printHelp(options_short); System.out.println("\n"); } }
From source file:ZipImploder.java
/** * Main command line entry point./* ww w. j a v a 2 s.com*/ * * @param args */ public static void main(final String[] args) { if (args.length == 0) { printHelp(); System.exit(0); } String zipName = null; String jarName = null; String manName = null; String sourceDir = null; String leadDir = null; boolean jarActive = false, manActive = false, zipActive = false, sourceDirActive = false, leadDirActive = false; boolean verbose = false; boolean noDirs = false; // process arguments for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.charAt(0) == '-') { // switch arg = arg.substring(1); if (arg.equalsIgnoreCase("jar")) { jarActive = true; manActive = false; zipActive = false; sourceDirActive = false; leadDirActive = false; } else if (arg.equalsIgnoreCase("manifest")) { jarActive = false; manActive = true; zipActive = false; sourceDirActive = false; leadDirActive = false; } else if (arg.equalsIgnoreCase("zip")) { zipActive = true; manActive = false; jarActive = false; sourceDirActive = false; leadDirActive = false; } else if (arg.equalsIgnoreCase("dir")) { jarActive = false; manActive = false; zipActive = false; sourceDirActive = true; leadDirActive = false; } else if (arg.equalsIgnoreCase("lead")) { jarActive = false; manActive = false; zipActive = false; sourceDirActive = false; leadDirActive = true; } else if (arg.equalsIgnoreCase("noDirs")) { noDirs = true; jarActive = false; manActive = false; zipActive = false; sourceDirActive = false; leadDirActive = false; } else if (arg.equalsIgnoreCase("verbose")) { verbose = true; jarActive = false; manActive = false; zipActive = false; sourceDirActive = false; leadDirActive = false; } else { reportError("Invalid switch - " + arg); } } else { if (jarActive) { if (jarName != null) { reportError("Duplicate value - " + arg); } jarName = arg; } else if (manActive) { if (manName != null) { reportError("Duplicate value - " + arg); } manName = arg; } else if (zipActive) { if (zipName != null) { reportError("Duplicate value - " + arg); } zipName = arg; } else if (sourceDirActive) { if (sourceDir != null) { reportError("Duplicate value - " + arg); } sourceDir = arg; } else if (leadDirActive) { if (leadDir != null) { reportError("Duplicate value - " + arg); } leadDir = arg; } else { reportError("Too many parameters - " + arg); } } } if (sourceDir == null || (zipName == null && jarName == null)) { reportError("Missing parameters"); } if (manName != null && zipName != null) { reportError("Manifests not supported on ZIP files"); } if (leadDir == null) { leadDir = new File(sourceDir).getAbsolutePath().replace('\\', '/') + '/'; } if (verbose) { System.out.println("Effective command: " + ZipImploder.class.getName() + (jarName != null ? " -jar " + jarName + (manName != null ? " -manifest " + manName : "") : "") + (zipName != null ? " -zip " + zipName : "") + " -dir " + sourceDir + " -lead " + leadDir + (noDirs ? " -noDirs" : "") + (verbose ? " -verbose" : "")); } try { ZipImploder zi = new ZipImploder(verbose); if (leadDir != null) { zi.setBaseDir(leadDir); } if (manName != null) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(manName)); try { zi.setManifest(new Manifest(bis)); } finally { bis.close(); } } zi.setIncludeDirs(!noDirs); zi.process(zipName, jarName, sourceDir); if (verbose) { System.out.println("\nDone Directories=" + zi.getDirCount() + " Files=" + zi.getFileCount()); } } catch (IOException ioe) { System.err.println("Exception - " + ioe.getMessage()); // ioe.printStackTrace(); // *** debug *** System.exit(2); } }
From source file:Main.java
public static void close(BufferedInputStream inputStream) { try {//w ww.j a v a2 s . c om inputStream.close(); inputStream = null; } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static Bitmap downloadImage(String urlStr) throws IOException { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); Bitmap result = BitmapFactory.decodeStream(bis); bis.close(); return result; }
From source file:Main.java
/** * @param url// w ww.j av a 2 s.c o m * @return */ public static Bitmap getBitMapFromURL(URL url) { Bitmap mBitmap = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); conn.getContentLength(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); mBitmap = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (Exception e) { Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage()); e.printStackTrace(); } return mBitmap; }
From source file:Main.java
/** Reads the specified file into a string * @param filePath the path to the file that should be read * @return the content of the specified file as a string or an empty string if the file does not exist * @throws IOException *//*from w w w. jav a2 s.co m*/ public static String readFileAsString(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { return ""; } byte[] buffer = new byte[(int) new File(filePath).length()]; BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath)); bufferedInputStream.read(buffer); bufferedInputStream.close(); return new String(buffer); }
From source file:Main.java
/** * * @param f//from w w w .ja va 2 s . c o m * @throws Exception */ private static X509Certificate readCertificate(File f) throws CertificateException, IOException { CertificateFactory cf = CertificateFactory.getInstance("X.509"); // Use BufferedInputStream (which supports mark and reset) so that each // generateCertificate call consumes one certificate. BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); X509Certificate cert = (X509Certificate) cf.generateCertificate(in); in.close(); return cert; }
From source file:com.phonegap.plugins.xapkreader.XAPKReader.java
private static byte[] readFile(Context ctx, String filename) throws IOException { // Get APKExpensionFile ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, XAPKReader.mainVersion, XAPKReader.patchVersion);/*w w w . ja va 2 s. co m*/ if (null == expansionFile) { Log.e("XAPKReader", "APKExpansionFile not found."); return null; } // Find file in ExpansionFile String fileName = Helpers.getExpansionAPKFileName(ctx, true, XAPKReader.patchVersion); fileName = fileName.substring(0, fileName.lastIndexOf(".")); AssetFileDescriptor file = expansionFile.getAssetFileDescriptor(fileName + "/" + filename); if (null == file) { Log.e("XAPKReader", "File not found (" + filename + ")."); return null; } // Read file int size = (int) file.getLength(); byte[] data = new byte[size]; BufferedInputStream buf = new BufferedInputStream(file.createInputStream()); buf.read(data, 0, data.length); buf.close(); return data; }
From source file:Util.java
public static byte[] readFile(File file) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int bytes = (int) file.length(); byte[] buffer = new byte[bytes]; int readBytes = bis.read(buffer); bis.close(); return buffer; }
From source file:com.amazonaws.services.simpleworkflow.flow.examples.deployment.DeploymentInitiator.java
public static String loadFile(String fileName) throws IOException { int length = (int) new File(fileName).length(); byte[] buffer = new byte[length]; BufferedInputStream is = null; try {/*from www. jav a 2 s. com*/ is = new BufferedInputStream(new FileInputStream(fileName)); is.read(buffer); } finally { if (is != null) { is.close(); } } return new String(buffer); }