Debian Codegraph @

net/sf/saxon/resource/BinaryResource.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.resource;
9
10
11 import net.sf.saxon.Configuration;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.lib.Resource;
14 import net.sf.saxon.lib.ResourceFactory;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.Base64BinaryValue;
18
19 import java.io.*;
20 import java.net.URLConnection;
21
22
23 public class BinaryResource implements Resource {
24
25 private String href = null;
26 private String contentType = null;
27 private byte[] data;
28 private URLConnection connection = null;
29 private InputStream inputStream = null;
30
31 /**
32 * ResourceFactory suitable for creating a BinaryResource
33 */
34
35 public static final ResourceFactory FACTORY = new ResourceFactory() {
36 public Resource makeResource(Configuration config, String resourceURI, String contentType, AbstractResourceCollection.InputDetails details) throws XPathException {
37 return new BinaryResource(resourceURI, contentType, details.inputStream);
38 }
39 };
40
41 /**
42 * Create a binary resource
43 *
44 * @param href the URI of the resource
45 * @param contentType the media type of the resource
46 * @param in inputStream containing the binary content of the resource. Note that the InputStream
47 * is not consumed by this method, but is retained for use by the getItem() method.
48 */
49
50 public BinaryResource(String href, String contentType, InputStream in) {
51 this.contentType = contentType;
52 this.href = href;
53 this.inputStream = in;
54 }
55
56 /**
57 * Set the content of the resource as an array of bytes
58 *
59 * @param data the content of the resource
60 */
61
62 public void setData(byte[] data) {
63 this.data = data;
64 }
65
66 /**
67 * Get the content of the resource as an array of bytes
68 * @return the content (if it has been set using setData())
69 */
70
71 public byte[] getData() {
72 return data;
73 }
74
75 /**
76 * Get the URI of the resource
77 *
78 * @return the URI of the resource
79 */
80
81 public String getResourceURI() {
82 return href;
83 }
84
85 private byte[] readBinaryFromConn(URLConnection con) throws XPathException {
86 InputStream raw = null;
87 this.connection = con;
88 try {
89 raw = connection.getInputStream();
90
91 int contentLength = connection.getContentLength();
92 InputStream in = new BufferedInputStream(raw);
93 byte[] data = new byte[contentLength];
94 int bytesRead = 0;
95 int offset = 0;
96 while (offset < contentLength) {
97 bytesRead = in.read(data, offset, data.length - offset);
98 if (bytesRead == -1) {
99 break;
100 }
101 offset += bytesRead;
102 }
103 in.close();
104
105 if (offset != contentLength) {
106 throw new XPathException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
107 }
108 return data;
109 } catch (IOException e) {
110 throw new XPathException(e);
111 }
112 }
113
114 /**
115 * Utility method to construct an array of bytes from the content of an InputStream
116 * @param in the input stream. The method consumes the input stream but does not close it.
117 * @param path file name or URI used only for diagnostics
118 * @return byte array representing the content of the InputStream
119 * @throws XPathException
120 */
121
122 public static byte[] readBinaryFromStream(InputStream in, String path) throws XPathException {
123 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
124
125 int nRead;
126 byte[] data = new byte[16384];
127
128 try {
129 while ((nRead = in.read(data, 0, data.length)) != -1) {
130 buffer.write(data, 0, nRead);
131 }
132 buffer.flush();
133 return buffer.toByteArray();
134 } catch (IOException e) {
135 throw new XPathException("Failed to read: " + path + " " + e);
136 }
137 }
138
139 public Item getItem(XPathContext context) throws XPathException {
140 if (data != null) {
141 return new Base64BinaryValue(data);
142 } else if (connection != null) {
143 data = readBinaryFromConn(connection);
144 return new Base64BinaryValue(data);
145 } else if (inputStream != null) {
146 data = readBinaryFromStream(inputStream, href);
147 return new Base64BinaryValue(data);
148 }
149 return null;
150
151 }
152
153 public String getContentType() {
154 return contentType;
155 }
156
157
158 }