2009-09-09 22:49:16 +02:00
|
|
|
// AbstractRandomAccessWriter.java
|
|
|
|
// -------------------------------
|
|
|
|
// (C) 2004 by Michael Peter Christen; mc@yacy.net
|
2005-04-07 21:19:42 +02:00
|
|
|
// first published on http://www.anomic.de
|
|
|
|
// Frankfurt, Germany, 2004
|
2005-10-05 15:30:12 +02:00
|
|
|
//
|
2009-01-30 16:33:00 +01:00
|
|
|
// $LastChangedDate: 2009-01-30 15:48:11 +0100 (Fr, 30 Jan 2009) $
|
|
|
|
// $LastChangedRevision: 5539 $
|
|
|
|
// $LastChangedBy: orbiter $
|
2005-04-07 21:19:42 +02:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2009-10-10 03:00:49 +02:00
|
|
|
package net.yacy.kelondro.io;
|
2005-04-07 21:19:42 +02:00
|
|
|
|
2006-05-16 00:13:45 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayInputStream;
|
2005-05-05 07:32:19 +02:00
|
|
|
import java.io.IOException;
|
2006-05-16 00:13:45 +02:00
|
|
|
import java.io.InputStreamReader;
|
2008-01-29 11:12:48 +01:00
|
|
|
import java.util.HashMap;
|
2005-05-05 07:32:19 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2005-04-07 21:19:42 +02:00
|
|
|
|
2009-10-10 03:14:19 +02:00
|
|
|
import net.yacy.kelondro.util.ByteBuffer;
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
|
2009-09-09 22:49:16 +02:00
|
|
|
public abstract class AbstractWriter extends AbstractReader implements Writer {
|
2005-10-05 15:30:12 +02:00
|
|
|
|
2009-09-09 22:49:16 +02:00
|
|
|
|
2005-04-07 21:19:42 +02:00
|
|
|
// pseudo-native methods:
|
2008-12-10 15:35:01 +01:00
|
|
|
abstract public void setLength(long length) throws IOException;
|
2005-04-07 21:19:42 +02:00
|
|
|
abstract public void write(byte[] b, int off, int len) throws IOException;
|
2009-09-09 22:49:16 +02:00
|
|
|
|
2005-09-14 12:10:49 +02:00
|
|
|
// derived methods:
|
2005-04-07 21:19:42 +02:00
|
|
|
|
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeShort(final int v) throws IOException {
|
2008-12-04 13:54:16 +01:00
|
|
|
byte[] b = new byte[2];
|
|
|
|
b[0] = (byte) ((v >>> 8) & 0xFF);
|
|
|
|
b[1] = (byte) ( v & 0xFF);
|
|
|
|
this.write(b);
|
2005-04-07 21:19:42 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeInt(final int v) throws IOException {
|
2008-12-10 12:15:19 +01:00
|
|
|
this.write(int2array(v));
|
|
|
|
}
|
|
|
|
|
2009-07-01 00:20:55 +02:00
|
|
|
public final static byte[] int2array(final int v) {
|
2008-12-04 13:54:16 +01:00
|
|
|
byte[] b = new byte[4];
|
|
|
|
b[0] = (byte) ((v >>> 24) & 0xFF);
|
|
|
|
b[1] = (byte) ((v >>> 16) & 0xFF);
|
|
|
|
b[2] = (byte) ((v >>> 8) & 0xFF);
|
|
|
|
b[3] = (byte) ( v & 0xFF);
|
2008-12-10 12:15:19 +01:00
|
|
|
return b;
|
2005-04-07 21:19:42 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeLong(final long v) throws IOException {
|
2008-12-04 13:54:16 +01:00
|
|
|
byte[] b = new byte[8];
|
|
|
|
b[0] = (byte) ((v >>> 56) & 0xFF);
|
|
|
|
b[1] = (byte) ((v >>> 48) & 0xFF);
|
|
|
|
b[2] = (byte) ((v >>> 40) & 0xFF);
|
|
|
|
b[3] = (byte) ((v >>> 32) & 0xFF);
|
|
|
|
b[4] = (byte) ((v >>> 24) & 0xFF);
|
|
|
|
b[5] = (byte) ((v >>> 16) & 0xFF);
|
|
|
|
b[6] = (byte) ((v >>> 8) & 0xFF);
|
|
|
|
b[7] = (byte) ( v & 0xFF);
|
|
|
|
this.write(b);
|
2005-04-07 21:19:42 +02:00
|
|
|
}
|
2005-10-05 15:30:12 +02:00
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void write(final byte[] b) throws IOException {
|
2005-10-05 15:30:12 +02:00
|
|
|
this.write(b, 0, b.length);
|
2005-04-07 21:19:42 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
private final static byte cr = 13;
|
|
|
|
private final static byte lf = 10;
|
2005-12-05 10:13:13 +01:00
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeLine(final String line) throws IOException {
|
2008-08-02 14:12:04 +02:00
|
|
|
final byte[] b = new byte[line.length() + 2];
|
2006-05-10 23:36:44 +02:00
|
|
|
System.arraycopy(line.getBytes(), 0, b, 0, line.length());
|
|
|
|
b[b.length - 2] = cr;
|
|
|
|
b[b.length - 1] = lf;
|
|
|
|
this.write(b);
|
|
|
|
}
|
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeLine(final byte[] line) throws IOException {
|
2008-08-02 14:12:04 +02:00
|
|
|
final byte[] b = new byte[line.length + 2];
|
2006-05-10 23:36:44 +02:00
|
|
|
System.arraycopy(line, 0, b, 0, line.length);
|
|
|
|
b[b.length - 2] = cr;
|
|
|
|
b[b.length - 1] = lf;
|
|
|
|
this.write(b);
|
2005-04-07 21:19:42 +02:00
|
|
|
}
|
2008-12-04 13:54:16 +01:00
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void writeMap(final Map<String, String> map, final String comment) throws IOException {
|
2006-05-10 23:36:44 +02:00
|
|
|
this.seek(0);
|
2007-12-27 18:56:59 +01:00
|
|
|
final Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
|
|
|
|
Map.Entry<String, String> entry;
|
2009-01-30 16:33:00 +01:00
|
|
|
final ByteBuffer bb = new ByteBuffer(map.size() * 40);
|
2006-12-10 14:56:32 +01:00
|
|
|
bb.append("# ").append(comment).append("\r\n");
|
2006-05-10 23:36:44 +02:00
|
|
|
while (iter.hasNext()) {
|
2007-12-27 18:56:59 +01:00
|
|
|
entry = iter.next();
|
2006-12-10 15:06:58 +01:00
|
|
|
bb.append(entry.getKey()).append('=');
|
|
|
|
if (entry.getValue() != null) { bb.append(entry.getValue()); }
|
2006-12-10 14:56:32 +01:00
|
|
|
bb.append("\r\n");
|
2006-05-10 23:36:44 +02:00
|
|
|
}
|
2006-12-10 14:56:32 +01:00
|
|
|
bb.append("# EOF\r\n");
|
2006-05-20 14:09:58 +02:00
|
|
|
write(bb.getBytes());
|
2008-08-06 21:43:12 +02:00
|
|
|
bb.close();
|
2006-05-10 23:36:44 +02:00
|
|
|
}
|
2006-05-16 00:37:02 +02:00
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final HashMap<String, String> readMap() throws IOException {
|
2006-05-10 23:36:44 +02:00
|
|
|
this.seek(0);
|
2008-08-02 14:12:04 +02:00
|
|
|
final byte[] b = readFully();
|
|
|
|
final BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
|
2008-01-29 11:12:48 +01:00
|
|
|
final HashMap<String, String> map = new HashMap<String, String>();
|
2006-05-10 23:36:44 +02:00
|
|
|
String line;
|
|
|
|
int pos;
|
|
|
|
while ((line = br.readLine()) != null) { // very slow readLine????
|
|
|
|
line = line.trim();
|
|
|
|
if (line.equals("# EOF")) return map;
|
|
|
|
if ((line.length() == 0) || (line.charAt(0) == '#')) continue;
|
2010-01-12 21:53:19 +01:00
|
|
|
pos = line.indexOf('=');
|
2006-05-10 23:36:44 +02:00
|
|
|
if (pos < 0) continue;
|
|
|
|
map.put(line.substring(0, pos), line.substring(pos + 1));
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
2006-05-20 14:09:58 +02:00
|
|
|
|
2009-10-12 23:59:39 +02:00
|
|
|
public final void deleteOnExit() {
|
2008-09-12 13:51:48 +02:00
|
|
|
if (this.file != null) this.file.deleteOnExit();
|
|
|
|
}
|
2005-04-07 21:19:42 +02:00
|
|
|
|
2005-10-05 15:30:12 +02:00
|
|
|
}
|