List of usage examples for java.io BufferedInputStream read
public synchronized int read(byte b[], int off, int len) throws IOException
From source file:net.itransformers.idiscover.v2.core.listeners.GraphmlFileLogGroovyDiscoveryListenerTestCase.java
String readInputStreamToString(InputStream inputStream) throws IOException { BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; int len;/*w w w . ja v a 2s . com*/ while ((len = bis.read(buffer, 0, 256)) != -1) { bos.write(buffer, 0, len); } return bos.toString().replace("\r", "");// for tests under windows }
From source file:net.sf.jmimemagic.detectors.TextFileDetector.java
/** * DOCUMENT ME!//from w w w.j av a2 s. co m * * @param file DOCUMENT ME! * @param offset DOCUMENT ME! * @param length DOCUMENT ME! * @param bitmask DOCUMENT ME! * @param comparator DOCUMENT ME! * @param mimeType DOCUMENT ME! * @param params DOCUMENT ME! * * @return DOCUMENT ME! */ public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, Map params) { log.debug("processing file data"); try { BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[length]; int n = is.read(b, offset, length); if (n > 0) { return process(b, offset, length, bitmask, comparator, mimeType, params); } } catch (IOException e) { log.error("TextFileDetector: error", e); } return null; }
From source file:Main.java
public static String unzip(String filename) throws IOException { BufferedOutputStream origin = null; String path = null;/*from w w w .j av a2 s . c o m*/ Integer BUFFER_SIZE = 20480; if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { File flockedFilesFolder = new File( Environment.getExternalStorageDirectory() + File.separator + "FlockLoad"); System.out.println("FlockedFileDir: " + flockedFilesFolder); String compressedFile = flockedFilesFolder.toString() + "/" + filename; System.out.println("compressed File name:" + compressedFile); //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip"; File purgeFile = new File(compressedFile); try { ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile)); BufferedInputStream bis = new BufferedInputStream(zin); try { ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { byte data[] = new byte[BUFFER_SIZE]; path = flockedFilesFolder.toString() + "/" + ze.getName(); System.out.println("path is:" + path); if (ze.isDirectory()) { File unzipFile = new File(path); if (!unzipFile.isDirectory()) { unzipFile.mkdirs(); } } else { FileOutputStream fout = new FileOutputStream(path, false); origin = new BufferedOutputStream(fout, BUFFER_SIZE); try { /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) { origin.write(c); } */ int count; while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) { origin.write(data, 0, count); } zin.closeEntry(); } finally { origin.close(); fout.close(); } } } } finally { bis.close(); zin.close(); purgeFile.delete(); } } catch (Exception e) { e.printStackTrace(); } return path; } return null; }
From source file:org.dataone.proto.trove.mn.rest.v1.ExceptionController.java
public void writeToResponse(InputStream in, OutputStream out) throws IOException { try {//from w ww .j a va 2 s.c o m BufferedInputStream f = new BufferedInputStream(in); byte[] barray = new byte[SIZE]; int nRead; while ((nRead = f.read(barray, 0, SIZE)) != -1) { String printit = new String(Arrays.copyOf(barray, nRead)); log.info(printit); out.write(barray, 0, nRead); } } finally { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } }
From source file:edu.usf.cutr.manager.IOManagerImpl.java
@Override public void downloadFile(String url, String path) throws MalformedURLException, IOException { BufferedInputStream in = null; FileOutputStream fout = null; try {//w w w. ja v a2 s .c om in = new BufferedInputStream(new URL(url).openStream()); fout = new FileOutputStream(path); final byte data[] = new byte[1024]; int count; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } }
From source file:org.dataone.proto.trove.mn.service.v1.impl.MNStorageImpl.java
@Override public Identifier create(Identifier pid, InputStream object, SystemMetadata sysmeta) throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidRequest { try {// www.j a va 2s. co m File systemMetadata = new File(dataoneCacheDirectory + File.separator + "meta" + File.separator + EncodingUtilities.encodeUrlPathSegment(pid.getValue())); TypeMarshaller.marshalTypeToFile(sysmeta, systemMetadata.getAbsolutePath()); if (systemMetadata.exists()) { systemMetadata.setLastModified(sysmeta.getDateSysMetadataModified().getTime()); } else { throw new ServiceFailure("1190", "SystemMetadata not found on FileSystem after create"); } File objectFile = new File(dataoneCacheDirectory + File.separator + "object" + File.separator + EncodingUtilities.encodeUrlPathSegment(pid.getValue())); if (!objectFile.exists() && objectFile.createNewFile()) { objectFile.setReadable(true); objectFile.setWritable(true); objectFile.setExecutable(false); } if (object != null) { FileOutputStream objectFileOutputStream = new FileOutputStream(objectFile); BufferedInputStream inputStream = new BufferedInputStream(object); byte[] barray = new byte[SIZE]; int nRead = 0; while ((nRead = inputStream.read(barray, 0, SIZE)) != -1) { objectFileOutputStream.write(barray, 0, nRead); } objectFileOutputStream.flush(); objectFileOutputStream.close(); inputStream.close(); } } catch (FileNotFoundException ex) { throw new ServiceFailure("1190", ex.getCause().toString() + ":" + ex.getMessage()); } catch (IOException ex) { throw new ServiceFailure("1190", ex.getMessage()); } catch (JiBXException ex) { throw new InvalidSystemMetadata("1180", ex.getMessage()); } return pid; }
From source file:com.testmax.util.FileDownLoader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }//w ww .j a v a2 s .c o m URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); String file_path = downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""); FileWriter downloadedFile = new FileWriter(file_path, true); try { int status = client.executeMethod(getRequest); WmLog.getCoreLogger().info("HTTP Status {} when getting '{}'" + status + downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.write(bytes); } downloadedFile.close(); in.close(); WmLog.getCoreLogger().info("File downloaded to '{}'" + file_path); } catch (Exception Ex) { WmLog.getCoreLogger().error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return file_path; }
From source file:azkaban.executor.ExecutionLogsDao.java
private void uploadLogFile(final DatabaseTransOperator transOperator, final int execId, final String name, final int attempt, final File[] files, final EncodingType encType) throws SQLException { // 50K buffer... if logs are greater than this, we chunk. // However, we better prevent large log files from being uploaded somehow final byte[] buffer = new byte[50 * 1024]; int pos = 0;// w w w. jav a2s .c om int length = buffer.length; int startByte = 0; try { for (int i = 0; i < files.length; ++i) { final File file = files[i]; final BufferedInputStream bufferedStream = new BufferedInputStream(new FileInputStream(file)); try { int size = bufferedStream.read(buffer, pos, length); while (size >= 0) { if (pos + size == buffer.length) { // Flush here. uploadLogPart(transOperator, execId, name, attempt, startByte, startByte + buffer.length, encType, buffer, buffer.length); pos = 0; length = buffer.length; startByte += buffer.length; } else { // Usually end of file. pos += size; length = buffer.length - pos; } size = bufferedStream.read(buffer, pos, length); } } finally { IOUtils.closeQuietly(bufferedStream); } } // Final commit of buffer. if (pos > 0) { uploadLogPart(transOperator, execId, name, attempt, startByte, startByte + pos, encType, buffer, pos); } } catch (final SQLException e) { logger.error("Error writing log part.", e); throw new SQLException("Error writing log part", e); } catch (final IOException e) { logger.error("Error chunking.", e); throw new SQLException("Error chunking", e); } }
From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }/*from ww w .j a v a2 s. c o m*/ URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.RFC_2965); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); FileHandler downloadedFile = new FileHandler( downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true); try { int status = client.executeMethod(getRequest); LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.getWritableFileOutputStream().write(block, 0, bytes); } downloadedFile.close(); in.close(); LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile()); } catch (Exception Ex) { LOGGER.error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return downloadedFile.getAbsoluteFile(); }
From source file:net.sf.jmimemagic.detectors.OfficeXFileDetector.java
/** * DOCUMENT ME!//w w w . j a va2 s.c o m * * @param file DOCUMENT ME! * @param offset DOCUMENT ME! * @param length DOCUMENT ME! * @param bitmask DOCUMENT ME! * @param comparator DOCUMENT ME! * @param mimeType DOCUMENT ME! * @param params DOCUMENT ME! * * @return DOCUMENT ME! */ public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, Map params) { log.debug("processing file data"); BufferedInputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[length]; int n = is.read(b, offset, length); if (n > 0) { return process(b, offset, length, bitmask, comparator, mimeType, params); } } catch (IOException e) { log.error("error opening stream", e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { log.error("error closing stream"); } } } return null; }