📄 cm.java
字号:
int hash = 0;
try {
StringWriter out = new StringWriter();
((StructuredTextDocument) doc).sendToWriter(out);
hash = out.toString().hashCode();
out.close();
} catch (IOException ex) {
}
return "cm" + Integer.toString(hash);
}
/**
* Returns the relative time at which the specified file should
* expire in other peers caches.
*
* @param dn contains the name of the folder
* @param fn contains the name of the file
* @return long containing the time the file will expire or -1 if never expires.
* @since 1.0
*/
public long getExpirationTime(String dn, String fn) {
File file = getFile(dn, fn);
long absoluteExpirationTimeForOthers =
expirer.getExpirationTime(file);
if (absoluteExpirationTimeForOthers == -1)
return 0;
else
return absoluteExpirationTimeForOthers -
System.currentTimeMillis();
}
/**
* Gets the list of all the files into the given folder
*
* @param dn contains the name of the folder
* @return String[] an array of Strings containing the name of the files
* @since 1.0
*/
public String[] getFileNames(String dn) {
if (dn == null) {
// why?!
return null;
} else {
File dir = new File(rootDir, dn);
return dir.list();
}
}
/**
* Returns the name of the oldest file in a given directory
*
* @param dn contains the name of the folder
* @return String returns the name of the oldest file
* @since 1.0
*/
public String getOldestFile(String dn) {
if (dn == null) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("nul directory name");
return null;
}
try {
File dir = new File(rootDir, dn);
//PDA requirements 19.02.2002
//method java.io.File.listFiles() did not exist in jdk 1.1.8
//File [] list = dir.listFiles();
String[] ss = dir.list();
File[] list = null;
if (ss != null) {
int n = ss.length;
list = new File[n];
for (int i = 0; i < n; i++) {
list[i] = new File(rootDir, ss[i]);
}
}
//PDA requirements 19.02.2002
if (list == null || list.length == 0) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("no files in folder");
return null;
}
long oldestTime = list[0].lastModified();
File oldest = list[0];
File file = null;
long time = 0;
for (int i = 1; i < list.length; ++i) {
file = list[i];
time = file.lastModified();
if (time < oldestTime) {
// This one is more recent
oldest = list[i];
oldestTime = time;
}
}
return oldest.getName();
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.WARN))
LOG.debug("Failure determining oldest file", e);
return null;
}
}
/**
* Returns true if the directory exists
*
* @param dn contains the name of the folder
* @return boolean
* @since 1.0
*/
public boolean isFolder(String dn) {
if (dn == null) {
return false;
}
File dir = new File(rootDir, dn);
return dir.exists() && dir.isDirectory();
}
/**
* Returns the inputStream of a specified file, in a specified dir
*
* @param dn directory name
* @param fn file name
* @return The inputStream value
* @exception IOException if an I/O error occurs
* @since 1.0
*/
public FileInputStream getInputStream(String dn, String fn)
throws IOException {
return new FileInputStream(getFile(dn, fn));
}
public void createFolder(String f) throws IOException {
createFolder(f, null);
}
/**
* Creates a new folder. Note that this function is idempotent.
*
* @param f directory name
* @param raw don't use any optimization of any sort (no idex, nor
* cache
* @throws IOException if an I/O error occurs.
* @since 1.0
*/
public synchronized void createFolder(String f, String [] elements) throws IOException {
// note : initdir creates IndexService.Index for the folder
// while this method creates the CmCache
File dir = initDir(f);
expirer.addDocDir(dir);
switch (searchMethod) {
case RAW:
// nothing needs to be done
break;
//PDA requirements 21.02.2002
//package net.jxta.impl.index.* is not ported to PDA
//search method "INDEX" is not used on a PDA for the time being
/*case INDEX :
if (indexes.get(f) == null) {
if (LOG.isEnabledFor(Priority.ERROR)){
LOG.error("Creating indexer threads with null index");
}
new Thread(new CmIndexerThread(new File[] { dir },
new IndexService.Index[] {
(IndexService.Index) indexes.get(f) }),
"CmIndexerThread: " + f).start();
}
break;*/
//PDA requirements 21.02.2002
case CACHE :
if (elements != null ) {
if (caches.get(f) == null) {
CmCache cmc = new CmCache (elements, dir);
caches.put(f,cmc);
new Thread (cmc, f+" CmCache ").start();
}
}
break;
default :
// do nothing
break;
}
}
/**
* Generates a random file name
*
* @param dn directory name in which the tmp filename would be created
* @return temporary file name.
* @since 1.0
*/
public String createTmpName(String dn) {
String tryName;
File dir = new File(rootDir, dn);
do {
tryName = "cm" + Integer.toString(random.nextInt());
} while ((new File(dir, tryName)).exists());
return tryName;
}
/**
* Remove a file
*
* @param dn directory name
* @param fn file name
* @throws IOException if an I/O error occurs
* @since 1.0
*/
public void remove(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
try {
expirer.cancelFileExpiration(file);
//PDA requirements 21.02.2002
//package net.jxta.impl.index.* is not ported to PDA
//search method "INDEX" is not used on a PDA for the time being
/*if (searchMethod == INDEX) {
InputStream is= null;
try {
is = new FileInputStream(file);
((IndexService.Index) indexes.get(dn)).remove(fn, is);
} catch (IndexService.BadDocumentException e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("bad file ", e );
} catch (IndexService.IndexException e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index Exception", e );
} finally {
if (is != null) {
try { is.close(); }
catch (IOException e) { }
}
}
} else*/ if ( searchMethod == CACHE ) {
//PDA requirements 21.02.2002
((CmCache) caches.get(dn)).remove(fn);
}
} finally {
if (!file.delete()) {
throw new IOException("Cm cannot remove the file " + file);
}
}
}
/**
* Tests if a file does exist
*
* @param dn directory name
* @param fn file name
* @return Description of the Returned Value
* @throws IOException if an I/O error occurs
* @since 1.0
*/
public boolean exists(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
return file.exists();
}
/**
* Restore a saved StructuredDocument.
*
* @param dn directory name
* @param fn file name
* @return StructuredDocument containing the file
* @throws IOException if an I/O error occurs
* was not possible.
* @since 1.0
*/
public StructuredDocument restore(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
return restoreFile(file);
}
/**
* Restore a saved byte array.
*
* @param dn directory name
* @param fn file name
* @return byte [] containing the file
* @throws IOException if an I/O error occurs
* @throws InvocationTargetException when restoring the document
* was not possible.
* @since 1.0
*/
public byte[] restoreBytes(String dn, String fn)
throws IOException {
File file = getFile(dn, fn);
InputStream ip = null;
byte[] buffer = null;
try {
ip = new FileInputStream(file);
int size = (int) file.length();
buffer = new byte[size];
int got = 0;
while (got < size) {
int res = ip.read(buffer, got, size - got);
if (-1 == res) {
ip.close();
return null;
}
got += res;
}
} finally {
if (ip != null) {
try {
ip.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -