List of usage examples for com.mongodb.util Util toHex
public static String toHex(final byte[] bytes)
From source file:cc.acs.mongofs.gridfs.CLI.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 1) { printUsage();//from w w w .j av a 2 s . co m return; } Mongo m = null; for (int i = 0; i < args.length; i++) { String s = args[i]; if (s.equals("--db")) { db = args[i + 1]; i++; continue; } if (s.equals("--host")) { host = args[i + 1]; i++; continue; } if (s.equals("help")) { printUsage(); return; } if (s.equals("list")) { GridFS fs = getGridFS(); System.out.printf("%-60s %-10s\n", "Filename", "Length"); for (DBObject o : fs.getFileList()) { System.out.printf("%-60s %-10d\n", o.get("filename"), ((Number) o.get("length")).longValue()); } return; } if (s.equals("get")) { GridFS fs = getGridFS(); String fn = args[i + 1]; GridFSDBFile f = fs.findOne(fn); if (f == null) { System.err.println("can't find file: " + fn); return; } f.writeTo(f.getFilename()); return; } if (s.equals("put")) { GridFS fs = getGridFS(); String fn = args[i + 1]; GridFSInputFile f = fs.createFile(new File(fn)); f.save(); f.validate(); return; } if (s.equals("md5")) { GridFS fs = getGridFS(); String fn = args[i + 1]; GridFSDBFile f = fs.findOne(fn); if (f == null) { System.err.println("can't find file: " + fn); return; } MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.reset(); DigestInputStream is = new DigestInputStream(f.getInputStream(), md5); int read = 0; while (is.read() >= 0) { read++; int r = is.read(new byte[17]); if (r < 0) break; read += r; } byte[] digest = md5.digest(); System.out.println("length: " + read + " md5: " + Util.toHex(digest)); return; } System.err.println("unknown option: " + s); return; } }
From source file:cc.acs.mongofs.gridfs.GridFSInputFile.java
License:Apache License
public int saveChunks(int chunkSize) throws IOException { if (_saved)/* w w w . ja va2 s . co m*/ throw new RuntimeException("already saved!"); if (chunkSize > 3.5 * 1000 * 1000) throw new RuntimeException("chunkSize must be less than 3.5MiB!"); byte[] b = new byte[chunkSize]; long total = 0; int cn = 0; MessageDigest md = _md5Pool.get(); md.reset(); DigestInputStream in = new DigestInputStream(_in, md); while (true) { int start = 0; while (start < b.length) { int r = in.read(b, start, b.length - start); if (r == 0) throw new RuntimeException("i'm doing something wrong"); if (r < 0) break; start += r; } total += start; byte[] mine = b; if (start != b.length) { mine = new byte[start]; System.arraycopy(b, 0, mine, 0, start); } DBObject chunk = BasicDBObjectBuilder.start().add("files_id", _id).add("n", cn++).add("data", mine) .get(); _fs._chunkCollection.save(chunk); if (start < b.length) break; } _md5 = Util.toHex(md.digest()); _md5Pool.done(md); _length = total; _saved = true; return cn; }