/** * write. * * @param is InputStream instance. * @param os OutputStream instance. * @param bufferSize buffer size. * @return count. * @throws IOException */ public static long write(InputStream is, OutputStream os, int bufferSize) throws IOException { int read; long total = 0; byte[] buff = new byte[bufferSize]; while (is.available() > 0) { read = is.read(buff, 0, buff.length); if (read > 0) { os.write(buff, 0, read); total += read; } } return total; }
File f = new File(getCacheDir()+"/m1.map"); if (!f.exists()) try { InputStream is = getAssets().open("m1.map"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); FileOutputStream fos = new FileOutputStream(f); fos.write(buffer); fos.close(); } catch (Exception e) { throw new RuntimeException(e); } mapView.setMapFile(f.getPath());
private void process(OutputStream out) throws IOException { if (template.available() == 0) { throw new IOException("Template already processed"); } try (InputStream in = new BufferedInputStream(template)) { ParserState state = new ParserState(); while (true) { int ch = in.read(); if (ch == -1) { break; } String str = process(state, (char) ch); if (str != null) { out.write(str.getBytes()); } else if (!state.skip) { out.write(ch); } } } }
public static String loadConfig(String name) { // 先取本地文件,再取类路径 File filePath = new File(".." + File.separator + Constant.CONF_DIR + File.separator + name); if (!filePath.exists()) { URL url = MappingConfigsLoader.class.getClassLoader().getResource(""); if (url != null) { filePath = new File(url.getPath() + name); } } if (filePath.exists()) { String fileName = filePath.getName(); if (!fileName.endsWith(".yml")) { return null; } try (InputStream in = new FileInputStream(filePath)) { byte[] bytes = new byte[in.available()]; in.read(bytes); return new String(bytes, StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException("Read mapping config: " + filePath.getAbsolutePath() + " error. ", e); } } return null; } }
/** * Reads all available bytes from InputStream as a byte array. * Uses <code>in.available()</code> to determine the size of input stream. * This is the fastest method for reading input stream to byte array, but * depends on stream implementation of <code>available()</code>. * Buffered internally. */ public static byte[] readAvailableBytes(InputStream in) throws IOException { int l = in.available(); byte[] byteArray = new byte[l]; int i = 0, j; while ((i < l) && (j = in.read(byteArray, i, l - i)) >= 0) { i +=j; } if (i < l) { throw new IOException("Failed to completely read input stream"); } return byteArray; }
throws IOException { if (is == null) { throw new IOException("Class not found"); byte[] b = new byte[is.available()]; int len = 0; while (true) { int n = is.read(b, len, b.length - len); if (n == -1) { if (len < b.length) { int last = is.read(); if (last < 0) { return b; is.close();
/** Returns the length in bytes of this file, or 0 if this file is a directory, does not exist, or the size cannot otherwise be * determined. */ public long length () { if (type == FileType.Classpath || !file.exists()) { InputStream input = read(); try { return input.available(); } catch (Exception ignored) { } finally { try { input.close(); } catch (IOException ignored) { } } return 0; } return file().length(); }
@Override public int available() throws IOException { if (buf == null) { throw new IOException(); } return buf.length - pos + in.available(); }
/** Compresses the given {@link InputStream} into the given {@link OutputStream}. * * @param in the {@link InputStream} to compress * @param out the {@link OutputStream} to compress to * @throws IOException */ static public void compress (InputStream in, OutputStream out) throws IOException { CommandLine params = new CommandLine(); boolean eos = false; if (params.Eos) eos = true; com.badlogic.gdx.utils.compression.lzma.Encoder encoder = new com.badlogic.gdx.utils.compression.lzma.Encoder(); if (!encoder.SetAlgorithm(params.Algorithm)) throw new RuntimeException("Incorrect compression mode"); if (!encoder.SetDictionarySize(params.DictionarySize)) throw new RuntimeException("Incorrect dictionary size"); if (!encoder.SetNumFastBytes(params.Fb)) throw new RuntimeException("Incorrect -fb value"); if (!encoder.SetMatchFinder(params.MatchFinder)) throw new RuntimeException("Incorrect -mf value"); if (!encoder.SetLcLpPb(params.Lc, params.Lp, params.Pb)) throw new RuntimeException("Incorrect -lc or -lp or -pb value"); encoder.SetEndMarkerMode(eos); encoder.WriteCoderProperties(out); long fileSize; if (eos) { fileSize = -1; } else { if ((fileSize = in.available()) == 0) { fileSize = -1; } } for (int i = 0; i < 8; i++) { out.write((int)(fileSize >>> (8 * i)) & 0xFF); } encoder.Code(in, out, -1, -1, null); }
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile); java.util.Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdir(); continue; } java.io.InputStream is = jar.getInputStream(file); // get the input stream java.io.FileOutputStream fos = new java.io.FileOutputStream(f); while (is.available() > 0) { // write contents of 'is' to 'fos' fos.write(is.read()); } fos.close(); is.close(); }
public class Exec { public static void main(String []args) throws Exception { Process ps=Runtime.getRuntime().exec(new String[]{"java","-jar","A.jar"}); ps.waitFor(); java.io.InputStream is=ps.getInputStream(); byte b[]=new byte[is.available()]; is.read(b,0,b.length); System.out.println(new String(b)); } }
Payload payload = blob.getPayload(); InputStream is = payload.getInput(); File tempFile = new File(downloadDir, name + ".tmp"); Timer.Context downloadContext = downloadTimer.time(); try { byte[] buf = new byte[BUF_SIZE]; while (read < length) { int avail = Math.min(is.available(), BUF_SIZE); if (avail < 0) { try { Thread.sleep(100); } catch (Exception ex) {} } else { int readLength = is.read(buf); read += readLength; out.write(buf, 0, readLength); File permFile = new File(downloadDir, name); if (tempFile.renameTo(permFile)) { notifyListeners(permFile); } else { throw new IOException("Could not rename file");
public static Map<String, String> loadConfigs(String name) { Map<String, String> configContentMap = new HashMap<>(); // 先取本地文件,再取类路径 File configDir = new File(".." + File.separator + Constant.CONF_DIR + File.separator + name); if (!configDir.exists()) { URL url = MappingConfigsLoader.class.getClassLoader().getResource(""); if (url != null) { configDir = new File(url.getPath() + name + File.separator); } } File[] files = configDir.listFiles(); if (files != null) { for (File file : files) { String fileName = file.getName(); if (!fileName.endsWith(".yml")) { continue; } try (InputStream in = new FileInputStream(file)) { byte[] bytes = new byte[in.available()]; in.read(bytes); String configContent = new String(bytes, StandardCharsets.UTF_8); configContentMap.put(fileName, configContent); } catch (IOException e) { throw new RuntimeException("Read " + name + "mapping config: " + fileName + " error. ", e); } } } return configContentMap; }
public static String readTxt(String file, String charsetName) throws IOException { InputStream is = IOAdapter.open(file); byte[] targetArray = new byte[is.available()]; int len; int off = 0; while ((len = is.read(targetArray, off, targetArray.length - off)) != -1 && off < targetArray.length) { off += len; } is.close(); return new String(targetArray, charsetName); }
/** * Reads all available bytes from {@link InputStream} as a byte array. * Uses {@link InputStream#available()} to determine the size of input stream. * This is the fastest method for reading {@link InputStream} to byte array, but * depends on {@link InputStream} implementation of {@link InputStream#available()}. * * @param input {@link InputStream} to read. * @return byte[] * @throws IOException if total read is less than {@link InputStream#available()}; */ public static byte[] readAvailableBytes(final InputStream input) throws IOException { int numToRead = input.available(); byte[] buffer = new byte[numToRead]; int totalRead = ZERO; int read; while ((totalRead < numToRead) && (read = input.read(buffer, totalRead, numToRead - totalRead)) >= ZERO) { totalRead = totalRead + read; } if (totalRead < numToRead) { throw new IOException("Failed to completely read InputStream"); } return buffer; }