Permalink
Browse files

First crack at a stream encoder

  • Loading branch information...
djspiewak committed Mar 19, 2008
1 parent 4b3a685 commit e1930d34bfa2beb99a52db6e38fc2ebad877c4be
@@ -0,0 +1,14 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public interface Type {
public void write(OutputStream os) throws IOException;
}
@@ -0,0 +1,28 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.composite;
import java.io.IOException;
import java.io.OutputStream;
import com.googlecode.jbencode.Type;
/**
* @author Daniel Spiewak
*/
public abstract class CompositeType implements Type {
private final char prefix;
public CompositeType(char prefix) {
this.prefix = prefix;
}
public final void write(OutputStream os) throws IOException {
os.write(prefix);
writeValue(os);
os.write('e');
}
protected abstract void writeValue(OutputStream os) throws IOException;
}
@@ -0,0 +1,32 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.composite;
import java.io.IOException;
import java.io.OutputStream;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* @author Daniel Spiewak
*/
public abstract class DictionaryType extends CompositeType {
public DictionaryType() {
super('d');
}
@Override
protected final void writeValue(OutputStream os) throws IOException {
final SortedSet<Key<?>> keys = new TreeSet<Key<?>>();
populate(keys);
for (Key<?> key : keys) {
key.write(os);
}
}
protected abstract void populate(SortedSet<Key<?>> keys);
}
@@ -0,0 +1,32 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.composite;
import java.io.IOException;
import java.io.OutputStream;
import com.googlecode.jbencode.Type;
import com.googlecode.jbencode.primitive.StringType;
/**
* @author Daniel Spiewak
*/
public final class Key<T extends StringType & Comparable<T>> implements Type, Comparable<Key<T>> {
private final T key;
private final Type value;
public Key(T key, Type value) {
this.key = key;
this.value = value;
}
public void write(OutputStream os) throws IOException {
key.write(os);
value.write(os);
}
public int compareTo(Key<T> o) {
return o.key.compareTo(key);
}
}
@@ -0,0 +1,24 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.composite;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public abstract class ListType extends CompositeType {
public ListType() {
super('l');
}
@Override
protected final void writeValue(OutputStream os) throws IOException {
populate(new ListValueStream(os));
}
protected abstract void populate(ListValueStream list) throws IOException;
}
@@ -0,0 +1,24 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.composite;
import java.io.IOException;
import java.io.OutputStream;
import com.googlecode.jbencode.Type;
/**
* @author Daniel Spiewak
*/
public final class ListValueStream {
private final OutputStream os;
ListValueStream(OutputStream os) {
this.os = os;
}
public void add(Type value) throws IOException {
value.write(os);
}
}
@@ -0,0 +1,25 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.primitive;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public class IntegerType extends InvariantPrimitiveType {
private final long value;
public IntegerType(long value) {
super('i');
this.value = value;
}
@Override
protected void writeValue(OutputStream os) throws IOException {
os.write(Long.toString(value).getBytes());
}
}
@@ -0,0 +1,28 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.primitive;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public abstract class InvariantPrimitiveType extends PrimitiveType {
private final char prefix;
public InvariantPrimitiveType(char prefix) {
this.prefix = prefix;
}
@Override
protected final void writePrefix(OutputStream os) throws IOException {
os.write(prefix);
}
@Override
protected final void writeSuffix(OutputStream os) throws IOException {
os.write('e');
}
}
@@ -0,0 +1,27 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.primitive;
import java.io.IOException;
import java.io.OutputStream;
import com.googlecode.jbencode.Type;
/**
* @author Daniel Spiewak
*/
public abstract class PrimitiveType implements Type {
public PrimitiveType() {}
public final void write(OutputStream os) throws IOException {
writePrefix(os);
writeValue(os);
writeSuffix(os);
}
protected abstract void writePrefix(OutputStream os) throws IOException;
protected abstract void writeValue(OutputStream os) throws IOException;
protected abstract void writeSuffix(OutputStream os) throws IOException;
}
@@ -0,0 +1,22 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.primitive;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public abstract class StringType extends VariantPrimitiveType {
public StringType() {
super(' ');
}
@Override
protected final void writePrefix(OutputStream os) throws IOException {
writeLength(os);
}
}
@@ -0,0 +1,33 @@
/*
* Created on Mar 19, 2008
*/
package com.googlecode.jbencode.primitive;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Daniel Spiewak
*/
public abstract class VariantPrimitiveType extends PrimitiveType {
private final char prefix;
public VariantPrimitiveType(char prefix) {
this.prefix = prefix;
}
protected abstract long getLength();
@Override
protected void writePrefix(OutputStream os) throws IOException {
os.write(prefix);
}
protected final void writeLength(OutputStream os) throws IOException {
os.write(Long.toString(getLength()).getBytes());
os.write(':');
}
@Override
protected final void writeSuffix(OutputStream os) throws IOException {}
}

0 comments on commit e1930d3

Please sign in to comment.