📄 persistentinputstream.java
字号:
/*
* Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
* Published By SunSoft Press/Prentice-Hall
* Copyright (C) 1996 Sun Microsystems Inc.
* All Rights Reserved. ISBN 0-13-565755-5
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for NON-COMMERCIAL purposes
* and without fee is hereby granted provided that this
* copyright notice appears in all copies.
*
* THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR
* WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
* AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
* BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
/**
* @version 1.00 07 Feb 1996
* @author Cay Horstmann, modified by Roland E. Schmid
*/
package sfclasses;
import java.io.*;
import java.util.*;
import corejava.*;
/**
* This stream is used to restore data from a persistent state.
* Normally the methods are called from the read() method defined
* in the Persistent interface. <br>
* When you want to start the restore process, you have to create
* a new PersistentInputStream passing the data input stream as
* parameter. Then call the appropriate method to restore the object
* you are using as starting point (e.g. readPersistent). Any objects
* contained in this object are automatically restored (recursively).
*/
public class PersistentInputStream extends PushbackInputStream
{ /**
* Initialize the stream using fs as data source.
* @param fs data input stream
*/
public PersistentInputStream(InputStream fs)
{ super(fs);
}
/**
* Read object
* @param skip Identification string, must end with a comma!
* @return Object read, must be typecast to assign to object variable.
*/
public Persistent readPersistent(String skip)
throws java.io.IOException
{
skipString(skip);
int ch;
do
ch = read();
while (ch==10 || ch==','); // skip LF and comma
if (ch == '0')
return null;
unread(ch);
int n;
if (Character.isDigit((char)ch))
{ n = Format.atoi(getToken());
return find(n);
}
String className = getToken();
skipString("[");
Persistent x;
try
{ Class cl = Class.forName(className);
Object ob = cl.newInstance();
x = (Persistent)ob;
} catch(Exception ex)
{ throw new java.io.IOException(ex.toString());
}
x.read(this);
skipString("]");
n = Format.atoi(getToken());
add(n, x);
return x;
}
/**
* Read integer.
* @param skip Identification string, must end with a comma!
* @return Value read
*/
public int readInt(String skip) throws java.io.IOException
{ skipString(skip);
String s = getToken();
return Format.atoi(s);
}
/**
* Read boolean.
* @param skip Identification string, must end with a comma!
* @return Value read
*/
public boolean readBoolean(String skip) throws java.io.IOException
{ skipString(skip);
String s = getToken();
if (s.equals("true"))
return true;
else
return false;
}
/**
* Read double.
* @param skip Identification string, must end with a comma!
* @return Value read
*/
public double readDouble(String skip)
throws java.io.IOException
{ skipString(skip);
String s = getToken();
return Format.atof(s);
}
/**
* Read string.
* @param skip Identification string, must end with a comma!
* @return Value read
*/
public String readString(String skip)
throws java.io.IOException
{ skipString(skip);
skipWhite();
int ch = read();
if (ch == '0') return null;
if (ch != '"')
throw new java.io.IOException("String expected");
StringBuffer b = new StringBuffer(100);
while (true)
{ ch = read();
if (ch == -1 || ch == '"') {
ch = read(); // skip comma
if (ch != ',')
unread(ch);
return new String(b);
}
if (ch == '\\')
ch = read();
b.append((char)ch);
}
}
/**
* Read vector.
* @param skip Identification string, must end with a comma!
* @return Vector read
*/
public Vector readPersistentVector(String skip) throws java.io.IOException
{ skipString(skip);
int ch;
do
ch = read();
while (ch==10); // skip LF
if (ch == '0') return null;
unread(ch);
skipString("Vector[");
int n = readInt("Size=");
Vector v = new Vector(n);
for (int i = 0; i < n; i++)
{ Persistent x = readPersistent("Element=");
v.addElement(x);
}
skipString("],");
return v;
}
/**
* Skip given string. Called from the various read() methods.
*/
public void skipString(String s) throws java.io.IOException
{
skipWhite();
int i = 0;
while (i < s.length())
{ int ch;
ch = read();
if (ch != s.charAt(i))
throw new java.io.IOException(s + " expected");
i++;
}
}
private String getToken() throws java.io.IOException
{ skipWhite();
StringBuffer b = new StringBuffer(100);
while (true)
{ int ch;
ch = read();
if (ch == -1 || Character.isWhitespace((char)ch)
|| ch == ',' || ch == '[' || ch == ']')
{ if (ch != ',')
unread(ch);
return new String(b);
}
if (ch == '\\')
ch = read();
b.append((char)ch);
}
}
private void skipWhite() throws java.io.IOException
{ boolean more = true;
int ch;
do
{ ch = read();
} while (ch != -1 && (Character.isWhitespace((char)ch) || ch==10));
unread(ch);
}
private void add(int n, Persistent x)
{ if (n > pmap.size()) pmap.setSize(n);
pmap.setElementAt(x, n - 1);
}
private Persistent find(int n)
{ if (n <= pmap.size())
return (Persistent)pmap.elementAt(n - 1);
else
return null;
}
private Vector pmap = new Vector();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -