Package org.bson.types

Examples of org.bson.types.Binary


            out = c.findOne();
            b = (byte[])(out.get( "a" ) );
            assertEquals( "eliot" , new String( b ) );

            out.put( "a" , new Binary( (byte)111 , raw ) );
            c.save( out );
            Binary blah = (Binary)c.findOne().get( "a" );
            assertEquals( 111 , blah.getType() );
            assertEquals( Util.toHex( raw ) , Util.toHex( blah.getData() ) );
        }

    }
View Full Code Here


            byte[] raw = new byte[9];
            ByteBuffer bb = ByteBuffer.wrap( raw );
            bb.order( Bytes.ORDER );
            bb.putInt( 5 );
            bb.put( "eliot".getBytes() );
            out.put( "a" , new Binary( BSON.B_BINARY , "eliot".getBytes() ) );
            c.save( out );

            // objects of subtype B_BINARY or B_GENERAL should becomes byte[]
            out = c.findOne();
//            Binary blah = (Binary)(out.get( "a" ) );
            byte[] bytes = (byte[]) out.get("a");
            assertEquals( "eliot" , new String( bytes ) );

            out.put( "a" , new Binary( (byte)111 , raw ) );
            c.save( out );
            Binary blah = (Binary)c.findOne().get( "a" );
            assertEquals( 111 , blah.getType() );
            assertEquals( Util.toHex( raw ) , Util.toHex( blah.getData() ) );
        }

    }
View Full Code Here

    public void testOversizedDocumentFailure() {
        int maxObjSize = getMongoClient().getMaxBsonObjectSize();
        DBCollection c = collection;
        c.drop();
        try {
            c.save( new BasicDBObject( "foo" , new Binary(new byte[maxObjSize]) ) );
            fail("Should not be able to save an object larger than maximum bson object size of " + maxObjSize);
        }
        catch ( MongoInternalException ie ) {
            assertEquals(-3, ie.getCode());
        }
View Full Code Here

    @Override
    public void gotBinary(final String name, final byte type, final byte[] data) {
        if( type == BSON.B_GENERAL || type == BSON.B_BINARY )
            _put( name , data );
        else
            _put( name , new Binary( type , data ) );
    }
View Full Code Here

        ObjectId test_ref_id = new ObjectId();
        DBObject test_doc = new BasicDBObject( "abc", "12345" );
        String[] test_arr = new String[] { "foo" , "bar" , "baz" , "x" , "y" , "z" };
        BSONTimestamp test_tsp = new BSONTimestamp();
        Date test_date = new Date();
        Binary test_bin = new Binary( "scott".getBytes() );
        UUID test_uuid = UUID.randomUUID();
        Pattern test_regex = Pattern.compile( "^test.*regex.*xyz$", Pattern.CASE_INSENSITIVE );
        BasicDBObjectBuilder b = BasicDBObjectBuilder.start();
        b.append( "_id", oid );
        b.append( "null", null );
View Full Code Here

    public void shouldThrowAnErrorIfTryingToWriteNamesIntoAJavascriptScope2() {
        //do we really need to test every type written after writeJavaScriptWithScope?
        writer.writeStartDocument();
        writer.writeJavaScriptWithScope("js1", "var i = 1");

        writer.writeBinaryData(new Binary(new byte[]{0, 0, 1, 0}));
    }
View Full Code Here

    @Test
    public void testWriteBinary() {

        writer.writeStartDocument();

        writer.writeBinaryData("b1", new Binary(new byte[]{0, 0, 0, 0, 0, 0, 0, 0}));
        writer.writeBinaryData("b2", new Binary(BSONBinarySubType.OldBinary.getValue(), new byte[]{1, 1, 1, 1, 1}));
        writer.writeBinaryData("b3", new Binary(BSONBinarySubType.Function.getValue(), new byte[]{}));

        writer.writeEndDocument();

        final byte[] expectedValues = {49, 0, 0, 0, 5, 98, 49, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 98, 50, 0,
                                       9, 0,
View Full Code Here

    public void testPopOfMaxDocumentSize() {
        writer.writeStartDocument();
        writer.pushMaxDocumentSize(10);
        writer.writeStartDocument("doc");
        writer.writeEndDocument();
        writer.writeBinaryData("bin", new Binary(new byte[256]));
        writer.writeEndDocument();
    }
View Full Code Here

        // test  BINARY
        byte b[] = {1,2,3,4};
        Base64Codec encoder = new Base64Codec();
        String base64 = encoder.encode(b);
        StringBuilder buf = new StringBuilder();
        serializer.serialize(new Binary(b), buf);
        assertEquals(buf.toString(), "{ \"$binary\" : \""+base64+"\" , \"$type\" : 0}");
       
        // test  BSON_TIMESTAMP
        buf = new StringBuilder();
        serializer.serialize(new BSONTimestamp(123, 456), buf);
View Full Code Here

        }
    }

    @Test
    public void binaryParsing() {
        Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
        assertEquals(parsedBinary.getType(), 0);
        assertArrayEquals(parsedBinary.getData(), new byte[]{97, 98, 99, 100});
    }
View Full Code Here

TOP

Related Classes of org.bson.types.Binary

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.