From: David ‘Bombe’ Roden Date: Wed, 13 Oct 2010 19:28:24 +0000 (+0200) Subject: Add String-based Bucket implementation. X-Git-Tag: 0.1-RC1~451 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e812dbaa796c92dc48a77201b5b403674bec5c83;p=Sone.git Add String-based Bucket implementation. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java b/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java new file mode 100644 index 0000000..2e2a102 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java @@ -0,0 +1,147 @@ +/* + * FreenetSone - TemplateBucket.java - Copyright © 2010 David Roden + * + * 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 3 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, see . + */ + +package net.pterodactylus.sone.freenet; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; + +import com.db4o.ObjectContainer; + +import freenet.support.api.Bucket; + +/** + * {@link Bucket} implementation wrapped around a {@link String}. + * + * @author David ‘Bombe’ Roden + */ +public class StringBucket implements Bucket { + + /** The string to deliver. */ + private final String string; + + /** The encoding for the data. */ + private final Charset encoding; + + /** + * Creates a new string bucket using the default encoding. + * + * @param string + * The string to wrap + */ + public StringBucket(String string) { + this(string, Charset.defaultCharset()); + } + + /** + * Creates a new string bucket, using the given encoding to create a byte + * array from the string. + * + * @param string + * The string to wrap + * @param encoding + * The encoding of the data + */ + public StringBucket(String string, Charset encoding) { + this.string = string; + this.encoding = encoding; + } + + /** + * {@inheritDoc} + */ + @Override + public Bucket createShadow() throws IOException { + return new StringBucket(string); + } + + /** + * {@inheritDoc} + */ + @Override + public void free() { + /* ignore. */ + } + + /** + * {@inheritDoc} + */ + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(string.getBytes(encoding)); + } + + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return getClass().getName() + "@" + hashCode(); + } + + /** + * {@inheritDoc} + */ + @Override + public OutputStream getOutputStream() throws IOException { + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isReadOnly() { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public void removeFrom(ObjectContainer objectContainer) { + /* ignore. */ + } + + /** + * {@inheritDoc} + */ + @Override + public void setReadOnly() { + /* ignore, it is already read-only. */ + } + + /** + * {@inheritDoc} + */ + @Override + public long size() { + return string.getBytes(encoding).length; + } + + /** + * {@inheritDoc} + */ + @Override + public void storeTo(ObjectContainer objectContainer) { + /* ignore. */ + } + +}