KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > ByteArrayBinding


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: ByteArrayBinding.java,v 1.24 2006/10/30 21:14:06 bostic Exp $
7  */

8
9 package com.sleepycat.bind;
10
11 import com.sleepycat.je.DatabaseEntry;
12
13 /**
14  * A pass-through <code>EntryBinding</code> that uses the entry's byte array as
15  * the key or data object.
16  *
17  * @author Mark Hayes
18  */

19 public class ByteArrayBinding implements EntryBinding {
20
21     /*
22      * We can return the same byte[] for 0 length arrays.
23      */

24     private static byte[] ZERO_LENGTH_BYTE_ARRAY = new byte[0];
25
26     /**
27      * Creates a byte array binding.
28      */

29     public ByteArrayBinding() {
30     }
31
32     // javadoc is inherited
33
public Object JavaDoc entryToObject(DatabaseEntry entry) {
34
35     int len = entry.getSize();
36     if (len == 0) {
37         return ZERO_LENGTH_BYTE_ARRAY;
38     } else {
39         byte[] bytes = new byte[len];
40         System.arraycopy(entry.getData(), entry.getOffset(),
41                  bytes, 0, bytes.length);
42         return bytes;
43     }
44     }
45
46     // javadoc is inherited
47
public void objectToEntry(Object JavaDoc object, DatabaseEntry entry) {
48
49         byte[] bytes = (byte[]) object;
50         entry.setData(bytes, 0, bytes.length);
51     }
52 }
53
Popular Tags