comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ByteArrayRepresentation.java @ 380:9798e4d83681

Services are now able to return more than just XML documents. artifacts/trunk@4188 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 Apr 2012 11:15:10 +0000
parents
children
comparison
equal deleted inserted replaced
379:95ff5a25a339 380:9798e4d83681
1 package de.intevation.artifactdatabase.rest;
2
3 import org.restlet.representation.Representation;
4
5 import java.io.Reader;
6 import java.io.OutputStream;
7 import java.io.InputStream;
8 import java.io.Writer;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.ByteArrayInputStream;
12
13 import java.nio.ByteBuffer;
14
15 import java.nio.channels.ReadableByteChannel;
16 import java.nio.channels.WritableByteChannel;
17
18 import org.restlet.data.MediaType;
19
20 public class ByteArrayRepresentation
21 extends Representation
22 {
23 protected byte [] data;
24
25 public ByteArrayRepresentation(MediaType mediaType, byte [] data) {
26 super(mediaType);
27 this.data = data;
28 }
29
30 @Override
31 public long getSize() {
32 return data.length;
33 }
34
35 @Override
36 public ReadableByteChannel getChannel() throws IOException {
37 return null;
38 }
39
40 @Override
41 public Reader getReader() throws IOException {
42 return new InputStreamReader(getStream());
43 }
44
45 @Override
46 public InputStream getStream() throws IOException {
47 return new ByteArrayInputStream(data);
48 }
49
50 @Override
51 public void write(Writer writer) throws IOException {
52 writer.append(ByteBuffer.wrap(data).asCharBuffer());
53 }
54
55 @Override
56 public void write(WritableByteChannel writableChannel) throws IOException {
57 writableChannel.write(ByteBuffer.wrap(data));
58 }
59
60 @Override
61 public void write(OutputStream outputStream) throws IOException {
62 outputStream.write(data);
63 }
64 }
65 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org