2 * Sone - StringBucket.java - Copyright © 2010–2013 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.InputStream;
22 import java.io.OutputStream;
23 import java.nio.charset.Charset;
25 import com.db4o.ObjectContainer;
27 import freenet.support.api.Bucket;
30 * {@link Bucket} implementation wrapped around a {@link String}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class StringBucket implements Bucket {
36 /** The string to deliver. */
37 private final String string;
39 /** The encoding for the data. */
40 private final Charset encoding;
43 * Creates a new string bucket using the default encoding.
48 public StringBucket(String string) {
49 this(string, Charset.defaultCharset());
53 * Creates a new string bucket, using the given encoding to create a byte
54 * array from the string.
59 * The encoding of the data
61 public StringBucket(String string, Charset encoding) {
63 this.encoding = encoding;
70 public Bucket createShadow() {
71 return new StringBucket(string);
86 public InputStream getInputStream() {
87 return new ByteArrayInputStream(string.getBytes(encoding));
94 public String getName() {
95 return getClass().getName() + "@" + hashCode();
102 public OutputStream getOutputStream() {
110 public boolean isReadOnly() {
118 public void removeFrom(ObjectContainer objectContainer) {
126 public void setReadOnly() {
127 /* ignore, it is already read-only. */
135 return string.getBytes(encoding).length;
142 public void storeTo(ObjectContainer objectContainer) {