X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FStringBucket.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FStringBucket.java;h=2e2a102f0007d2407da234870d98f7d558dd8ed8;hb=e812dbaa796c92dc48a77201b5b403674bec5c83;hp=0000000000000000000000000000000000000000;hpb=80b69bbf2b53e5587088a1e8b8764aeb92e1c1ae;p=Sone.git
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. */
+ }
+
+}