01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: ByteArrayBinding.java,v 1.24.2.2 2008/01/07 15:14:05 cwl Exp $
07: */
08:
09: 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 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(), bytes,
41: 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: }
|