1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.bind;
9 
10 import com.sleepycat.db.DatabaseEntry;
11 
12 /**
13  * A pass-through <code>EntryBinding</code> that uses the entry's byte array as
14  * the key or data object.
15  *
16  * @author Mark Hayes
17  */
18 public class ByteArrayBinding implements EntryBinding<byte[]> {
19 
20     /*
21      * We can return the same byte[] for 0 length arrays.
22      */
23     private static byte[] ZERO_LENGTH_BYTE_ARRAY = new byte[0];
24 
25     /**
26      * Creates a byte array binding.
27      */
ByteArrayBinding()28     public ByteArrayBinding() {
29     }
30 
31     // javadoc is inherited
entryToObject(DatabaseEntry entry)32     public byte[] entryToObject(DatabaseEntry entry) {
33 
34         int len = entry.getSize();
35         if (len == 0) {
36             return ZERO_LENGTH_BYTE_ARRAY;
37         } else {
38             byte[] bytes = new byte[len];
39             System.arraycopy(entry.getData(), entry.getOffset(),
40                              bytes, 0, bytes.length);
41             return bytes;
42         }
43     }
44 
45     // javadoc is inherited
objectToEntry(byte[] object, DatabaseEntry entry)46     public void objectToEntry(byte[] object, DatabaseEntry entry) {
47 
48         entry.setData(object, 0, object.length);
49     }
50 }
51