2 * Sone - StringBucket.java - Copyright © 2010–2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.nio.charset.Charset;
26 import com.db4o.ObjectContainer;
28 import freenet.support.api.Bucket;
31 * {@link Bucket} implementation wrapped around a {@link String}.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class StringBucket implements Bucket {
37 /** The string to deliver. */
38 private final String string;
40 /** The encoding for the data. */
41 private final Charset encoding;
44 * Creates a new string bucket using the default encoding.
49 public StringBucket(String string) {
50 this(string, Charset.defaultCharset());
54 * Creates a new string bucket, using the given encoding to create a byte
55 * array from the string.
60 * The encoding of the data
62 public StringBucket(String string, Charset encoding) {
64 this.encoding = encoding;
71 public Bucket createShadow() {
72 return new StringBucket(string);
87 public InputStream getInputStream() throws IOException {
88 return new ByteArrayInputStream(string.getBytes(encoding));
95 public String getName() {
96 return getClass().getName() + "@" + hashCode();
103 public OutputStream getOutputStream() throws IOException {
111 public boolean isReadOnly() {
119 public void removeFrom(ObjectContainer objectContainer) {
127 public void setReadOnly() {
128 /* ignore, it is already read-only. */
136 return string.getBytes(encoding).length;
143 public void storeTo(ObjectContainer objectContainer) {