Skip to main content
summaryrefslogtreecommitdiffstats
blob: 67182ba3ab4864cba2976f03c34fc9a86d75fd36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.eclipse.jdt.internal.core.util;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import java.io.*;
import java.net.*;


/** An anonymous file source creates files in the given directory.
 */
public class AnonymousFileSource {
	File fDirectory;
/**
 * Creates an anonymous file source which creates files in the given directory.
 */
public AnonymousFileSource(File directory) {
	if (!directory.exists()) {
		directory.mkdirs();
	} else if (!directory.isDirectory()) {
		throw new IllegalArgumentException("Directory arguments should be a directory.");
	}
	fDirectory = directory;	
}
/**
 * Allocates and returns a RandomAccessFile in R/W mode on a new anonymous file.
 * Guaranteed to be unallocated.
 */
synchronized public RandomAccessFile allocateAnonymousFile() throws IOException {
	
	File file = getAnonymousFile();
	return new RandomAccessFile(file, "rw");
}
/**
 * Returns a URL on a newly allocated file with the given initial content.
 * Guaranteed to be unallocated.
 */
synchronized public URL allocateAnonymousURL(byte[] bytes) throws IOException {
	try {
		byte hasharray[] = java.security.MessageDigest.getInstance("SHA").digest(bytes);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < hasharray.length; i++) {
			sb.append(Character.forDigit((int)((hasharray[i] >> 4) & 0x0F), 16));
			sb.append(Character.forDigit((int)(hasharray[i] & 0x0F), 16));
		}
		sb.append(".jnk");
		String fileName = sb.toString();
		File file = fileForName(fileName);
		if (!file.exists()) {
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			raf.write(bytes);
			raf.close();
		}
		return convertFileToURL(file);
	} 
	catch (java.security.NoSuchAlgorithmException e) {
		throw new IOException(e.getMessage());
	}
}
/**
 * Returns a URL using the "file" protocol corresponding to the given File.
 */
static public URL convertFileToURL(File file) {
	try {
		String path = file.getCanonicalPath().replace(java.io.File.separatorChar, '/');
		return new URL("file", "", "/" + path);
	}
	catch (IOException ioe) {
		throw new Error();
	}
}
/**
 * Answer a File to use for the given simple file name.
 */
File fileForName(String fileName) {
	File dir;
	if (fileName.length() >= 1) {
		String dirName = Integer.toHexString((fileName.hashCode() % 255) & 255);
		dir = new File(fDirectory, dirName);
		dir.mkdirs();
	} else {
		dir = fDirectory;
	}
	return new File(dir, fileName);	
}
/**
 * Returns a new anonymous file, but does not allocate it.  
 * Not guaranteed to be free when used since it is unallocated.
 */
synchronized public File getAnonymousFile() {
	File file;
	file = fileForName(getAnonymousFileName());
	while (file.exists()) {
		try {
			Thread.sleep(1);
		} 
		catch (InterruptedException e) {
		}
		file = fileForName(getAnonymousFileName());
	}
	return file;
}
/**
 * Returns a new anonymous file name.  
 * Not guaranteed to be free since its directory is unknown.
 */
synchronized public String getAnonymousFileName() {
	return getAnonymousFileName(System.currentTimeMillis());
}
/**
 * Returns a new anonymous file name based on the given long.  
 * Not guaranteed to be free since its directory is unknown.
 */
synchronized public String getAnonymousFileName(long l) {
	if (l < 0) l = -l;
	StringBuffer sb = new StringBuffer();
	sb.append(Character.forDigit((int)(l % 26 + 10), 36));
	l /= 26;
	while (l != 0) {
		sb.append(Character.forDigit((int)(l % 36), 36));
		l /= 36;
	}
	sb.append(".jnk");
	return sb.toString();
}
}

Back to the top