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          CheckArg.isNotNull(bytes, "bytes");
49          this.bytes = bytes;
50      }
51  
52      /**
53       * {@inheritDoc}
54       * 
55       * @see java.lang.Object#hashCode()
56       */
57      @Override
58      public int hashCode() {
59          if (sha1hash == null) {
60              // Idempotent, so doesn't matter if we recompute in concurrent threads ...
61              sha1hash = computeHash(bytes);
62              hc = sha1hash.hashCode();
63          }
64          return hc;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public long getSize() {
71          return this.bytes.length;
72      }
73  
74      /**
75       * {@inheritDoc}
76       * 
77       * @see org.modeshape.graph.property.Binary#getHash()
78       */
79      public byte[] getHash() {
80          if (sha1hash == null) {
81              // Idempotent, so doesn't matter if we recompute in concurrent threads ...
82              sha1hash = computeHash(bytes);
83              hc = sha1hash.hashCode();
84          }
85          return sha1hash;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public byte[] getBytes() {
92          return this.bytes;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      public InputStream getStream() {
99          return new ByteArrayInputStream(this.bytes);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     public void acquire() {
106         // do nothing
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     public void release() {
113         // do nothing
114     }
115 }