/*
PointerRecord.java
*
* Copyright (c) 1999-2001, Ted Nelson and Tuomas Lukka
*
* This file is part of Gzz.
*
* Gzz is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Gzz 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 Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with Gzz; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
*/
/*
* Written by Tuomas Lukka
*/
package gzz.mediaserver;
import java.util.*;
import java.io.*;
/** A record of a pointer change, as specified in the
* Documentation/Mediaserver
spec.
*/
public class PointerRecord {
public PointerRecord(String id, Mediaserver.Id to, Set obsoletes,
Mediaserver.Id myId) {
this.id = id;
if (to == null) throw new NullPointerException();
this.to = to;
this.obsoletes = new HashSet(obsoletes);
this.myId = myId;
}
/** The mediaserver id of the block containing this pointer record.
* May be null
if this is a new, unsaved pointer.
*/
final Mediaserver.Id myId;
public Mediaserver.Id getMSId() { return myId; }
/** The identifier of the pointer.
*/
final String id;
/** The mediaserver id this pointer record points to.
*/
final Mediaserver.Id to;
/** The set of the Mediaserver.Id
objects of the pointer
* records obsoleted by this one.
*/
final Set obsoletes;
public Mediaserver.Id save(Mediaserver ms) throws IOException {
if(myId != null)
throw new Error("Tried to save pointer record that has a "+
"mediaserver id already (i.e., is already saved");
ByteArrayOutputStream os = new ByteArrayOutputStream();
write(os);
return ms.addDatum(os.toByteArray(),
"application/x-gzigzag-ptr",
to
);
}
public void write(OutputStream o) throws IOException {
// be a bit too trusting about the data :(
StringBuffer b = new StringBuffer();
b.append("GZZPTR0\012");
b.append(id); b.append("\012");
b.append(to.getString()); b.append("\012");
for(Iterator i=obsoletes.iterator(); i.hasNext();) {
Mediaserver.Id oid = (Mediaserver.Id)i.next();
if(oid == null) {
System.out.println("Null oid!");
continue;
}
String s = oid.getString();
b.append(s);
b.append("\012");
}
o.write(b.toString().getBytes("ISO8859_1"));
o.flush();
o.close();
}
static public PointerRecord read(
Mediaserver.Id myId, InputStream i) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(i, "ISO8859_1"));
String s = br.readLine();
if(!s.equals("GZZPTR0"))
throw new IOException("Invalid format");
String id = br.readLine();
Mediaserver.Id to = new Mediaserver.Id(br.readLine());
String ob;
Set obsoletes = new HashSet();
while((ob = br.readLine()) != null)
obsoletes.add(new Mediaserver.Id(ob));
return new PointerRecord(id, to, obsoletes, myId);
}
}