List of usage examples for java.io BufferedInputStream BufferedInputStream
public BufferedInputStream(InputStream in)
BufferedInputStream
and saves its argument, the input stream in
, for later use. From source file:eu.learnpad.simulator.mon.utils.Manager.java
@SuppressWarnings("deprecation") public static Properties Read(String fileName) { Properties readedProps = new Properties(); File file = new File(fileName); FileInputStream fis = null;//from w w w . j ava 2 s . com BufferedInputStream bis = null; DataInputStream dis = null; try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. String property = ""; String key = ""; String value = ""; while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. property = dis.readLine().trim(); if (property.length() > 0) { key = property.substring(0, property.indexOf("=")); value = property.substring(property.indexOf("=") + 1, property.length()); readedProps.put(key.trim(), value.trim()); } } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } return readedProps; }
From source file:org.jetbrains.webdemo.examples.ExamplesLoader.java
private static ExamplesFolder loadFolder(String path, String url, List<ObjectNode> parentCommonFiles, ExamplesFolder parentFolder) {/* w w w . java 2 s . c om*/ File manifestFile = new File(path + File.separator + "manifest.json"); try (BufferedInputStream reader = new BufferedInputStream(new FileInputStream(manifestFile))) { ObjectNode manifest = (ObjectNode) JsonUtils.getObjectMapper().readTree(reader); String name = new File(path).getName(); List<ObjectNode> commonFiles = new ArrayList<>(); commonFiles.addAll(parentCommonFiles); boolean taskFolder = manifest.has("taskFolder") ? manifest.get("taskFolder").asBoolean() : false; List<LevelInfo> levels = null; if (manifest.has("levels")) { levels = new ArrayList<>(); for (JsonNode level : manifest.get("levels")) { levels.add(new LevelInfo(level.get("projectsNeeded").asInt(), level.get("color").asText())); } } if (parentFolder != null && parentFolder.isTaskFolder()) taskFolder = true; ExamplesFolder folder = new ExamplesFolder(name, url, taskFolder, levels); if (manifest.has("files")) { for (JsonNode node : manifest.get("files")) { ObjectNode fileManifest = (ObjectNode) node; fileManifest.put("path", path + File.separator + fileManifest.get("filename").asText()); commonFiles.add(fileManifest); } } if (manifest.has("folders")) { for (JsonNode node : manifest.get("folders")) { String folderName = node.textValue(); folder.addChildFolder(loadFolder(path + File.separator + folderName, url + folderName + "/", commonFiles, folder)); } } if (manifest.has("examples")) { for (JsonNode node : manifest.get("examples")) { String projectName = node.textValue(); String projectPath = path + File.separator + projectName; folder.addExample(loadExample(projectPath, url, ApplicationSettings.LOAD_TEST_VERSION_OF_EXAMPLES, commonFiles)); } } return folder; } catch (IOException e) { System.err.println("Can't load folder: " + e.toString()); return null; } }
From source file:Main.java
/** Downloads a file from the specified URL and stores the file to the specified location * @param fileUrl the URL from which the file should be downloaded * @param storageLocation the location to which the downloaded file should be stored. If the file exists, it will * be overridden! /*from www .ja v a 2 s . c o m*/ * @throws IOException */ public static void downloadFileFromWebserver(String fileUrl, String storageLocation) throws IOException { URL url = new URL(fileUrl); File file = new File(storageLocation); URLConnection urlConnection = url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); FileOutputStream fileOutputStream = new FileOutputStream(file); byte[] buffer = new byte[1024]; int bytesInBuffer = 0; while ((bytesInBuffer = bufferedInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesInBuffer); } fileOutputStream.close(); }
From source file:com.mebigfatguy.polycasso.URLFetcher.java
/** * retrieve arbitrary data found at a specific url * - either http or file urls/* ww w . j a v a 2s.c o m*/ * for http requests, sets the user-agent to mozilla to avoid * sites being cranky about a java sniffer * * @param url the url to retrieve * @param proxyHost the host to use for the proxy * @param proxyPort the port to use for the proxy * @return a byte array of the content * * @throws IOException the site fails to respond */ public static byte[] fetchURLData(String url, String proxyHost, int proxyPort) throws IOException { HttpURLConnection con = null; InputStream is = null; try { URL u = new URL(url); if (url.startsWith("file://")) { is = new BufferedInputStream(u.openStream()); } else { Proxy proxy; if (proxyHost != null) { proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } else { proxy = Proxy.NO_PROXY; } con = (HttpURLConnection) u.openConnection(proxy); con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6"); con.addRequestProperty("Accept-Charset", "UTF-8"); con.addRequestProperty("Accept-Language", "en-US,en"); con.addRequestProperty("Accept", "text/html,image/*"); con.setDoInput(true); con.setDoOutput(false); con.connect(); is = new BufferedInputStream(con.getInputStream()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); return baos.toByteArray(); } finally { IOUtils.closeQuietly(is); if (con != null) { con.disconnect(); } } }
From source file:com.jcalvopinam.core.Zipping.java
private static void splitAndZipFile(File inputFile, int bufferSize, CustomFile customFile) throws IOException { int counter = 1; byte[] bufferPart; byte[] buffer = new byte[bufferSize]; File newFile;// w ww . ja va2s .com FileInputStream fileInputStream; ZipOutputStream out; String temporalName; String outputFileName; try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile))) { int tmp; System.out.println("Please wait while the file is split:"); while ((tmp = bis.read(buffer)) > 0) { temporalName = String.format("%s.%03d", customFile.getFileName(), counter); newFile = new File(inputFile.getParent(), temporalName); try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) { fileOutputStream.write(buffer, 0, tmp); } fileInputStream = new FileInputStream(newFile);//file001.zip outputFileName = String.format("%s%s_%03d%s", customFile.getPath(), customFile.getFileName(), counter, Extensions.ZIP.getExtension()); out = new ZipOutputStream(new FileOutputStream(outputFileName)); out.putNextEntry(new ZipEntry(customFile.getFileNameExtension())); bufferPart = new byte[CustomFile.BYTE_SIZE]; int count; while ((count = fileInputStream.read(bufferPart)) > 0) { out.write(bufferPart, 0, count); System.out.print("."); } counter++; fileInputStream.close(); out.close(); FileUtils.deleteQuietly(newFile); } } System.out.println("\nEnded process!"); }
From source file:ca.simplegames.micro.utils.IO.java
/** * Copy the contents of the given input File to the given output File. * * @param in the file to copy from// w w w .j a v a2s.com * @param out the file to copy to * @return the number of bytes copied * @throws java.io.IOException in case of I/O errors */ public static long copy(File in, File out) throws IOException { return copy(new BufferedInputStream(new FileInputStream(in)), new BufferedOutputStream(new FileOutputStream(out))); }
From source file:com.cyberway.issue.util.SurtPrefixSet.java
/** * Allow class to be used as a command-line tool for converting * URL lists (or naked host or host/path fragments implied * to be HTTP URLs) to implied SURT prefix form. * //w ww . j a va 2s.c o m * Read from stdin or first file argument. Writes to stdout. * * @param args cmd-line arguments: may include input file * @throws IOException */ public static void main(String[] args) throws IOException { InputStream in = args.length > 0 ? new BufferedInputStream(new FileInputStream(args[0])) : System.in; PrintStream out = args.length > 1 ? new PrintStream(new BufferedOutputStream(new FileOutputStream(args[1]))) : System.out; BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { if (line.indexOf("#") > 0) line = line.substring(0, line.indexOf("#")); line = line.trim(); if (line.length() == 0) continue; out.println(prefixFromPlain(line)); } br.close(); out.close(); }
From source file:FileCopyUtils.java
/** * Copy the contents of the given input File to the given output File. * @param in the file to copy from//from w ww . j a va 2s. c o m * @param out the file to copy to * @return the number of bytes copied * @throws IOException in case of I/O errors */ public static int copy(File in, File out) throws IOException { return copy(new BufferedInputStream(new FileInputStream(in)), new BufferedOutputStream(new FileOutputStream(out))); }
From source file:Main.java
public static String largeFileSha1(File file) { InputStream inputStream = null; try {/*from w ww . j av a 2s .com*/ MessageDigest gsha1 = MessageDigest.getInstance("SHA1"); MessageDigest sha1 = MessageDigest.getInstance("SHA1"); inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int nRead = 0; int block = 0; int count = 0; final int BLOCK_SIZE = 4 * 1024 * 1024; while ((nRead = inputStream.read(buffer)) != -1) { count += nRead; sha1.update(buffer, 0, nRead); if (BLOCK_SIZE == count) { gsha1.update(sha1.digest()); sha1 = MessageDigest.getInstance("SHA1"); block++; count = 0; } } if (count != 0) { gsha1.update(sha1.digest()); block++; } byte[] digest = gsha1.digest(); byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(block).array(); byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, blockBytes.length) .put(digest, 0, digest.length).array(); return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
/** * Do an HTTP POST and return the data as a byte array. *//* w w w . j a va 2 s . co m*/ public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties) throws MalformedURLException, IOException { HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(data != null); urlConnection.setDoInput(true); if (requestProperties != null) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue()); } } urlConnection.connect(); if (data != null) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(data); out.close(); } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); return convertInputStreamToByteArray(in); } catch (IOException e) { String details; if (urlConnection != null) { details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage() + ")"; } else { details = ""; } Log.e("ExoplayerUtil", "executePost: Request failed" + details, e); throw e; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }