19
19
package com .mongodb ;
20
20
21
21
import java .util .*;
22
- import java .nio .ByteBuffer ;
22
+ import java .nio .* ;
23
23
24
24
import com .mongodb .util .*;
25
25
@@ -98,36 +98,48 @@ public static ObjectId massageToObjectId( Object o ){
98
98
* @throws IllegalArgumentException if the string is not a valid id
99
99
*/
100
100
public ObjectId ( String s ){
101
+ this ( s , false );
102
+ }
103
+
104
+ public ObjectId ( String s , boolean babble ){
101
105
102
106
if ( ! isValid ( s ) )
103
107
throw new IllegalArgumentException ( "invalid ObjectId [" + s + "]" );
104
108
105
- String baseString = s .substring ( 0 , 16 );
106
- String incString = s .substring ( 16 );
107
-
108
- ByteBuffer buf = ByteBuffer .allocate (24 );
109
-
110
- for (int i =0 ; i < baseString .length () / 2 ; i ++) {
111
- buf .put ((byte ) Integer .parseInt (baseString .substring (i *2 , i *2 + 2 ), 16 ));
109
+ if ( babble ){
110
+ String baseString = s .substring ( 0 , 16 );
111
+ String incString = s .substring ( 16 );
112
+
113
+ ByteBuffer buf = ByteBuffer .allocate (24 );
114
+
115
+ for (int i =0 ; i < baseString .length () / 2 ; i ++) {
116
+ buf .put ((byte ) Integer .parseInt (baseString .substring (i *2 , i *2 + 2 ), 16 ));
117
+ }
118
+
119
+ buf .flip ();
120
+
121
+ _base = buf .getLong ();
122
+
123
+ buf .clear ();
124
+
125
+ for (int i =0 ; i < incString .length () / 2 ; i ++) {
126
+ buf .put ((byte ) Integer .parseInt (incString .substring (i *2 , i *2 + 2 ), 16 ));
127
+ }
128
+
129
+ buf .flip ();
130
+
131
+ _inc = buf .getInt ();
112
132
}
113
-
114
- buf . flip () ;
115
-
116
- _base = buf . getLong ( );
117
-
118
- buf . clear ( );
119
-
120
- for ( int i = 0 ; i < incString . length () / 2 ; i ++) {
121
- buf . put (( byte ) Integer . parseInt ( incString . substring ( i * 2 , i * 2 + 2 ), 16 ) );
133
+ else {
134
+ byte b [] = new byte [ 12 ] ;
135
+ for ( int i = 0 ; i < b . length ; i ++ ){
136
+ b [ b . length -( i + 1 )] = ( byte ) Integer . parseInt ( s . substring ( i * 2 , i * 2 + 2 ) , 16 );
137
+ }
138
+ ByteBuffer bb = ByteBuffer . wrap ( b );
139
+
140
+ _inc = bb . getInt ();
141
+ _base = bb . getLong ( );
122
142
}
123
-
124
- buf .flip ();
125
-
126
- _inc = buf .getInt ();
127
-
128
- // _base = Long.parseLong( baseString , 16 );
129
- // _inc = Integer.parseInt( incString , 16 );
130
-
131
143
_new = false ;
132
144
}
133
145
@@ -176,7 +188,7 @@ public boolean equals( Object o ){
176
188
_inc == other ._inc ;
177
189
}
178
190
179
- public String toString (){
191
+ public String toStringBabble (){
180
192
String a = Long .toHexString ( _base );
181
193
String b = Integer .toHexString ( _inc );
182
194
@@ -193,6 +205,29 @@ public String toString(){
193
205
return buf .toString ();
194
206
}
195
207
208
+ public String toStringMongod (){
209
+ byte b [] = new byte [12 ];
210
+ ByteBuffer bb = ByteBuffer .wrap ( b );
211
+ bb .putInt ( _inc );
212
+ bb .putLong ( _base );
213
+
214
+ StringBuilder buf = new StringBuilder (24 );
215
+
216
+ for ( int i =b .length -1 ; i >=0 ; i -- ){
217
+ int x = b [i ] & 0xFF ;
218
+ String s = Integer .toHexString ( x );
219
+ if ( s .length () == 1 )
220
+ buf .append ( "0" );
221
+ buf .append ( s );
222
+ }
223
+
224
+ return buf .toString ();
225
+ }
226
+
227
+ public String toString (){
228
+ return toStringMongod ();
229
+ }
230
+
196
231
public int compareTo ( ObjectId id ){
197
232
if ( id == null )
198
233
return -1 ;
@@ -214,6 +249,14 @@ public int compareTo( ObjectId id ){
214
249
return 0 ;
215
250
}
216
251
252
+ public long getBase (){
253
+ return _base ;
254
+ }
255
+
256
+ public int getInc (){
257
+ return _inc ;
258
+ }
259
+
217
260
final long _base ;
218
261
final int _inc ;
219
262
0 commit comments