1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ByteArrayBinding.java,v 12.7 2008/01/08 20:58:35 bostic Exp $
7 */
8
9package com.sleepycat.bind;
10
11import com.sleepycat.db.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 */
19public 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 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 object, DatabaseEntry entry) {
48
49        byte[] bytes = (byte[]) object;
50        entry.setData(bytes, 0, bytes.length);
51    }
52}
53