1 11 package org.eclipse.core.internal.localstore; 12 13 import java.io.InputStream ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import org.eclipse.core.filesystem.EFS; 17 import org.eclipse.core.filesystem.IFileStore; 18 import org.eclipse.core.internal.utils.UniversalUniqueIdentifier; 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.CoreException; 21 22 27 public class BlobStore { 28 protected IFileStore localStore; 29 30 31 protected byte mask; 32 33 private static byte[] randomArray = {-43, -25, 37, 85, -45, 29, -95, -81, -69, 3, -109, -10, -86, 30, -54, -73, -14, 47, -2, -67, 25, -8, -63, 2, 119, -123, 125, 12, 76, -43, -37, 79, 69, -123, -54, 80, -106, -66, -99, -66, 80, -66, -37, -106, -87, 117, 95, 10, 77, -42, -23, 70, 5, -68, 44, 91, -91, -107, -79, 93, 17, 112, 4, 41, -26, -108, -68, 107, -43, 31, 52, 60, 111, -10, -30, 121, -127, -59, -112, -8, 92, -123, 96, 116, 104, 67, 74, -112, -71, -115, 96, 34, -74, 90, 36, -39, 28, -51, 107, 52, -55, 14, 8, 1, 27, -40, 60, 35, -5, -62, 7, -100, 32, 5, -111, 29, 96, 61, 110, -111, 50, 56, -21, -17, -86, -118, 17, -45, 56, 98, 101, 126, 27, 57, -45, -112, -50, -49, -77, 111, -96, 50, -13, 69, 106, 118, -101, -97, 28, 57, 11, -81, 43, -83, 96, -75, 99, -87, -85, -100, -10, -13, 30, 35 -58, -5, 81, 77, 92, -96, -21, -41, -69, 23, 71, 58, -9, 127, 56, 118, -124, 79, -68, 42, -68, -98, 121, -1, 65, -102, 118, -84, -39, 4, 47, 105, -52, -121, 27, 43, 90, 9, 31, 59, 115, -63, 28, 55, 101, 9, 117, -45, 112, 61, 55, 23, -21, 51, 104, 123, -118, 76, -108, 115, 119, 81, 54, 39, 46, -107, -65, 79, 16, -34, 69, -37, -120, -108, -75, 77, -6, 101, -33, -116, -62, -115, 44, -61, -39, 31, -33, -49, -107, -11, 115, -13, -73,}; 36 37 42 public BlobStore(IFileStore store, int limit) { 43 Assert.isNotNull(store); 44 localStore = store; 45 Assert.isTrue(localStore.fetchInfo().isDirectory()); 46 Assert.isTrue(limit == 256 || limit == 128 || limit == 64 || limit == 32 || limit == 16 || limit == 8 || limit == 4 || limit == 2 || limit == 1); 47 mask = (byte) (limit - 1); 48 } 49 50 public UniversalUniqueIdentifier addBlob(IFileStore target, boolean moveContents) throws CoreException { 51 UniversalUniqueIdentifier uuid = new UniversalUniqueIdentifier(); 52 folderFor(uuid).mkdir(EFS.NONE, null); 53 IFileStore destination = fileFor(uuid); 54 if (moveContents) 55 target.move(destination, EFS.NONE, null); 56 else 57 target.copy(destination, EFS.NONE, null); 58 return uuid; 59 } 60 61 64 private void appendByteString(StringBuffer buffer, byte value) { 65 String hexString; 66 if (value < 0) 67 hexString = Integer.toHexString(256 + value); 68 else 69 hexString = Integer.toHexString(value); 70 if (hexString.length() == 1) 71 buffer.append("0"); buffer.append(hexString); 73 } 74 75 80 private String bytesToHexString(byte[] b) { 81 StringBuffer buffer = new StringBuffer (); 82 for (int i = 0; i < b.length; i++) 83 appendByteString(buffer, b[i]); 84 return buffer.toString(); 85 } 86 87 90 public void deleteBlob(UniversalUniqueIdentifier uuid) { 91 Assert.isNotNull(uuid); 92 try { 93 fileFor(uuid).delete(EFS.NONE, null); 94 } catch (CoreException e) { 95 } 97 } 98 99 102 public void deleteBlobs(Set set) { 103 for (Iterator i = set.iterator(); i.hasNext();) 104 deleteBlob((UniversalUniqueIdentifier) i.next()); 105 } 106 107 public IFileStore fileFor(UniversalUniqueIdentifier uuid) { 108 IFileStore root = folderFor(uuid); 109 return root.getChild(bytesToHexString(uuid.toBytes())); 110 } 111 112 115 public IFileStore folderFor(UniversalUniqueIdentifier uuid) { 116 byte hash = hashUUIDbytes(uuid); 117 hash &= mask; String dirName = Integer.toHexString(hash + (128 & mask)); return localStore.getChild(dirName); 120 } 121 122 public InputStream getBlob(UniversalUniqueIdentifier uuid) throws CoreException { 123 IFileStore blobFile = fileFor(uuid); 124 return blobFile.openInputStream(EFS.NONE, null); 125 } 126 127 131 protected byte hashUUIDbytes(UniversalUniqueIdentifier uuid) { 132 byte[] bytes = uuid.toBytes(); 133 byte hash = 0; 134 for (int i = 0; i < bytes.length; i++) 135 hash ^= randomArray[bytes[i] + 128]; return hash; 137 } 138 } 139 | Popular Tags |