KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > AnonymousFileSource


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.RandomAccessFile JavaDoc;
16 import java.net.URL JavaDoc;
17
18 /**
19  * An anonymous file source creates files in the given directory.
20  */

21 public class AnonymousFileSource {
22     File JavaDoc fDirectory;
23 /**
24  * Creates an anonymous file source which creates files in the given directory.
25  */

26 public AnonymousFileSource(File JavaDoc directory) {
27     if (!directory.exists()) {
28         directory.mkdirs();
29     } else if (!directory.isDirectory()) {
30         throw new IllegalArgumentException JavaDoc("Directory arguments should be a directory."); //$NON-NLS-1$
31
}
32     fDirectory = directory;
33 }
34 /**
35  * Allocates and returns a RandomAccessFile in R/W mode on a new anonymous file.
36  * Guaranteed to be unallocated.
37  */

38 synchronized public RandomAccessFile JavaDoc allocateAnonymousFile() throws IOException JavaDoc {
39     
40     File JavaDoc file = getAnonymousFile();
41     return new RandomAccessFile JavaDoc(file, "rw"); //$NON-NLS-1$
42
}
43 /**
44  * Returns a URL on a newly allocated file with the given initial content.
45  * Guaranteed to be unallocated.
46  */

47 synchronized public URL JavaDoc allocateAnonymousURL(byte[] bytes) throws IOException JavaDoc {
48     try {
49         byte hasharray[] = java.security.MessageDigest.getInstance("SHA").digest(bytes); //$NON-NLS-1$
50
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
51         for (int i = 0; i < hasharray.length; i++) {
52             sb.append(Character.forDigit((hasharray[i] >> 4) & 0x0F, 16));
53             sb.append(Character.forDigit(hasharray[i] & 0x0F, 16));
54         }
55         sb.append(".jnk"); //$NON-NLS-1$
56
String JavaDoc fileName = sb.toString();
57         File JavaDoc file = fileForName(fileName);
58         if (!file.exists()) {
59             RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(file, "rw"); //$NON-NLS-1$
60
raf.write(bytes);
61             raf.close();
62         }
63         return convertFileToURL(file);
64     }
65     catch (java.security.NoSuchAlgorithmException JavaDoc e) {
66         throw new IOException JavaDoc(e.getMessage());
67     }
68 }
69 /**
70  * Returns a URL using the "file" protocol corresponding to the given File.
71  */

72 static public URL JavaDoc convertFileToURL(File JavaDoc file) {
73     try {
74         String JavaDoc path = file.getCanonicalPath().replace(java.io.File.separatorChar, '/');
75         return new URL JavaDoc("file", "", "/" + path); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
76
}
77     catch (IOException JavaDoc ioe) {
78         throw new Error JavaDoc();
79     }
80 }
81 /**
82  * Answer a File to use for the given simple file name.
83  */

84 File JavaDoc fileForName(String JavaDoc fileName) {
85     File JavaDoc dir;
86     if (fileName.length() >= 1) {
87         String JavaDoc dirName = Integer.toHexString((fileName.hashCode() % 255) & 255);
88         dir = new File JavaDoc(fDirectory, dirName);
89         dir.mkdirs();
90     } else {
91         dir = fDirectory;
92     }
93     return new File JavaDoc(dir, fileName);
94 }
95 /**
96  * Returns a new anonymous file, but does not allocate it.
97  * Not guaranteed to be free when used since it is unallocated.
98  */

99 synchronized public File JavaDoc getAnonymousFile() {
100     File JavaDoc file;
101     file = fileForName(getAnonymousFileName());
102     while (file.exists()) {
103         try {
104             Thread.sleep(1);
105         }
106         catch (InterruptedException JavaDoc e) {
107             // ignore
108
}
109         file = fileForName(getAnonymousFileName());
110     }
111     return file;
112 }
113 /**
114  * Returns a new anonymous file name.
115  * Not guaranteed to be free since its directory is unknown.
116  */

117 synchronized public String JavaDoc getAnonymousFileName() {
118     return getAnonymousFileName(System.currentTimeMillis());
119 }
120 /**
121  * Returns a new anonymous file name based on the given long.
122  * Not guaranteed to be free since its directory is unknown.
123  */

124 synchronized public String JavaDoc getAnonymousFileName(long l) {
125     if (l < 0) l = -l;
126     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
127     sb.append(Character.forDigit((int)(l % 26 + 10), 36));
128     l /= 26;
129     while (l != 0) {
130         sb.append(Character.forDigit((int)(l % 36), 36));
131         l /= 36;
132     }
133     sb.append(".jnk"); //$NON-NLS-1$
134
return sb.toString();
135 }
136 }
137
Popular Tags