List of usage examples for java.io DataOutputStream close
@Override public void close() throws IOException
From source file:com.ebay.erl.mobius.core.model.Tuple.java
public static void main(String[] arg) throws Throwable { DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("s:/test.binary"))); //dos.writeUTF("00_DW_LSTG_ITEM"); dos.writeInt(5);/*from w w w . ja v a2 s. c o m*/ dos.flush(); dos.close(); }
From source file:hyperloglog.tools.HyperLogLogCLI.java
public static void main(String[] args) { Options options = new Options(); addOptions(options);/*www .jav a2 s . co m*/ CommandLineParser parser = new BasicParser(); CommandLine cli = null; long n = 0; long seed = 123; EncodingType enc = EncodingType.SPARSE; int p = 14; int hb = 64; boolean bitPack = true; boolean noBias = true; int unique = -1; String filePath = null; BufferedReader br = null; String outFile = null; String inFile = null; FileOutputStream fos = null; DataOutputStream out = null; FileInputStream fis = null; DataInputStream in = null; try { cli = parser.parse(options, args); if (!(cli.hasOption('n') || cli.hasOption('f') || cli.hasOption('d'))) { System.out.println("Example usage: hll -n 1000 " + "<OR> hll -f /tmp/input.txt " + "<OR> hll -d -i /tmp/out.hll"); usage(options); return; } if (cli.hasOption('n')) { n = Long.parseLong(cli.getOptionValue('n')); } if (cli.hasOption('e')) { String value = cli.getOptionValue('e'); if (value.equals(EncodingType.DENSE.name())) { enc = EncodingType.DENSE; } } if (cli.hasOption('p')) { p = Integer.parseInt(cli.getOptionValue('p')); if (p < 4 && p > 16) { System.out.println("Warning! Out-of-range value specified for p. Using to p=14."); p = 14; } } if (cli.hasOption('h')) { hb = Integer.parseInt(cli.getOptionValue('h')); } if (cli.hasOption('c')) { noBias = Boolean.parseBoolean(cli.getOptionValue('c')); } if (cli.hasOption('b')) { bitPack = Boolean.parseBoolean(cli.getOptionValue('b')); } if (cli.hasOption('f')) { filePath = cli.getOptionValue('f'); br = new BufferedReader(new FileReader(new File(filePath))); } if (filePath != null && cli.hasOption('n')) { System.out.println("'-f' (input file) specified. Ignoring -n."); } if (cli.hasOption('s')) { if (cli.hasOption('o')) { outFile = cli.getOptionValue('o'); fos = new FileOutputStream(new File(outFile)); out = new DataOutputStream(fos); } else { System.err.println("Specify output file. Example usage: hll -s -o /tmp/out.hll"); usage(options); return; } } if (cli.hasOption('d')) { if (cli.hasOption('i')) { inFile = cli.getOptionValue('i'); fis = new FileInputStream(new File(inFile)); in = new DataInputStream(fis); } else { System.err.println("Specify input file. Example usage: hll -d -i /tmp/in.hll"); usage(options); return; } } // return after deserialization if (fis != null && in != null) { long start = System.currentTimeMillis(); HyperLogLog deserializedHLL = HyperLogLogUtils.deserializeHLL(in); long end = System.currentTimeMillis(); System.out.println(deserializedHLL.toString()); System.out.println("Count after deserialization: " + deserializedHLL.count()); System.out.println("Deserialization time: " + (end - start) + " ms"); return; } // construct hll and serialize it if required HyperLogLog hll = HyperLogLog.builder().enableBitPacking(bitPack).enableNoBias(noBias).setEncoding(enc) .setNumHashBits(hb).setNumRegisterIndexBits(p).build(); if (br != null) { Set<String> hashset = new HashSet<String>(); String line; while ((line = br.readLine()) != null) { hll.addString(line); hashset.add(line); } n = hashset.size(); } else { Random rand = new Random(seed); for (int i = 0; i < n; i++) { if (unique < 0) { hll.addLong(rand.nextLong()); } else { int val = rand.nextInt(unique); hll.addLong(val); } } } long estCount = hll.count(); System.out.println("Actual count: " + n); System.out.println(hll.toString()); System.out.println("Relative error: " + HyperLogLogUtils.getRelativeError(n, estCount) + "%"); if (fos != null && out != null) { long start = System.currentTimeMillis(); HyperLogLogUtils.serializeHLL(out, hll); long end = System.currentTimeMillis(); System.out.println("Serialized hyperloglog to " + outFile); System.out.println("Serialized size: " + out.size() + " bytes"); System.out.println("Serialization time: " + (end - start) + " ms"); out.close(); } } catch (ParseException e) { System.err.println("Invalid parameter."); usage(options); } catch (NumberFormatException e) { System.err.println("Invalid type for parameter."); usage(options); } catch (FileNotFoundException e) { System.err.println("Specified file not found."); usage(options); } catch (IOException e) { System.err.println("Exception occured while reading file."); usage(options); } }
From source file:Main.java
public static byte[] convertIntToByteArray(int my_int) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(my_int); dos.close(); byte[] int_bytes = bos.toByteArray(); bos.close();//from w ww . j a v a 2s .c o m return int_bytes; }
From source file:Main.java
public static void writeFile(File file, String string) throws IOException { file.getParentFile().mkdirs();//from w w w . j a v a 2 s. c o m DataOutputStream dout = new DataOutputStream(new FileOutputStream(file)); dout.write(string.getBytes()); dout.close(); }
From source file:com.aliyun.openservices.tablestore.hadoop.Utils.java
static String serialize(Writable w) { ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(os); try {//from w w w. j a v a 2 s . c o m w.write(out); out.close(); } catch (IOException ex) { // intend to ignore } byte[] buf = os.toByteArray(); return Base64.encodeBase64String(buf); }
From source file:Main.java
public static Process runSuCommandAsync(Context context, String command) throws IOException { DataOutputStream fout = new DataOutputStream(context.openFileOutput(SCRIPT_NAME, 0)); fout.writeBytes(command);/*from w w w . j a v a 2 s . com*/ fout.close(); String[] args = new String[] { "su", "-c", ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME }; Process proc = Runtime.getRuntime().exec(args); return proc; }
From source file:com.lightboxtechnologies.spectrum.FolderCount.java
static String convertScanToString(Scan scan) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dos = null; try {/*from w w w . ja va 2s . c o m*/ dos = new DataOutputStream(out); scan.write(dos); dos.close(); } finally { IOUtils.closeQuietly(dos); } return Base64.encodeBytes(out.toByteArray()); }
From source file:Main.java
public static Process runSuCommandAsync_prev(Context context, String command) throws IOException { DataOutputStream fout_prev = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_prev, 0)); fout_prev.writeBytes(command);/*from ww w .j a va2s . c o m*/ fout_prev.close(); String[] args_prev = new String[] { "su", "-c", ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_prev }; Process proc_prev = Runtime.getRuntime().exec(args_prev); return proc_prev; }
From source file:Main.java
public static Process runSuCommandAsync_spica(Context context, String command) throws IOException { DataOutputStream fout_spica = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_spica, 0)); fout_spica.writeBytes(command);//w w w .j av a 2 s. co m fout_spica.close(); String[] args_spica = new String[] { "su", "-c", ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_spica }; Process proc_spica = Runtime.getRuntime().exec(args_spica); return proc_spica; }
From source file:Main.java
public static Process runSuCommandAsync_prev2(Context context, String command) throws IOException { DataOutputStream fout_prev2 = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_prev2, 0)); fout_prev2.writeBytes(command);// w w w . j av a2s .c om fout_prev2.close(); String[] args_prev2 = new String[] { "su", "-c", ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_prev2 }; Process proc_prev2 = Runtime.getRuntime().exec(args_prev2); return proc_prev2; }