View Javadoc
1   package org.argeo.jcr;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import javax.jcr.Binary;
7   import javax.jcr.Property;
8   import javax.jcr.RepositoryException;
9   
10  /**
11   * A {@link Binary} wrapper implementing {@link AutoCloseable} for ease of use
12   * in try/catch blocks.
13   */
14  public class Bin implements Binary, AutoCloseable {
15  	private final Binary wrappedBinary;
16  
17  	public Bin(Property property) throws RepositoryException {
18  		this(property.getBinary());
19  	}
20  
21  	public Bin(Binary wrappedBinary) {
22  		if (wrappedBinary == null)
23  			throw new IllegalArgumentException("Wrapped binary cannot be null");
24  		this.wrappedBinary = wrappedBinary;
25  	}
26  
27  	// private static Binary getBinary(Property property) throws IOException {
28  	// try {
29  	// return property.getBinary();
30  	// } catch (RepositoryException e) {
31  	// throw new IOException("Cannot get binary from property " + property, e);
32  	// }
33  	// }
34  
35  	@Override
36  	public void close() {
37  		dispose();
38  	}
39  
40  	@Override
41  	public InputStream getStream() throws RepositoryException {
42  		return wrappedBinary.getStream();
43  	}
44  
45  	@Override
46  	public int read(byte[] b, long position) throws IOException, RepositoryException {
47  		return wrappedBinary.read(b, position);
48  	}
49  
50  	@Override
51  	public long getSize() throws RepositoryException {
52  		return wrappedBinary.getSize();
53  	}
54  
55  	@Override
56  	public void dispose() {
57  		wrappedBinary.dispose();
58  	}
59  
60  }