You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Ted Roeloffzen <te...@gmail.com> on 2006/11/28 10:12:44 UTC
Retrieving binary content
Hi all,
I have a question. In my application I have a node named Plugin with a
reference to a node named Image. The image node contains an image. Now in my
application i retrieve the Plugin node, but i can't figure out how to get
the node that it has a reference to and than how to retrieve the image it
contains. Can anybody help me with that?
Thanks,
Ted
Re: Retrieving binary content
Posted by Ted Roeloffzen <te...@gmail.com>.
Thanks al lot.
Ted
Re: Retrieving binary content
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 11/28/06, Ted Roeloffzen <te...@gmail.com> wrote:
> > A common pattern I use when processing stream data is:
> >
> > InputStream data = ...;
> > try {
> > byte[] buffer = new byte[BUFFER_SIZE];
> > for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
> > // process n bytes of data in buffer
> > }
> > } finally {
> > data.close();
> > }
>
> When I do this, i need to know how large the buffer needs to be, but i don't
> know how big it is. Is there a way to go around that?
The for loop is designed to read the stream in blocks. For example, you can use:
private static final int BUFFER_SIZE = 1000;
... in which case the stream is read in blocks of up to a thousand
bytes. Each iteration of the for loop body will process a block of n
bytes stored in the allocated buffer.
For example if you want to store the image in a file or send it over
the network, you can do that by by writing the byte blocks to a
respective OutputStream, like this:
InputStream data = ...;
try {
OutputStream destination = ...;
try {
byte[] buffer = new byte[BUFFER_SIZE];
for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
destination.write(buffer, 0, n);
}
} finally {
destination.close();
}
} finally {
data.close();
}
If you need the whole binary stream available in memory as a byte
array, then you can use a ByteArrayOutputStream to accumulate the
data:
OutputStream destination = new ByteArrayOutputStream();
InputStream data = ...;
try {
byte[] buffer = new byte[BUFFER_SIZE];
for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
destination.write(buffer, 0, n);
}
} finally {
data.close();
}
byte[] bytes = destination.toByteArray();
I hope this helps! See
http://java.sun.com/docs/books/tutorial/essential/io/bytestreams.html
and the related documentation for more information.
BR,
Jukka Zitting
Re: Retrieving binary content
Posted by Ted Roeloffzen <te...@gmail.com>.
>
>
>
> A common pattern I use when processing stream data is:
>
> InputStream data = ...;
> try {
> byte[] buffer = new byte[BUFFER_SIZE];
> for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
> // process n bytes of data in buffer
> }
> } finally {
> data.close();
}
When I do this, i need to know how large the buffer needs to be, but i don't
know how big it is.
Is there a way to go around that?
Ted
Re: Retrieving binary content
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 11/28/06, Ted Roeloffzen <te...@gmail.com> wrote:
> And how do I read the actual data with that inputstream?
It's a standard java.io.InputStream. See the javadocs for example at
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html.
A common pattern I use when processing stream data is:
InputStream data = ...;
try {
byte[] buffer = new byte[BUFFER_SIZE];
for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
// process n bytes of data in buffer
}
} finally {
data.close();
}
Note especially the finally branch used to guarantee that the stream
gets closed even if an error occurs.
> Sorry if this is a stupid question, but i'm not that experienced.
No problem, stupid is the one who doesn't ask. :-)
BR,
Jukka Zitting
Re: Retrieving binary content
Posted by Ted Roeloffzen <te...@gmail.com>.
And how do I read the actual data with that inputstream?
Sorry if this is a stupid question, but i'm not that experienced.
Ted
2006/11/28, Jukka Zitting <ju...@gmail.com>:
>
> Hi,
>
> On 11/28/06, Ted Roeloffzen <te...@gmail.com> wrote:
> > I have a question. In my application I have a node named Plugin with a
> > reference to a node named Image. The image node contains an image. Now
> in my
> > application i retrieve the Plugin node, but i can't figure out how to
> get
> > the node that it has a reference to and than how to retrieve the image
> it
> > contains. Can anybody help me with that?
>
> Something like this should work:
>
> Node plugin = ...;
> Node image = plugin.getProperty("...").getNode();
> InputStream data = image.getProperty("...").getStream();
>
> BR,
>
> Jukka Zitting
>
Re: Retrieving binary content
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 11/28/06, Ted Roeloffzen <te...@gmail.com> wrote:
> I have a question. In my application I have a node named Plugin with a
> reference to a node named Image. The image node contains an image. Now in my
> application i retrieve the Plugin node, but i can't figure out how to get
> the node that it has a reference to and than how to retrieve the image it
> contains. Can anybody help me with that?
Something like this should work:
Node plugin = ...;
Node image = plugin.getProperty("...").getNode();
InputStream data = image.getProperty("...").getStream();
BR,
Jukka Zitting