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