1 /* 2 * Created on 22 juil. 2003 3 * 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 */ 18 package org.gudy.azureus2.core3.util; 19 20 import java.util.Arrays; 21 22 import org.gudy.azureus2.plugins.utils.ByteArrayWrapper; 23 24 /** 25 * @author Olivier 26 * 27 */ 28 public class 29 HashWrapper 30 implements ByteArrayWrapper 31 { 32 33 private byte[] hash; 34 private int hash_code; 35 HashWrapper(byte[] _hash)36 public HashWrapper(byte[] _hash) 37 { 38 this(_hash,0,_hash.length); 39 } 40 HashWrapper(byte[] _hash, int offset,int length)41 public HashWrapper(byte[] _hash, int offset,int length) 42 { 43 hash = new byte[length]; 44 45 System.arraycopy(_hash,offset,hash,0,length); 46 47 for (int i = 0; i < length; i++) { 48 49 hash_code = 31*hash_code + hash[i]; 50 } 51 } 52 equals(Object o)53 public boolean equals(Object o) { 54 if(! (o instanceof HashWrapper)) 55 return false; 56 57 byte[] otherHash = ((HashWrapper)o).getHash(); 58 return Arrays.equals(hash, otherHash); 59 } 60 61 public byte[] getHash()62 getHash() 63 { 64 return( hash ); 65 } 66 67 public byte[] getBytes()68 getBytes() 69 { 70 return( hash ); 71 } 72 73 /* (non-Javadoc) 74 * @see java.lang.Object#hashCode() 75 */ hashCode()76 public int hashCode() 77 { 78 return( hash_code ); 79 } 80 toBase32String()81 public String toBase32String() { 82 return Base32.encode(hash); 83 } 84 } 85