1 4 package com.tc.object.dna.impl; 5 6 import java.io.Serializable ; 7 import java.io.UnsupportedEncodingException ; 8 import java.util.Arrays ; 9 10 17 public class UTF8ByteDataHolder implements Serializable { 18 19 private final byte[] bytes; 20 private int hash = 0; 21 22 public UTF8ByteDataHolder(String str) { 24 try { 25 this.bytes = str.getBytes("UTF-8"); 26 } catch (UnsupportedEncodingException e) { 27 throw new AssertionError (e); 28 } 29 } 30 31 public UTF8ByteDataHolder(byte[] b) { 32 this.bytes = b; 33 } 34 35 public byte[] getBytes() { 36 return bytes; 37 } 38 39 public String asString() { 40 try { 41 return new String (bytes, "UTF-8"); 42 } catch (UnsupportedEncodingException e) { 43 throw new AssertionError (e); 44 } 45 } 46 47 public String toString() { 48 return asString(); 49 } 50 51 public int hashCode() { 52 if (hash == 0) { 53 int tmp = 0; 54 for (int i = 0, n = bytes.length; i < n; i++) { 55 tmp = 31 * tmp + bytes[i++]; 56 } 57 hash = tmp; 58 } 59 return hash; 60 } 61 62 public boolean equals(Object obj) { 63 if (obj instanceof UTF8ByteDataHolder) { 64 UTF8ByteDataHolder other = (UTF8ByteDataHolder) obj; 65 return ((Arrays.equals(this.bytes, other.bytes)) && this.getClass().equals(other.getClass())); 66 } 67 return false; 68 } 69 } 70 | Popular Tags |