📄 persistentoutputstream.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 save data to a persistent state.
* Normally the methods are called from the write() method defined
* in the Persistent interface. <br>
* When you want to start the save process, you have to create
* a new PersistentOutputStream passing the data output stream as
* parameter. Then call the appropriate method to save the object
* you are using as starting point (e.g. writePersistent). Any objects
* contained in this object are automatically saved (recursively).
*/
public class PersistentOutputStream extends PrintWriter
{
/**
* Initialize the stream using fs as data destination.
* @param fs Data output stream
*/
public PersistentOutputStream(OutputStream fs)
{ super(fs, true);
}
/**
* Write object that does not implement Persistent. An object written
* with writeObject cannot be restored properly; only the string
* returned by the object's toString() method is written to the
* stream.
* @param s Identification string, must end with a comma!
* @param x Object to write
*/
public void writeObject(String s, Object x)
{ printIndent();
print(s + x);
println(",");
}
/**
* Write integer.
* @param s Identification string, must end with a comma!
* @param x Integer to write
*/
public void writeInt(String s, int x)
{ printIndent();
print(s + x);
println(",");
}
/**
* Write double.
* @param s Identification string, must end with a comma!
* @param x Value to write
*/
public void writeDouble(String s, double x)
{ printIndent();
print(s); Format.print(this, "%.14g", x); println(",");
}
/**
* Write boolean.
* @param s Identification string, must end with a comma!
* @param x Value to write
*/
public void writeBoolean(String s, boolean x)
{ printIndent();
print(s + x);
println(",");
}
/**
* Write string.
* @param s Identification string, must end with a comma!
* @param x Value to write
*/
public void writeString(String s, String x)
{ printIndent();
print(s);
if (x == null) print("0");
StringBuffer b = new StringBuffer(s.length()
+ x.length() + 2);
print('"');
for (int i = 0; i < x.length(); i++)
{ char ch = x.charAt(i);
if (ch == '\\' || ch == '"')
print('\\');
print((char)ch);
}
println("\",");
}
/**
* Write object of a class that implements Persistent.
* @param s Identification string, must end with a comma!
* @param x Object to write
*/
public void writePersistent(String s, Persistent x)
{ printIndent();
print(s);
if (x == null) println("0");
else
{ int n = find(x);
if (n > 0) println(n);
else
{ n = add(x);
print(((Object)x).getClass().getName());
print("[\n");
incIndent();
x.write(this);
decIndent();
printIndent();
print("]");
print(n);
println(",");
}
}
}
/**
* Write vector.
* @param s Identification string, must end with a comma!
* @param e Vector to write
*/
public void writePersistentVector(String s, Vector e)
{ printIndent();
print(s);
if (e == null) println("0");
else
{ print("Vector[\n");
incIndent();
writeInt("Size=", e.size());
for (int i = 0; i < e.size(); i++)
{ Persistent x = (Persistent)e.elementAt(i);
writePersistent("Element=", x);
}
decIndent();
printIndent();
println("],");
}
}
private int find(Persistent x)
{ for (int i = 0; i < pmap.size(); i++)
{ if (pmap.elementAt(i) == x)
return i + 1;
}
return 0;
}
private int add(Persistent x)
{ pmap.addElement(x);
return pmap.size();
}
private void incIndent() {
Indent += 1;
}
private void decIndent() {
Indent -= 1;
if (Indent < 0)
Indent = 0;
}
private void printIndent() {
for (int i=0; i < Indent; i++)
print(" ");
}
private Vector pmap = new Vector();
private int Indent=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -