1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.partition.impl.btree.jdbm;
21
22
23 import java.io.IOException;
24
25 import org.apache.directory.server.core.avltree.Marshaller;
26 import org.apache.directory.shared.asn1.codec.binary.Hex;
27
28
29
30
31
32
33
34
35
36
37
38 public class BTreeRedirectMarshaller implements Marshaller<BTreeRedirect>
39 {
40
41 static final int SIZE = 9;
42
43 public static final BTreeRedirectMarshaller INSTANCE = new BTreeRedirectMarshaller();
44
45
46
47
48 public final byte[] serialize( BTreeRedirect redirect ) throws IOException
49 {
50 byte[] bites = new byte[SIZE];
51
52 bites[0] = 1;
53
54 bites[1] = ( byte ) ( redirect.recId >> 56 );
55 bites[2] = ( byte ) ( redirect.recId >> 48 );
56 bites[3] = ( byte ) ( redirect.recId >> 40 );
57 bites[4] = ( byte ) ( redirect.recId >> 32 );
58 bites[5] = ( byte ) ( redirect.recId >> 24 );
59 bites[6] = ( byte ) ( redirect.recId >> 16 );
60 bites[7] = ( byte ) ( redirect.recId >> 8 );
61 bites[8] = ( byte ) redirect.recId;
62
63 return bites;
64 }
65
66
67
68
69
70 public final BTreeRedirect deserialize( byte[] bites ) throws IOException
71 {
72 if ( bites == null || bites.length != SIZE || bites[0] != 1 )
73 {
74 if ( bites != null )
75 {
76 throw new IOException( "Not a serialized BTreeRedirect object: "
77 + new String( Hex.encodeHex( bites ) ) );
78 }
79 else
80 {
81 throw new IOException( "Not a serialized BTreeRedirect object: byte array is null." );
82 }
83 }
84
85 long recId;
86 recId = bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 );
87 recId <<= 8;
88 recId += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 );
89 recId <<= 8;
90 recId += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 );
91 recId <<= 8;
92 recId += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 );
93 recId <<= 8;
94 recId += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 );
95 recId <<= 8;
96 recId += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 );
97 recId <<= 8;
98 recId += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 );
99 recId <<= 8;
100 recId += bites[8] + ( ( bites[8] < 0 ) ? 256 : 0 );
101
102 return new BTreeRedirect( recId );
103 }
104
105
106
107
108
109
110
111
112 public static boolean isRedirect( byte[] bites )
113 {
114 return bites != null && bites.length == SIZE && bites[0] == 1;
115 }
116 }