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.mavibot;
21
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutput;
28 import java.io.ObjectOutputStream;
29 import java.nio.ByteBuffer;
30 import java.util.Comparator;
31
32 import org.apache.directory.api.ldap.model.name.Dn;
33 import org.apache.directory.api.ldap.model.schema.comparators.DnComparator;
34 import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
35 import org.apache.directory.mavibot.btree.serializer.BufferHandler;
36 import org.apache.directory.server.i18n.I18n;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47
48 public class DnSerializer extends AbstractElementSerializer<Dn>
49 {
50
51 private static final long serialVersionUID = 1L;
52
53
54 private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
55
56
57
58
59 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
60
61 private static Comparator<Dn> comp = new Comparator<Dn>()
62 {
63 DnComparator comparator = new DnComparator( null );
64
65 @Override
66 public int compare( Dn dn1, Dn dn2 )
67 {
68 return comparator.compare( dn1, dn2 );
69 }
70 };
71
72
73
74
75
76 public DnSerializer()
77 {
78 super( comp );
79 }
80
81
82
83
84
85
86
87
88 public byte[] serialize( Dn dn )
89 {
90 try
91 {
92 ByteArrayOutputStream baos = new ByteArrayOutputStream();
93 ObjectOutput out = new ObjectOutputStream( baos );
94
95
96 dn.writeExternal( out );
97
98 out.flush();
99
100 if ( IS_DEBUG )
101 {
102 LOG.debug( ">------------------------------------------------" );
103 LOG.debug( "Serialized {}", dn );
104 }
105
106 return baos.toByteArray();
107 }
108 catch ( IOException e )
109 {
110 throw new RuntimeException( e );
111 }
112 }
113
114
115
116
117
118
119
120
121
122 @Override
123 public Dn deserialize( ByteBuffer buffer ) throws IOException
124 {
125 return deserialize( new BufferHandler( buffer.array() ) );
126 }
127
128
129 @Override
130 public Dn deserialize( BufferHandler bufferHandler ) throws IOException
131 {
132 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bufferHandler.getBuffer() ) );
133
134 try
135 {
136 Dn dn = new Dn();
137
138 dn.readExternal( in );
139
140 return dn;
141 }
142 catch ( ClassNotFoundException cnfe )
143 {
144 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
145 throw new IOException( cnfe.getLocalizedMessage() );
146 }
147 }
148
149
150
151
152
153 @Override
154 public Dn fromBytes( byte[] buffer ) throws IOException
155 {
156 return fromBytes( buffer, 0 );
157 }
158
159
160
161
162
163 @Override
164 public Dn fromBytes( byte[] buffer, int pos ) throws IOException
165 {
166 int length = buffer.length - pos;
167 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( buffer, pos, length ) );
168
169 try
170 {
171 Dn dn = new Dn();
172
173 dn.readExternal( in );
174
175 return dn;
176 }
177 catch ( ClassNotFoundException cnfe )
178 {
179 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
180 throw new IOException( cnfe.getLocalizedMessage() );
181 }
182 }
183
184
185
186
187
188 @Override
189 public Class<?> getType()
190 {
191 return Dn.class;
192 }
193 }