Update of /cvsroot/exist/eXist-1.0/src/org/exist/storage/io In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17296/src/org/exist/storage/io Added Files: AbstractVariableByteInput.java VariableByteArrayInput.java VariableByteInputStream.java VariableByteOutputStream.java VariableByteInput.java Log Message: Moved VariableByteOutput classes to their own package. Performance improvements. --- NEW FILE: VariableByteOutputStream.java --- package org.exist.storage.io; import java.io.OutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.exist.util.ByteArray; import org.exist.util.FastByteBuffer; /** * A byte array output stream using variable byte encoding. * * @author wolf */ public class VariableByteOutputStream extends OutputStream { protected ByteArray buf; public VariableByteOutputStream() { super(); buf = new FastByteBuffer(9); } public VariableByteOutputStream(int size) { super(); buf = new FastByteBuffer(size); } public void clear() { buf.setLength(0); } public void close() throws IOException { buf = null; } public void flush() throws IOException { } public byte[] toByteArray() { byte[] b = new byte[buf.size()]; buf.copyTo(b, 0); return b; } public ByteArray data() { return buf; } public void write(int b) throws IOException { buf.append((byte) b); } public void write(byte[] b) throws IOException { buf.append(b); } public void write(byte[] b, int off, int len) throws IOException { buf.append(b, off, len); } public void writeByte(byte b) { buf.append(b); } public void writeShort(int s) { while ((s & ~0177) != 0) { buf.append((byte) ((s & 0177) | 0200)); s >>>= 7; } buf.append((byte) s); } public void writeInt(int i) { while ((i & ~0177) != 0) { buf.append((byte) ((i & 0177) | 0200)); i >>>= 7; } buf.append((byte) i); } public void writeLong(long l) { while ((l & ~0177) != 0) { buf.append((byte) ((l & 0177) | 0200)); l >>>= 7; } buf.append((byte) l); } public void writeFixedLong(long l) { buf.append((byte) ((l >>> 56) & 0xff)); buf.append((byte) ((l >>> 48) & 0xff)); buf.append((byte) ((l >>> 40) & 0xff)); buf.append((byte) ((l >>> 32) & 0xff)); buf.append((byte) ((l >>> 24) & 0xff)); buf.append((byte) ((l >>> 16) & 0xff)); buf.append((byte) ((l >>> 8) & 0xff)); buf.append((byte) ((l >>> 0) & 0xff)); } public void writeUTF(String s) throws IOException { byte[] data = null; try { data = s.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { data = s.getBytes(); } writeInt(data.length); write(data, 0, data.length); } } --- NEW FILE: VariableByteArrayInput.java --- /* * eXist Open Source Native XML Database Copyright (C) 2001, Wolfgang M. Meier * (me...@if...) * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Library General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) any * later version. * * This library 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 Library General Public License for more * details. * * You should have received a copy of the GNU Library 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. * * $Id: VariableByteArrayInput.java,v 1.1 2004/05/03 13:01:34 wolfgang_m Exp $ */ package org.exist.storage.io; import java.io.EOFException; import java.io.IOException; /** * Implements VariableByteInput on top of a byte array. * * @author wolf */ public class VariableByteArrayInput extends AbstractVariableByteInput { private byte[] data; private int position; private int end; public VariableByteArrayInput() { super(); } public VariableByteArrayInput(byte[] data) { super(); this.data = data; this.position = 0; this.end = data.length; } public VariableByteArrayInput(byte[] data, int offset, int length) { super(); this.data = data; this.position = offset; this.end = offset + length; } public void initialize(byte[] data, int offset, int length) { this.data = data; this.position = offset; this.end = offset + length; } public byte readByte() throws IOException, EOFException { if (position == end) throw new EOFException(); return data[position++]; } /* * (non-Javadoc) * * @see java.io.InputStream#read() */ public int read() throws IOException { if (position == end) return -1; return data[position++] & 0xFF; } /* * (non-Javadoc) * * @see java.io.InputStream#available() */ public int available() throws IOException { return end - position; } public short readShort() throws IOException { if (position == end) throw new EOFException(); byte b = data[position++]; short i = (short) (b & 0177); for (int shift = 7; (b & 0200) != 0; shift += 7) { if (position == end) throw new EOFException(); b = data[position++]; i |= (b & 0177) << shift; } return i; } public int readInt() throws IOException { if (position == end) throw new EOFException(); byte b = data[position++]; int i = b & 0177; for (int shift = 7; (b & 0200) != 0; shift += 7) { if (position == end) throw new EOFException(); b = data[position++]; i |= (b & 0177) << shift; } return i; } public long readLong() throws IOException { if (position == end) throw new EOFException(); byte b = data[position++]; long i = b & 0177L; for (int shift = 7; (b & 0200) != 0; shift += 7) { if (position == end) throw new EOFException(); b = data[position++]; i |= (b & 0177L) << shift; } return i; } public void copyTo(VariableByteOutputStream os, int count) throws IOException { byte more; for (int i = 0; i < count; i++) { do { more = data[position++]; os.buf.append(more); } while ((more & 0x200) > 0); } } public void skip(int count) throws IOException { for (int i = 0; i < count; i++) { while (position < end && (data[position++] & 0200) > 0) ; } } } --- NEW FILE: VariableByteInputStream.java --- /* * eXist Open Source Native XML Database * Copyright (C) 2001-04 Wolfgang M. Meier * wol...@ex... * http://exist-db.org * * This program 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. * * 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: VariableByteInputStream.java,v 1.1 2004/05/03 13:01:34 wolfgang_m Exp $ */ package org.exist.storage.io; import java.io.IOException; import java.io.InputStream; /** * Implements VariableByteInput on top of an InputStream. * * @author wolf */ public class VariableByteInputStream extends AbstractVariableByteInput { private InputStream is; /** * */ public VariableByteInputStream(InputStream is) { super(); this.is = is; } /* (non-Javadoc) * @see java.io.InputStream#read() */ public int read() throws IOException { return is.read(); } /* (non-Javadoc) * @see java.io.InputStream#available() */ public int available() throws IOException { return is.available(); } } --- NEW FILE: AbstractVariableByteInput.java --- /* * eXist Open Source Native XML Database Copyright (C) 2001-04, Wolfgang M. * Meier (me...@if...) * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Library General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) any * later version. * * This library 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 Library General Public License for more * details. * * You should have received a copy of the GNU Library 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. * * $Id: AbstractVariableByteInput.java,v 1.1 2004/05/03 13:01:34 wolfgang_m Exp $ */ package org.exist.storage.io; import java.io.EOFException; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * Abstract base class for implementations of VariableByteInput. * * @author wolf */ public abstract class AbstractVariableByteInput implements VariableByteInput { public AbstractVariableByteInput() { } public abstract int available() throws IOException; public abstract int read() throws IOException; public byte readByte() throws IOException { final int i = read(); if (i < 0) throw new EOFException(); return (byte) i; } public short readShort() throws IOException { byte b = readByte(); short i = (short) (b & 0177); for (int shift = 7; (b & 0200) != 0; shift += 7) { b = readByte(); i |= (b & 0177L) << shift; } return i; } public int readInt() throws IOException { byte b = readByte(); int i = b & 0177; for (int shift = 7; (b & 0200) != 0; shift += 7) { b = readByte(); i |= (b & 0177L) << shift; } return i; } public long readLong() throws IOException { byte b = readByte(); long i = b & 0177; for (int shift = 7; (b & 0200) != 0; shift += 7) { b = readByte(); i |= (b & 0177L) << shift; } return i; } public String readUTF() throws IOException, EOFException { int len = readInt(); byte data[] = new byte[len]; read(data); String s; try { s = new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { s = new String(data); } return s; } public void skip(int count) throws IOException { for (int i = 0; i < count && available() > 0; i++) { while ((readByte() & 0200) > 0) ; } } public int read(byte[] data) throws IOException { return read(data, 0, data.length); } public int read(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int c = read(); if (c == -1) { return -1; } b[off] = (byte) c; int i = 1; try { for (; i < len; i++) { c = read(); if (c == -1) { break; } if (b != null) { b[off + i] = (byte) c; } } } catch (IOException ee) { } return i; } public void copyTo(VariableByteOutputStream os) throws IOException { int more; do { more = read(); os.buf.append((byte) more); more &= 0200; } while (more > 0); } public void copyTo(VariableByteOutputStream os, int count) throws IOException { int more; for (int i = 0; i < count; i++) { do { more = read(); os.buf.append((byte)more); more &= 0200; } while (more > 0); } } /* (non-Javadoc) * @see org.exist.storage.io.VariableByteInput#release() */ public void release() { } } --- NEW FILE: VariableByteInput.java --- /* * eXist Open Source Native XML Database * Copyright (C) 2001-04 Wolfgang M. Meier * wol...@ex... * http://exist-db.org * * This program 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. * * 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: VariableByteInput.java,v 1.1 2004/05/03 13:01:34 wolfgang_m Exp $ */ package org.exist.storage.io; import java.io.EOFException; import java.io.IOException; /** * Interface for reading variable byte encoded values. * * Variable byte encoding offers a good compression ratio if the stored * values are rather small, i.e. much smaller than the possible maximum for * the given type. * * @author wolf */ public interface VariableByteInput { /** * Read a single byte and return as an int value. * * @return the byte value as int or -1 if no more bytes are available. * @throws IOException */ public int read() throws IOException; /** * Fill the provided byte array with data from the input. * * @param data * @return * @throws IOException */ public int read(byte[] data) throws IOException; public int read(byte b[], int off, int len) throws IOException; /** * Returns a value > 0 if more bytes can be read * from the input. * * @return * @throws IOException */ public int available() throws IOException; /** * Read a single byte. Throws EOFException if no * more bytes are available. * * @return * @throws IOException */ public byte readByte() throws IOException; /** * Read a short value in variable byte encoding. * * @return * @throws IOException */ public short readShort() throws IOException; /** * Read an integer value in variable byte encoding. * * @return * @throws IOException */ public int readInt() throws IOException; /** * Read a long value in variable byte encoding. * * @return * @throws IOException */ public long readLong() throws IOException; public String readUTF() throws IOException, EOFException; /** * Read the following count numeric values from the input * and drop them. * * @param count * @throws IOException */ public void skip(int count) throws IOException; /** * Copy the next numeric value from the input to the * specified output stream. * * @param os * @throws IOException */ public void copyTo(VariableByteOutputStream os) throws IOException; /** * Copy the count next numeric values from the input to * the specified output stream. * * @param os * @param count * @throws IOException */ public void copyTo(VariableByteOutputStream os, int count) throws IOException; } |