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.api.util.Strings;
26 import org.apache.directory.server.core.avltree.Marshaller;
27 import org.apache.directory.server.i18n.I18n;
28
29
30
31
32
33
34
35
36
37
38
39 public class BTreeRedirectMarshaller implements Marshaller<BTreeRedirect>
40 {
41
42 static final int SIZE = 9;
43
44 public static final BTreeRedirectMarshaller/partition/impl/btree/jdbm/BTreeRedirectMarshaller.html#BTreeRedirectMarshaller">BTreeRedirectMarshaller INSTANCE = new BTreeRedirectMarshaller();
45
46
47
48
49
50 public final byte[] serialize( BTreeRedirect redirect ) throws IOException
51 {
52 byte[] bites = new byte[SIZE];
53
54 bites[0] = 1;
55
56 bites[1] = ( byte ) ( 0xFF & ( redirect.recId >> 56 ) );
57 bites[2] = ( byte ) ( 0xFF & ( redirect.recId >> 48 ) );
58 bites[3] = ( byte ) ( 0xFF & ( redirect.recId >> 40 ) );
59 bites[4] = ( byte ) ( 0xFF & ( redirect.recId >> 32 ) );
60 bites[5] = ( byte ) ( 0xFF & ( redirect.recId >> 24 ) );
61 bites[6] = ( byte ) ( 0xFF & ( redirect.recId >> 16 ) );
62 bites[7] = ( byte ) ( 0xFF & ( redirect.recId >> 8 ) );
63 bites[8] = ( byte ) ( 0xFF & ( redirect.recId ) );
64
65 return bites;
66 }
67
68
69
70
71
72 public final BTreeRedirect deserialize( byte[] bytes ) throws IOException
73 {
74 if ( ( bytes == null ) || ( bytes.length != SIZE ) || ( bytes[0] != 1 ) )
75 {
76 if ( bytes != null )
77 {
78 throw new IOException( I18n.err( I18n.ERR_568, Strings.dumpBytes( bytes ) ) );
79 }
80 else
81 {
82 throw new IOException( I18n.err( I18n.ERR_569 ) );
83 }
84 }
85
86 long recId = ( ( long ) ( bytes[1] & 0xFF ) << 56 )
87 | ( ( long ) ( bytes[2] & 0xFF ) << 48 )
88 | ( ( long ) ( bytes[3] & 0xFF ) << 40 )
89 | ( ( long ) ( bytes[4] & 0xFF ) << 32 )
90 | ( ( long ) ( bytes[5] & 0xFF ) << 24 )
91 | ( ( long ) ( bytes[6] & 0xFF ) << 16 )
92 | ( ( long ) ( bytes[7] & 0xFF ) << 8 )
93 | ( ( long ) ( bytes[8] & 0xFF ) );
94
95 return new BTreeRedirect( recId );
96 }
97
98
99
100
101
102
103
104
105 public static boolean isRedirect( byte[] bites )
106 {
107 return ( bites != null ) && ( bites.length == SIZE ) && ( bites[0] == 1 );
108 }
109 }