📄 simpledatabase.java
字号:
/*
Simple Implementation of Kerberos protocol v5
Copyright (C) 2003 Thia Yeo Ching (tycordinal@yahoo.co.uk)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package SimpleKerberos.tool;
import java.io.*;
/**
* General purpose database built using file system.
* Each database is a directory.
* Each record is a text file, and the file name is the ID.
*/
public class SimpleDatabase
{
private static final String USER_HOME = "user.home";
private static final String FILE_SEPARATOR = "file.separator";
private String databaseName_;
/**
* @param dbName
*/
public SimpleDatabase(String dbName)
{
databaseName_ = dbName;
}
public boolean create()
{
// always create under home dir
String fullpathname = getHomeDirWithLastFileSeparator() + databaseName_;
File db = new File(fullpathname);
boolean exists = db.exists();
if (!exists)
{
if (db.mkdir())
{
return true;
}
}
return false;
}
public boolean exists()
{
// always create under home dir
String fullpathname = getHomeDirWithLastFileSeparator() + databaseName_;
File db = new File(fullpathname);
return db.exists();
}
/**
* Return data in the file
* @param name name of the file
* @return null if cannot find the name
*/
public String select(String name)
{
String fullpathname = getFullName(name);
File n = new File(fullpathname);
if (n.exists())
{
try
{
BufferedReader in = new BufferedReader(new FileReader(n));
String str;
str = in.readLine();
in.close();
if (str != null)
{
return str;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
public boolean insert(String name, String data)
{
String fullpathname = getFullName(name);
File n = new File(fullpathname);
if (!n.exists())
{
try
{
if (n.createNewFile())
{
BufferedWriter out = new BufferedWriter(new FileWriter(n));
// file contents will only be the hashed password
out.write(data);
out.close();
return true;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return false;
}
private String getFullName(String name)
{
return getDatabaseFullPath() + getFileSeparator() + name;
}
private String getDatabaseFullPath()
{
return getHomeDirWithLastFileSeparator() + databaseName_;
}
private String getHomeDirWithLastFileSeparator()
{
return System.getProperty(USER_HOME) +
getFileSeparator();
}
private String getFileSeparator()
{
return System.getProperty(FILE_SEPARATOR);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -