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:jeeves.utils.BinaryFile.java
/** * Copies an input stream (from a file) to an output stream *///from ww w . j a v a 2 s . co m public static void copy(InputStream in, OutputStream out, boolean closeInput, boolean closeOutput) throws IOException { try { if (in instanceof FileInputStream) { FileInputStream fin = (FileInputStream) in; WritableByteChannel outChannel; if (out instanceof FileOutputStream) { outChannel = ((FileOutputStream) out).getChannel(); } else { outChannel = Channels.newChannel(out); } fin.getChannel().transferTo(0, Long.MAX_VALUE, outChannel); } else { BufferedInputStream input = new BufferedInputStream(in); byte buffer[] = new byte[BUF_SIZE]; int nRead; while ((nRead = input.read(buffer)) > 0) out.write(buffer, 0, nRead); } } finally { if (closeInput) IOUtils.closeQuietly(in); if (closeOutput) IOUtils.closeQuietly(out); } }
From source file:fr.gouv.finances.dgfip.xemelios.importers.DefaultImporter.java
protected static String getFileXmlVersion(File f) throws IOException { String ret = null;/*w ww. j a v a2 s . c om*/ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); byte[] bb = new byte[255]; bis.read(bb); String buff = new String(bb); buff = buff.substring(buff.indexOf("<?")); buff = buff.substring(0, buff.indexOf("?>") + 2); String header = buff; int versionPos = header.indexOf("version="); if (versionPos >= 0) { String s = header.substring(versionPos + 8); if (s.startsWith("\"")) { ret = s.substring(1, s.indexOf('"', 1)); } else if (s.startsWith("'")) { ret = s.substring(1, s.indexOf('\'', 1)); } else { s = s.substring(0, s.length() - 2).trim(); if (s.indexOf(' ') > 0) { ret = s.substring(0, s.indexOf(' ')); } else { ret = s; } } } else { ret = (new InputStreamReader(new FileInputStream(f))).getEncoding(); } bis.close(); logger.debug("version=" + ret); return ret; }
From source file:gemlite.core.util.RSAUtils.java
public static byte[] readBytes(String filePath) { byte[] content = new byte[0]; File file = new File(filePath); if (file.exists() && file.isFile()) try {/*from w ww .ja v a 2 s . c o m*/ InputStream in = new FileInputStream(file); BufferedInputStream bufin = new BufferedInputStream(in); int buffSize = 1024; ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize); byte[] temp = new byte[buffSize]; int size = 0; while ((size = bufin.read(temp)) != -1) { out.write(temp, 0, size); } bufin.close(); content = out.toByteArray(); } catch (Exception e) { LogUtil.getCoreLog().warn("readKey file :{} failure :{}", filePath, e); } return content; }
From source file:fr.gouv.finances.dgfip.xemelios.importers.DefaultImporter.java
protected static String getFileEncoding(File f) throws IOException { String ret = "UTF-8"; try {//from www. ja v a2s . com BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); byte[] bb = new byte[255]; int read = bis.read(bb); if (read < 255) { logger.info("while reading " + f.getName() + " for getFileEncoding(), can only read " + read + " bytes."); } String buff = new String(bb); buff = buff.substring(buff.indexOf("<?")); buff = buff.substring(0, buff.indexOf("?>") + 2); String header = buff; int encodingPos = header.indexOf("encoding="); if (encodingPos >= 0) { String s = header.substring(encodingPos + 9); if (s.startsWith("\"")) { ret = s.substring(1, s.indexOf('"', 1)); } else if (s.startsWith("'")) { ret = s.substring(1, s.indexOf('\'', 1)); } else { s = s.substring(0, s.length() - 2).trim(); if (s.indexOf(' ') > 0) { ret = s.substring(0, s.indexOf(' ')); } else { ret = s; } } } else { ret = "UTF-8"; } bis.close(); } catch (Exception ex) { logger.error("getFileEncoding(File)" + ex); } logger.debug("encoding=" + ret); return ret; }
From source file:com.eviware.soapui.support.Tools.java
public static int copyFile(File source, File target, boolean overwrite) throws IOException { int bytes = 0; if (target.exists()) { if (overwrite) { target.delete();// w ww . j a v a2 s .co m } else { return -1; } } else { String path = target.getAbsolutePath(); int ix = path.lastIndexOf(File.separatorChar); if (ix != -1) { path = path.substring(0, ix); File pathFile = new File(path); if (!pathFile.exists()) { pathFile.mkdirs(); } } } BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target)); int read = in.read(copyBuffer); while (read != -1) { if (read > 0) { out.write(copyBuffer, 0, read); bytes += read; } read = in.read(copyBuffer); } in.close(); out.close(); return bytes; }
From source file:com.iStudy.Study.Renren.Util.java
/** * ???/*from ww w.ja va 2s . com*/ * * @param inputStream * @return * @throws IOException */ public static byte[] streamToByteArray(InputStream inputStream) { byte[] content = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); try { byte[] buffer = new byte[1024]; int length = 0; while ((length = bis.read(buffer)) != -1) { baos.write(buffer, 0, length); } content = baos.toByteArray(); if (content.length == 0) { content = null; } baos.close(); bis.close(); } catch (IOException e) { logger(e.getMessage()); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { logger(e.getMessage()); } } if (bis != null) { try { bis.close(); } catch (IOException e) { logger(e.getMessage()); } } } return content; }
From source file:com.edgenius.core.util.ZipFileUtil.java
private static void addEntry(ZipOutputStream zop, File entry, String entryName) throws IOException { if (StringUtils.isBlank(entryName)) return;//from w w w . j a v a 2s .c o m BufferedInputStream source = null; if (entry != null) { source = new BufferedInputStream(new FileInputStream(entry)); } else { //to make this as directory if (!entryName.endsWith("/")) entryName += "/"; } ZipEntry zipEntry = new ZipEntry(entryName); zop.putNextEntry(zipEntry); if (entry != null) { //transfer bytes from file to ZIP file byte[] data = new byte[BUFFER_SIZE]; int length; while ((length = source.read(data)) > 0) { zop.write(data, 0, length); } IOUtils.closeQuietly(source); } zop.closeEntry(); }
From source file:net.servicestack.client.Utils.java
public static byte[] readBytesToEnd(InputStream stream) throws IOException { ByteArrayBuffer bytes = new ByteArrayBuffer(1024); final BufferedInputStream bufferedStream = new BufferedInputStream(stream, 8192); try {/*from www . jav a 2 s .c o m*/ final byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = bufferedStream.read(buffer)) > 0) { bytes.append(buffer, 0, bytesRead); } return bytes.toByteArray(); } finally { bufferedStream.close(); } }
From source file:com.stimulus.archiva.language.NGramProfile.java
/** * Create a new Language profile from (preferably quite large) text file * /*from w w w . j a v a 2s . co m*/ * @param name is thename of profile * @param is is the stream to read * @param encoding is the encoding of stream */ public static NGramProfile create(String name, InputStream is, String encoding) { NGramProfile newProfile = new NGramProfile(name, ABSOLUTE_MIN_NGRAM_LENGTH, ABSOLUTE_MAX_NGRAM_LENGTH); BufferedInputStream bis = new BufferedInputStream(is); byte buffer[] = new byte[4096]; StringBuffer text = new StringBuffer(); int len; try { while ((len = bis.read(buffer)) != -1) { text.append(new String(buffer, 0, len, encoding)); } } catch (IOException e) { logger.error("failed to create NGRAM profile", e); } newProfile.analyze(text); return newProfile; }
From source file:com.zenika.dorm.maven.test.grinder.GrinderGenerateJson.java
/** * @param file to generate checksum * @param fileChecksum to put the generated checksum */// w ww . java 2s .c o m private static void generateChecksum(File file, File fileChecksum, String checksumType) { FileOutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null; FileInputStream inputStream = null; BufferedInputStream bufferedInputStream = null; try { MessageDigest digest = MessageDigest.getInstance(checksumType); outputStream = new FileOutputStream(fileChecksum); bufferedOutputStream = new BufferedOutputStream(outputStream); inputStream = new FileInputStream(file); bufferedInputStream = new BufferedInputStream(inputStream); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = bufferedInputStream.read(dataBytes)) != -1) { digest.update(dataBytes, 0, nread); } byte[] byteDigested = digest.digest(); StringBuffer sb = new StringBuffer(""); for (int i = 0; i < byteDigested.length; i++) { sb.append(Integer.toString((byteDigested[i] & 0xff) + 0x100, 16).substring(1)); } bufferedOutputStream.write(sb.toString().getBytes()); bufferedOutputStream.flush(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } catch (FileNotFoundException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } catch (IOException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } } if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } } if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { throw new RuntimeException("Unable to digest this file: " + file, e); } } } }