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 freenet.support.api.Bucket;
26 import com.db4o.ObjectContainer;
29 * {@link Bucket} implementation wrapped around a {@link String}.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class StringBucket implements Bucket {
35 /** The string to deliver. */
36 private final String string;
38 /** The encoding for the data. */
39 private final Charset encoding;
42 * Creates a new string bucket using the default encoding.
47 public StringBucket(String string) {
48 this(string, Charset.defaultCharset());
52 * Creates a new string bucket, using the given encoding to create a byte
53 * array from the string.
58 * The encoding of the data
60 public StringBucket(String string, Charset encoding) {
62 this.encoding = encoding;
66 public Bucket createShadow() {
67 return new StringBucket(string);
76 public InputStream getInputStream() {
77 return new ByteArrayInputStream(string.getBytes(encoding));
81 public String getName() {
82 return getClass().getName() + "@" + hashCode();
86 public OutputStream getOutputStream() {
91 public boolean isReadOnly() {
96 public void removeFrom(ObjectContainer objectContainer) {
101 public void setReadOnly() {
102 /* ignore, it is already read-only. */
107 return string.getBytes(encoding).length;
111 public void storeTo(ObjectContainer objectContainer) {