Merge branch 'release-0.8.9'
[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 com.db4o.ObjectContainer;
26
27 import freenet.support.api.Bucket;
28
29 /**
30  * {@link Bucket} implementation wrapped around a {@link String}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class StringBucket implements Bucket {
35
36         /** The string to deliver. */
37         private final String string;
38
39         /** The encoding for the data. */
40         private final Charset encoding;
41
42         /**
43          * Creates a new string bucket using the default encoding.
44          *
45          * @param string
46          *            The string to wrap
47          */
48         public StringBucket(String string) {
49                 this(string, Charset.defaultCharset());
50         }
51
52         /**
53          * Creates a new string bucket, using the given encoding to create a byte
54          * array from the string.
55          *
56          * @param string
57          *            The string to wrap
58          * @param encoding
59          *            The encoding of the data
60          */
61         public StringBucket(String string, Charset encoding) {
62                 this.string = string;
63                 this.encoding = encoding;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         public Bucket createShadow() {
71                 return new StringBucket(string);
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public void free() {
79                 /* ignore. */
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public InputStream getInputStream() {
87                 return new ByteArrayInputStream(string.getBytes(encoding));
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         public String getName() {
95                 return getClass().getName() + "@" + hashCode();
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         public OutputStream getOutputStream() {
103                 return null;
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         @Override
110         public boolean isReadOnly() {
111                 return true;
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         @Override
118         public void removeFrom(ObjectContainer objectContainer) {
119                 /* ignore. */
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public void setReadOnly() {
127                 /* ignore, it is already read-only. */
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public long size() {
135                 return string.getBytes(encoding).length;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public void storeTo(ObjectContainer objectContainer) {
143                 /* ignore. */
144         }
145
146 }