@Converter public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) { BasicDBObject answer = null; try { byte[] input = IOConverter.toBytes(is); if (isBson(input)) { BSONCallback callback = new JSONCallback(); new BasicBSONDecoder().decode(input, callback); answer = (BasicDBObject) callback.get(); } else { answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange)); } } catch (Exception e) { LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e); } finally { // we need to make sure to close the input stream IOHelper.close(is, "InputStream", LOG); } return answer; }
/** * decrypt a BSON string * @param datas : BSON formated String * @return JSON formated String */ private static String decryptBSON(String datas){ String[] tab = datas.split("[a-zA-Z]"); byte[] array = new byte[tab.length]; for(int i =0; i<tab.length; i++){ array[i] = Byte.valueOf(tab[i]); } BSONDecoder decoder = new BasicBSONDecoder(); BasicBSONObject obj = (BasicBSONObject) decoder.readObject(array); System.out.println(obj); return obj.toString(); }
public BSONObject getContent(FileSystem fs) { if (cache != null) return cache; byte[] extentBuffer = extent.getBuffer(fs); byte[] buff = new byte[BSONlength]; for (int i = offset + 16; i < offset + 16 + BSONlength; i++) buff[i - offset - 16] = extentBuffer[i]; BasicBSONDecoder decoder = new BasicBSONDecoder(); cache = decoder.readObject(buff); return cache; }
/** * Reads documents via a {@link BasicBSONDecoder}. * * @param bytes * The bytes of the document to be read. * @param divisor * The divisor for the number of {@link #ITERATIONS}. * @return The time to read each document in microseconds. * @see #testLargeDocumentReadPerformance() */ protected double doLegacyRead(final byte[] bytes, final int divisor) { final BasicBSONDecoder decoder = new BasicBSONDecoder(); final int iterations = ITERATIONS / divisor; final long startTime = System.nanoTime(); for (int i = 0; i < iterations; ++i) { decoder.readObject(bytes); } final long endTime = System.nanoTime(); final double delta = ((double) (endTime - startTime)) / TimeUnit.MICROSECONDS.toNanos(1); return (delta / iterations); }
@Override public void displayFile( FileSystem fs, Path path, OutputStream outStream, int startLine, int endLine) throws IOException { FSDataInputStream in = null; try { in = fs.open(path, 16 * 1024 * 1024); long endTime = System.currentTimeMillis() + STOP_TIME; BasicBSONCallback callback = new BasicBSONCallback(); BasicBSONDecoder decoder = new BasicBSONDecoder(); /* * keep reading and rendering bsonObjects until one of these conditions is met: * * a. we have rendered all bsonObjects desired. * b. we have run out of time. */ for (int lineno = 1; lineno <= endLine && System.currentTimeMillis() <= endTime; lineno++) { if (lineno < startLine) { continue; } callback.reset(); decoder.decode(in, callback); BSONObject value = (BSONObject) callback.get(); StringBuilder bldr = new StringBuilder(); bldr.append("\n\n Record "); bldr.append(lineno); bldr.append('\n'); JSON.serialize(value, bldr); outStream.write(bldr.toString().getBytes("UTF-8")); } } catch (IOException e) { outStream.write(("Error in display avro file: " + e.getLocalizedMessage()).getBytes("UTF-8")); } finally { if (in != null) { in.close(); } outStream.flush(); } }
@Override public void displayFile(FileSystem fs, Path path, OutputStream outStream, int startLine, int endLine) throws IOException { FSDataInputStream in = null; try { in = fs.open(path, 16 * 1024 * 1024); long endTime = System.currentTimeMillis() + STOP_TIME; BasicBSONCallback callback = new BasicBSONCallback(); BasicBSONDecoder decoder = new BasicBSONDecoder(); /* * keep reading and rendering bsonObjects until one of these conditions is * met: * * a. we have rendered all bsonObjects desired. b. we have run out of * time. */ for (int lineno = 1; lineno <= endLine && System.currentTimeMillis() <= endTime; lineno++) { if (lineno < startLine) { continue; } callback.reset(); decoder.decode(in, callback); BSONObject value = (BSONObject) callback.get(); StringBuilder bldr = new StringBuilder(); bldr.append("\n\n Record "); bldr.append(lineno); bldr.append('\n'); JSON.serialize(value, bldr); outStream.write(bldr.toString().getBytes("UTF-8")); } } catch (IOException e) { outStream .write(("Error in display avro file: " + e.getLocalizedMessage()) .getBytes("UTF-8")); } finally { if (in != null) { in.close(); } outStream.flush(); } }
@Override protected BSONDecoder initialValue() { return new BasicBSONDecoder(); }