001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.graph.property.basic; 025 026 import java.io.ByteArrayInputStream; 027 import java.io.InputStream; 028 import net.jcip.annotations.Immutable; 029 import org.jboss.dna.common.util.CheckArg; 030 import org.jboss.dna.graph.property.Binary; 031 032 /** 033 * An implementation of {@link Binary} that keeps the binary data in-memory. 034 * 035 * @author Randall Hauch 036 */ 037 @Immutable 038 public class InMemoryBinary extends AbstractBinary { 039 040 /** 041 * Version {@value} . 042 */ 043 private static final long serialVersionUID = 2L; 044 045 private final byte[] bytes; 046 private byte[] sha1hash; 047 private int hc; 048 049 public InMemoryBinary( byte[] bytes ) { 050 CheckArg.isNotNull(bytes, "bytes"); 051 this.bytes = bytes; 052 } 053 054 /** 055 * {@inheritDoc} 056 * 057 * @see java.lang.Object#hashCode() 058 */ 059 @Override 060 public int hashCode() { 061 if (sha1hash == null) { 062 // Idempotent, so doesn't matter if we recompute in concurrent threads ... 063 sha1hash = computeHash(bytes); 064 hc = sha1hash.hashCode(); 065 } 066 return hc; 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 public long getSize() { 073 return this.bytes.length; 074 } 075 076 /** 077 * {@inheritDoc} 078 * 079 * @see org.jboss.dna.graph.property.Binary#getHash() 080 */ 081 public byte[] getHash() { 082 if (sha1hash == null) { 083 // Idempotent, so doesn't matter if we recompute in concurrent threads ... 084 sha1hash = computeHash(bytes); 085 hc = sha1hash.hashCode(); 086 } 087 return sha1hash; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 public byte[] getBytes() { 094 return this.bytes; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 100 public InputStream getStream() { 101 return new ByteArrayInputStream(this.bytes); 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 public void acquire() { 108 // do nothing 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 public void release() { 115 // do nothing 116 } 117 }