[JAVA-103] ByteEncoder assumes all arrays are Object[] Created: 12/Apr/10  Updated: 29/Oct/10  Resolved: 29/Sep/10

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 1.4
Fix Version/s: 2.2

Type: Bug Priority: Major - P3
Reporter: Matt Mastracci Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Java


Backwards Compatibility: Major Change

 Description   

The ByteEncoder class assumes that if a value isArray(), the array type is Object. This throws a ClassCastException if you attempt to pass a primitive array:

else if ( val.getClass().isArray() )
putList( name , Arrays.asList( (Object[])val ) );

It would be more flexible to use Array.get() to reflectively read the array:

private void putArray( String name , Object array )

{ _put( ARRAY , name ); final int sizePos = _buf.position(); _buf.putInt( 0 ); int size = Array.getLength(array); for ( int i=0; i<size; i++ ) _putObjectField( String.valueOf( i ) , Array.get( array, i ) ); _buf.put( EOO ); _buf.putInt( sizePos , _buf.position() - sizePos ); }

I can provide a patch, if needed.



 Comments   
Comment by auto [ 29/Sep/10 ]

Author:

{'login': 'erh', 'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}

Message: additional test for JAVA-103
http://github.com/mongodb/mongo-java-driver/commit/6141925e28ce3a843c4e11d0d632eb440650f1fa

Comment by auto [ 29/Sep/10 ]

Author:

{'login': '', 'name': 'hmeiser', 'email': 'hmeiser@example.com'}

Message: BSONEncoder now can handle primitive arrays JAVA-103
http://github.com/mongodb/mongo-java-driver/commit/a72081d4f6ffaa187c8a6f0e1882f943ac29e863

Comment by Hans Meiser [ 29/Sep/10 ]

http://github.com/theunique/mongo-java-driver/commit/e6e164c6fcadfcf9a50b4b4723ba516afeb49c85

Generated at Thu Feb 08 08:51:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.