home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 15
/
BUGCD1998_06.ISO
/
aplic
/
jbuilder
/
jsamples.z
/
PersistString.java
< prev
next >
Wrap
Text File
|
1997-07-03
|
2KB
|
62 lines
package borland.samples.apps.chess.client;
import java.io.*;
import java.util.Vector;
/**re-invent the wheel: Concatenate an array of Strings in such a way that they can hold any data
* and be parsed back into the array of strings they were created from
* Simplifies passing multiparameter messages between client and server
*/
public class PersistString {
static public String concat(String[] data ) {
String retVal = "";
for (int i=0;i < data.length && data[i] != null ;i++)
retVal = retVal + i + "\"" + PersistString.padQuotes(data[i])+ "\"" ;
return retVal;
}
static public String [] parse(String infoData) {
Vector output = new Vector(10);
StringReader stream = new StringReader(infoData);
//StringBufferInputStream stream = new StringBufferInputStream(infoData);
//StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(stream, 1000));
StreamTokenizer st = new StreamTokenizer(stream);
try {
st.nextToken();
while(true) {
String data = null;
while (st.nextToken() == '"') {
if (data == null)
data = st.sval;
else
data = data + "\"" + st.sval;
}
output.addElement(data);
if (st.ttype == StreamTokenizer.TT_EOF)
break;
}
}
catch (Exception e) {
System.out.println("PersistStream error " + e);
}
String [] outputArray = new String [output.size()];
for (int i=0;i<output.size();i++)
outputArray[i] = (String) output.elementAt(i);
return outputArray;
}
static String padQuotes(String data) {
int index = 0 ;
int oldIndex = 0;
while (index >= 0) {
index = data.indexOf('\"',oldIndex);
if (index > 0) {
data = data.substring(0,index) + "\"" + data.substring(index );
//System.out.println ("Found a quote in the data " + data);
oldIndex = index + 2;
}
}
return data;
}
}