📄 persistent.java
字号:
//
// Unified Library Application
// Case study in Unified Modeling Language Toolkit
//
// Persistent.java: super class for class that wants to be
// persistent. Subclass must implement read and write operations
// that serializes the object to disk. This class reads files
// sequentially and could be improved in performance.
//
// Copyright (c) 1998 John Wiley & Sons, Inc. All rights reserved.
// Reproduction or translations of this work beyond that permitted
// in Section 117 of the 1976 United States Copyright Act without
// the express written permission of the copyright owner is unlawful.
// Requests for further information should be addressed to Permissions
// Department, John Wiley & Sons, Inc. The purchaser may make back-up
// copies for his/her own use only and not for distribution or resale.
// The Publisher assumes no responsibility for errors, omissions, or
// damages, caused by the use of these programs of from the use of the
// information contained herein.
package db;
import util.*;
import java.io.*;
import java.net.*;
public abstract class Persistent
{
private int objid;
private static RandomAccessFile iterfile;
public Persistent()
{
String cname = getClass().getName();
try {
RandomAccessFile file = new RandomAccessFile(cname + ".dat","rw");
try
{ // Update object id counter
objid = file.readInt();
file.seek(0);
objid++;
file.writeInt(objid);
}
catch (IOException e)
{
// File is new, write first object id
objid = 1;
try
{
file.writeInt(objid);
}
catch (IOException e2)
{
System.err.println(e2.getMessage());
}
}
if (file != null)
file.close();
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
}
public ObjId getObjId()
{
return new ObjId(getClass().getName(),objid);
}
protected static final Persistent getObject(ObjId id)
{
Persistent res = null;
String classname = id.getName();
int no = id.getId();
try
{
RandomAccessFile in =
new RandomAccessFile(classname + ".dat","r");
Class cl = Class.forName(classname);
res = (Persistent) cl.newInstance();
in.readInt();
while (in.getFilePointer() < in.length())
{
res.objid = in.readInt();
res.read(in);
if (res.objid == no)
{
in.close();
return res;
}
}
in.close();
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch (InstantiationException e)
{
System.err.println(e.getMessage());
}
catch (IllegalAccessException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
return null;
}
public final void store()
{
try
{
RandomAccessFile out =
new RandomAccessFile(getClass().getName() + ".dat","rw");
// Position at end of file
out.seek(out.length());
out.writeInt(objid);
write(out);
out.close();
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
public boolean delete()
{
Persistent res = null;
try
{
RandomAccessFile in =
new RandomAccessFile(getClass().getName() + ".dat","rw");
Class cl = Class.forName(getClass().getName());
res = (Persistent) cl.newInstance();
in.readInt();
long filepointer = in.getFilePointer();
res.objid = in.readInt();
res.read(in);
do
{
if (res.objid == objid)
{
in.seek(filepointer);
in.writeInt(0);
in.close();
return true;
}
else
{
filepointer = in.getFilePointer();
res.objid = in.readInt();
res.read(in);
}
}
while (true);
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch (InstantiationException e)
{
System.err.println(e.getMessage());
}
catch (IllegalAccessException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
return false;
}
public boolean update()
{
delete();
store();
return true;
}
public static Persistent iterate(String classname, boolean start)
{
Persistent res = null;
try
{
if (start)
{
iterfile =
new RandomAccessFile(classname + ".dat","r");
iterfile.readInt();
}
Class cl = Class.forName(classname);
do
{
res = (Persistent) cl.newInstance();
res.objid = iterfile.readInt();
res.read(iterfile);
} while (res.objid == 0);
return res;
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch (InstantiationException e)
{
System.err.println(e.getMessage());
}
catch (IllegalAccessException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
// This is a "expected" exception,
// means that all objects have been iterated
try {
if (iterfile != null)
iterfile.close();
}
catch (IOException e2)
{
System.err.println(e2.getMessage());
}
}
return null;
}
// This method can be used to "clean up" a file filled with
// deleted objects.
public void clean()
{
Persistent ref = null;
String classname = getClass().getName();
try
{
RandomAccessFile in =
new RandomAccessFile(classname + ".dat","rw");
RandomAccessFile out =
new RandomAccessFile(classname + ".cln","rw");
Class cl = Class.forName(classname);
ref = (Persistent) cl.newInstance();
int high = in.readInt();
out.writeInt(high);
while (in.getFilePointer() < in.length())
{
ref.objid = in.readInt();
ref.read(in);
if (ref.objid != 0)
{
out.writeInt(ref.objid);
ref.write(out);
}
}
out.seek(0);
in.close();
int ch;
// Copy the outfile back to the original file
FileOutputStream dat = new FileOutputStream(classname + ".dat");
while ((ch = out.read()) != -1)
dat.write(ch);
dat.close();
out.close();
// Delete the cln file
File newf = new File(classname + ".cln");
boolean ok = newf.delete();
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch (InstantiationException e)
{
System.err.println(e.getMessage());
}
catch (IllegalAccessException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
public abstract void write(RandomAccessFile out) throws IOException;
public abstract void read(RandomAccessFile in) throws IOException;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -