Skip to content
Snippets Groups Projects
Unverified Commit d512c218 authored by akwizgran's avatar akwizgran
Browse files

Removed consumers from BdfWriter.

parent d69a242f
No related branches found
No related tags found
No related merge requests found
......@@ -9,9 +9,6 @@ public interface BdfWriter {
void flush() throws IOException;
void close() throws IOException;
void addConsumer(Consumer c);
void removeConsumer(Consumer c);
void writeNull() throws IOException;
void writeBoolean(boolean b) throws IOException;
void writeInteger(long l) throws IOException;
......
package org.briarproject.api.data;
import java.io.IOException;
public interface Consumer {
void write(byte b) throws IOException;
void write(byte[] b, int off, int len) throws IOException;
}
......@@ -3,11 +3,9 @@ package org.briarproject.data;
import org.briarproject.api.Bytes;
import org.briarproject.api.FormatException;
import org.briarproject.api.data.BdfWriter;
import org.briarproject.api.data.Consumer;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
......@@ -35,7 +33,6 @@ import static org.briarproject.data.Types.TRUE;
class BdfWriterImpl implements BdfWriter {
private final OutputStream out;
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
BdfWriterImpl(OutputStream out) {
this.out = out;
......@@ -49,100 +46,92 @@ class BdfWriterImpl implements BdfWriter {
out.close();
}
public void addConsumer(Consumer c) {
consumers.add(c);
}
public void removeConsumer(Consumer c) {
if (!consumers.remove(c)) throw new IllegalArgumentException();
}
public void writeNull() throws IOException {
write(NULL);
out.write(NULL);
}
public void writeBoolean(boolean b) throws IOException {
if (b) write(TRUE);
else write(FALSE);
if (b) out.write(TRUE);
else out.write(FALSE);
}
public void writeInteger(long i) throws IOException {
if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
write(INT_8);
write((byte) i);
out.write(INT_8);
out.write((byte) i);
} else if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
write(INT_16);
out.write(INT_16);
writeInt16((short) i);
} else if (i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) {
write(INT_32);
out.write(INT_32);
writeInt32((int) i);
} else {
write(INT_64);
out.write(INT_64);
writeInt64(i);
}
}
private void writeInt16(short i) throws IOException {
write((byte) (i >> 8));
write((byte) ((i << 8) >> 8));
out.write((byte) (i >> 8));
out.write((byte) ((i << 8) >> 8));
}
private void writeInt32(int i) throws IOException {
write((byte) (i >> 24));
write((byte) ((i << 8) >> 24));
write((byte) ((i << 16) >> 24));
write((byte) ((i << 24) >> 24));
out.write((byte) (i >> 24));
out.write((byte) ((i << 8) >> 24));
out.write((byte) ((i << 16) >> 24));
out.write((byte) ((i << 24) >> 24));
}
private void writeInt64(long i) throws IOException {
write((byte) (i >> 56));
write((byte) ((i << 8) >> 56));
write((byte) ((i << 16) >> 56));
write((byte) ((i << 24) >> 56));
write((byte) ((i << 32) >> 56));
write((byte) ((i << 40) >> 56));
write((byte) ((i << 48) >> 56));
write((byte) ((i << 56) >> 56));
out.write((byte) (i >> 56));
out.write((byte) ((i << 8) >> 56));
out.write((byte) ((i << 16) >> 56));
out.write((byte) ((i << 24) >> 56));
out.write((byte) ((i << 32) >> 56));
out.write((byte) ((i << 40) >> 56));
out.write((byte) ((i << 48) >> 56));
out.write((byte) ((i << 56) >> 56));
}
public void writeFloat(double d) throws IOException {
write(FLOAT_64);
out.write(FLOAT_64);
writeInt64(Double.doubleToRawLongBits(d));
}
public void writeString(String s) throws IOException {
byte[] b = s.getBytes("UTF-8");
if (b.length <= Byte.MAX_VALUE) {
write(STRING_8);
write((byte) b.length);
out.write(STRING_8);
out.write((byte) b.length);
} else if (b.length <= Short.MAX_VALUE) {
write(STRING_16);
out.write(STRING_16);
writeInt16((short) b.length);
} else {
write(STRING_32);
out.write(STRING_32);
writeInt32(b.length);
}
write(b);
out.write(b);
}
public void writeRaw(byte[] b) throws IOException {
if (b.length <= Byte.MAX_VALUE) {
write(RAW_8);
write((byte) b.length);
out.write(RAW_8);
out.write((byte) b.length);
} else if (b.length <= Short.MAX_VALUE) {
write(RAW_16);
out.write(RAW_16);
writeInt16((short) b.length);
} else {
write(RAW_32);
out.write(RAW_32);
writeInt32(b.length);
}
write(b);
out.write(b);
}
public void writeList(Collection<?> c) throws IOException {
write(LIST);
out.write(LIST);
for (Object o : c) writeObject(o);
write(END);
out.write(END);
}
private void writeObject(Object o) throws IOException {
......@@ -163,38 +152,28 @@ class BdfWriterImpl implements BdfWriter {
}
public void writeListStart() throws IOException {
write(LIST);
out.write(LIST);
}
public void writeListEnd() throws IOException {
write(END);
out.write(END);
}
public void writeDictionary(Map<?, ?> m) throws IOException {
write(DICTIONARY);
out.write(DICTIONARY);
for (Entry<?, ?> e : m.entrySet()) {
if (!(e.getKey() instanceof String)) throw new FormatException();
writeString((String) e.getKey());
writeObject(e.getValue());
}
write(END);
out.write(END);
}
public void writeDictionaryStart() throws IOException {
write(DICTIONARY);
out.write(DICTIONARY);
}
public void writeDictionaryEnd() throws IOException {
write(END);
}
private void write(byte b) throws IOException {
out.write(b);
for (Consumer c : consumers) c.write(b);
}
private void write(byte[] b) throws IOException {
out.write(b);
for (Consumer c : consumers) c.write(b, 0, b.length);
out.write(END);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment