View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.property.basic;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.InputStream;
28  import net.jcip.annotations.Immutable;
29  import org.modeshape.common.util.CheckArg;
30  import org.modeshape.graph.property.Binary;
31  
32  /**
33   * An implementation of {@link Binary} that keeps the binary data in-memory.
34   */
35  @Immutable
36  public class InMemoryBinary extends AbstractBinary {
37  
38      /**
39       * Version {@value} .
40       */
41      private static final long serialVersionUID = 2L;
42  
43      private final byte[] bytes;
44      private byte[] sha1hash;
45      private int hc;
46  
47      public InMemoryBinary( byte[] bytes ) {
48          super();
49          CheckArg.isNotNull(bytes, "bytes");
50          this.bytes = bytes;
51      }
52  
53      /**
54       * {@inheritDoc}
55       * 
56       * @see java.lang.Object#hashCode()
57       */
58      @Override
59      public int hashCode() {
60          if (sha1hash == null) {
61              // Idempotent, so doesn't matter if we recompute in concurrent threads ...
62              sha1hash = computeHash(bytes);
63              hc = sha1hash.hashCode();
64          }
65          return hc;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public long getSize() {
72          return this.bytes.length;
73      }
74  
75      /**
76       * {@inheritDoc}
77       * 
78       * @see org.modeshape.graph.property.Binary#getHash()
79       */
80      public byte[] getHash() {
81          if (sha1hash == null) {
82              // Idempotent, so doesn't matter if we recompute in concurrent threads ...
83              sha1hash = computeHash(bytes);
84              hc = sha1hash.hashCode();
85          }
86          return sha1hash;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public byte[] getBytes() {
93          return this.bytes;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public InputStream getStream() {
100         return new ByteArrayInputStream(this.bytes);
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public void acquire() {
107         // do nothing
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public void release() {
114         // do nothing
115     }
116 }