List of usage examples for java.io BufferedInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:com.piaoyou.util.FileUtil.java
public static void copyFile(File org, File des, boolean overwrite) throws IOException { if (!org.exists()) { throw new FileNotFoundException("Cannot find the source file: " + org.getAbsolutePath()); }/*from ww w . ja v a 2s . c o m*/ if (!org.canRead()) { throw new IOException("Cannot read the source file: " + org.getAbsolutePath()); } if (overwrite == false) { if (des.exists()) { return; } } else { if (des.exists()) { if (!des.canWrite()) { throw new IOException("Cannot write the destination file: " + des.getAbsolutePath()); } } else { if (!des.createNewFile()) { throw new IOException("Cannot write the destination file: " + des.getAbsolutePath()); } } } BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; byte[] block = new byte[1024]; try { inputStream = new BufferedInputStream(new FileInputStream(org)); outputStream = new BufferedOutputStream(new FileOutputStream(des)); while (true) { int readLength = inputStream.read(block); if (readLength == -1) break;// end of file outputStream.write(block, 0, readLength); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } } }
From source file:com.piaoyou.util.FileUtil.java
public static void copyFile(String srcFilename, String destFilename, boolean overwrite) throws IOException { File srcFile = new File(srcFilename); if (!srcFile.exists()) { throw new FileNotFoundException("Cannot find the source file: " + srcFile.getAbsolutePath()); }/*from w w w. j a v a 2s .com*/ if (!srcFile.canRead()) { throw new IOException("Cannot read the source file: " + srcFile.getAbsolutePath()); } File destFile = new File(destFilename); if (overwrite == false) { if (destFile.exists()) { return; } } else { if (destFile.exists()) { if (!destFile.canWrite()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } else { if (!destFile.createNewFile()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } } BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; byte[] block = new byte[1024]; try { inputStream = new BufferedInputStream(new FileInputStream(srcFile)); outputStream = new BufferedOutputStream(new FileOutputStream(destFile)); while (true) { int readLength = inputStream.read(block); if (readLength == -1) break;// end of file outputStream.write(block, 0, readLength); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } } }
From source file:com.athena.dolly.websocket.client.test.WebSocketClient.java
public void sendFile(File file) throws Exception { // Send 10 messages and wait for responses System.out.println("WebSocket Client sending file ->" + file.getAbsolutePath()); //for (int i = 0; i < 10; i++) { // ch.writeAndFlush(new TextWebSocketFrame("Message #" + i)); //}/*from w w w . j a v a 2s. co m*/ ch.writeAndFlush(new TextWebSocketFrame(file.getAbsolutePath())); // Binary File Data Send byte[] buf = new byte[65536]; int len = 0; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while ((len = bis.read(buf)) != -1) { ch.write(new BinaryWebSocketFrame(Unpooled.copiedBuffer(buf, 0, len))); } ch.flush(); System.out.println("File send succeed"); bis.close(); // Ping System.out.println("WebSocket Client sending ping"); ch.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 }))); }
From source file:com.naryx.tagfusion.expression.function.file.FileCopy.java
/** * Does a byte-4-byte copy/*w w w . jav a2s . com*/ * * @param vfsSrc * @param vfsDest * @throws Exception */ protected void copy(cfVFSData vfsSrc, cfVFSData vfsDest) throws Exception { vfsSrc.openInputStream(); vfsDest.openOutputStream(); BufferedInputStream ios = vfsSrc.getStreamReader(); byte b[] = new byte[16000]; int c; while ((c = ios.read(b)) != -1) { vfsDest.getStreamWriter().write(b, 0, c); } vfsSrc.close(); vfsDest.close(); }
From source file:com.jbrisbin.vpc.jobsched.exe.ExeMessageHandler.java
public ExeMessage handleMessage(final ExeMessage msg) throws Exception { log.debug("handling message: " + msg.toString()); List<String> args = msg.getArgs(); args.add(0, msg.getExe());/*from ww w . j av a2 s .c om*/ try { ProcessBuilder pb = new ProcessBuilder(args); pb.environment().putAll(msg.getEnv()); pb.directory(new File(msg.getDir())); pb.redirectErrorStream(true); Process p = pb.start(); BufferedInputStream stdout = new BufferedInputStream(p.getInputStream()); byte[] buff = new byte[4096]; for (int bytesRead = 0; bytesRead > -1; bytesRead = stdout.read(buff)) { msg.getOut().write(buff, 0, bytesRead); } p.waitFor(); } catch (Throwable t) { log.error(t.getMessage(), t); Object errmsg = t.getMessage(); if (null != errmsg) { msg.getOut().write(((String) errmsg).getBytes()); } } return msg; }
From source file:com.tesshu.subsonic.client.sample4_music_andmovie.BinaryDownloadApplication.java
@Bean protected CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { File tmpDirectory = new File(tmpPath); tmpDirectory.mkdir();//from w ww. j a v a 2s .c om Optional<SearchResult2> result2 = search2.getOf("e", null, null, null, null, 1, null, null); result2.ifPresent(result -> result.getSongs().forEach(song -> { download.download(song, (subject, inputStream) -> { File dir = new File(tmpPath + "/" + song.getPath().replaceAll("([^/]+?)?$", StringUtils.EMPTY)); dir.mkdirs(); File file = new File(tmpPath + "/" + song.getPath()); try { FileOutputStream fos = new FileOutputStream(file); BufferedInputStream reader = new BufferedInputStream(inputStream); byte buf[] = new byte[256]; int len; while ((len = reader.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); reader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } LOG.info(file.getAbsolutePath()); }, callback); })); tmpDirectory.deleteOnExit(); }; }
From source file:fr.univrouen.poste.services.ZipService.java
public void writeZip(List<PosteCandidature> posteCandidatures, OutputStream destStream) throws IOException, SQLException { ZipOutputStream out = new ZipOutputStream(destStream); for (PosteCandidature posteCandidature : posteCandidatures) { String folderName = posteCandidature.getPoste().getNumEmploi().concat("/"); folderName = folderName.concat(posteCandidature.getCandidat().getNom().concat("-")); folderName = folderName.concat(posteCandidature.getCandidat().getPrenom().concat("-")); folderName = folderName.concat(posteCandidature.getCandidat().getNumCandidat().concat("/")); for (PosteCandidatureFile posteCandidatureFile : posteCandidature.getCandidatureFiles()) { String fileName = posteCandidatureFile.getId().toString().concat("-") .concat(posteCandidatureFile.getFilename()); String folderFileName = folderName.concat(fileName); out.putNextEntry(new ZipEntry(folderFileName)); InputStream inputStream = posteCandidatureFile.getBigFile().getBinaryFile().getBinaryStream(); BufferedInputStream bufInputStream = new BufferedInputStream(inputStream, BUFFER); byte[] bytes = new byte[BUFFER]; int length; while ((length = bufInputStream.read(bytes)) >= 0) { out.write(bytes, 0, length); }/*from w w w.j av a 2s. c om*/ out.closeEntry(); } } out.close(); }
From source file:de.micromata.tpsb.doc.sources.JarSourceFileRepository.java
private String getContents(JarFile jarFile, JarEntry jarEntry) { try {/*from ww w . j a va 2 s.c o m*/ InputStream is = jarFile.getInputStream(jarEntry); BufferedInputStream stream = new BufferedInputStream(is); int bytesRead = 0; StringBuilder sb = new StringBuilder(); while ((bytesRead = stream.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead, Charset.forName(CharEncoding.UTF_8)); sb.append(chunk); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:io.milton.servlet.UrlResource.java
@Override public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException { InputStream in = null;/*from w w w .j a v a2 s .com*/ try { in = resource.openStream(); BufferedInputStream bin = new BufferedInputStream(in); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = bin.read(buffer))) { out.write(buffer, 0, n); } } finally { IOUtils.closeQuietly(in); } }
From source file:com.discovery.darchrow.io.FileUtil.java
/** * ? {@code byte[] bytes}./*from w w w.ja v a 2s .c om*/ * * @param file * file * @return {@link java.io.ByteArrayOutputStream#toByteArray()} * @see com.baozun.nebulaplus.io.FileUtil#getFileInputStream(File) * @see java.io.ByteArrayOutputStream#toByteArray() */ public static final byte[] toByteArray(File file) { InputStream inputStream = getFileInputStream(file); //Creates a BufferedInputStream and saves its argument, the input stream in, for later use. //An internal buffer array is created and stored in buf. BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); //Creates a new byte array output stream. //The buffer capacity is initially 32 bytes, though its size increases if necessary. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { byte[] bytes = new byte[IOConstants.DEFAULT_BUFFER_LENGTH]; int j; while ((j = bufferedInputStream.read(bytes)) != -1) { byteArrayOutputStream.write(bytes, 0, j); } byteArrayOutputStream.flush(); byte[] byteArray = byteArrayOutputStream.toByteArray(); return byteArray; } catch (IOException e) { throw new UncheckedIOException(e); } finally { // ???StreamClose.??Close IOUtils.closeQuietly(byteArrayOutputStream); IOUtils.closeQuietly(bufferedInputStream); } }