Fix shadow creation.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / StringBucket.java
1 /*
2  * Sone - StringBucket.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.charset.Charset;
24
25 import freenet.support.api.Bucket;
26 import com.db4o.ObjectContainer;
27
28 /**
29  * {@link Bucket} implementation wrapped around a {@link String}.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class StringBucket implements Bucket {
34
35         /** The string to deliver. */
36         private final String string;
37
38         /** The encoding for the data. */
39         private final Charset encoding;
40
41         /**
42          * Creates a new string bucket using the default encoding.
43          *
44          * @param string
45          *            The string to wrap
46          */
47         public StringBucket(String string) {
48                 this(string, Charset.defaultCharset());
49         }
50
51         /**
52          * Creates a new string bucket, using the given encoding to create a byte
53          * array from the string.
54          *
55          * @param string
56          *            The string to wrap
57          * @param encoding
58          *            The encoding of the data
59          */
60         public StringBucket(String string, Charset encoding) {
61                 this.string = string;
62                 this.encoding = encoding;
63         }
64
65         @Override
66         public Bucket createShadow() {
67                 return new StringBucket(string, encoding);
68         }
69
70         @Override
71         public void free() {
72                 /* ignore. */
73         }
74
75         @Override
76         public InputStream getInputStream() {
77                 return new ByteArrayInputStream(string.getBytes(encoding));
78         }
79
80         @Override
81         public String getName() {
82                 return getClass().getName() + "@" + hashCode();
83         }
84
85         @Override
86         public OutputStream getOutputStream() {
87                 return null;
88         }
89
90         @Override
91         public boolean isReadOnly() {
92                 return true;
93         }
94
95         @Override
96         public void removeFrom(ObjectContainer objectContainer) {
97                 /* ignore. */
98         }
99
100         @Override
101         public void setReadOnly() {
102                 /* ignore, it is already read-only. */
103         }
104
105         @Override
106         public long size() {
107                 return string.getBytes(encoding).length;
108         }
109
110         @Override
111         public void storeTo(ObjectContainer objectContainer) {
112                 /* ignore. */
113         }
114
115 }