You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OleBlob.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.util;
  14. import java.io.Closeable;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.sql.Blob;
  22. import java.util.stream.Stream;
  23. import java.util.stream.StreamSupport;
  24. import com.healthmarketscience.jackcess.impl.OleUtil;
  25. /**
  26. * Extensions of the Blob interface with additional functionality for working
  27. * with the OLE content from an access database. The ole data type in access
  28. * has a wide range of functionality (including wrappers with nested wrappers
  29. * with nested filesystems!), and jackcess only supports a small portion of
  30. * it. That said, jackcess should support the bulk of the common
  31. * functionality.
  32. * <p>
  33. * The main Blob methods will interact with the <i>entire</i> OLE field data
  34. * which, in most cases, contains additional wrapper information. In order to
  35. * access the ultimate "content" contained within the OLE data, the {@link
  36. * #getContent} method should be used. The type of this content may be a
  37. * variety of formats, so additional sub-interfaces are available to interact
  38. * with it. The most specific sub-interface can be determined by the {@link
  39. * ContentType} of the Content.
  40. * <p>
  41. * Once an OleBlob is no longer useful, <i>it should be closed</i> using
  42. * {@link #free} or {@link #close} methods (after which, the instance will no
  43. * longer be functional).
  44. * <p>
  45. * Note, the OleBlob implementation is read-only (through the interface). In
  46. * order to modify blob contents, create a new OleBlob instance using {@link
  47. * OleBlob.Builder} and write it to the access database.
  48. * <p>
  49. * <b>Example for interpreting an existing OLE field:</b>
  50. * <pre>
  51. * OleBlob oleBlob = null;
  52. * try {
  53. * oleBlob = row.getBlob("MyOleColumn");
  54. * Content content = oleBlob.getContent()
  55. * if(content.getType() == OleBlob.ContentType.SIMPLE_PACKAGE) {
  56. * FileOutputStream out = ...;
  57. * ((SimplePackageContent)content).writeTo(out);
  58. * out.closee();
  59. * }
  60. * } finally {
  61. * if(oleBlob != null) { oleBlob.close(); }
  62. * }
  63. * </pre>
  64. * <p>
  65. * <b>Example for creating new, embedded ole data:</b>
  66. * <pre>
  67. * OleBlob oleBlob = null;
  68. * try {
  69. * oleBlob = new OleBlob.Builder()
  70. * .setSimplePackage(new File("some_data.txt"))
  71. * .toBlob();
  72. * db.addRow(1, oleBlob);
  73. * } finally {
  74. * if(oleBlob != null) { oleBlob.close(); }
  75. * }
  76. * </pre>
  77. * <p>
  78. * <b>Example for creating new, linked ole data:</b>
  79. * <pre>
  80. * OleBlob oleBlob = null;
  81. * try {
  82. * oleBlob = new OleBlob.Builder()
  83. * .setLink(new File("some_data.txt"))
  84. * .toBlob();
  85. * db.addRow(1, oleBlob);
  86. * } finally {
  87. * if(oleBlob != null) { oleBlob.close(); }
  88. * }
  89. * </pre>
  90. *
  91. * @author James Ahlborn
  92. */
  93. public interface OleBlob extends Blob, Closeable
  94. {
  95. /** Enum describing the types of blob contents which are currently
  96. supported/understood */
  97. public enum ContentType {
  98. /** the blob contents are a link (file path) to some external content.
  99. Content will be an instance of LinkContent */
  100. LINK,
  101. /** the blob contents are a simple wrapper around some embedded content
  102. and related file names/paths. Content will be an instance
  103. SimplePackageContent */
  104. SIMPLE_PACKAGE,
  105. /** the blob contents are a complex embedded data known as compound
  106. storage (aka OLE2). Working with compound storage requires the
  107. optional POI library. Content will be an instance of CompoundContent.
  108. If the POI library is not available on the classpath, then compound
  109. storage data will instead be returned as type {@link #OTHER}. */
  110. COMPOUND_STORAGE,
  111. /** the top-level blob wrapper is understood, but the nested blob contents
  112. are unknown, probably just some embedded content. Content will be an
  113. instance of OtherContent */
  114. OTHER,
  115. /** the top-level blob wrapper is not understood (this may not be a valid
  116. ole instance). Content will simply be an instance of Content (the
  117. data can be accessed from the main blob instance) */
  118. UNKNOWN;
  119. }
  120. /**
  121. * Writes the entire raw blob data to the given stream (this is the access
  122. * db internal format, which includes all wrapper information).
  123. *
  124. * @param out stream to which the blob will be written
  125. */
  126. public void writeTo(OutputStream out) throws IOException;
  127. /**
  128. * Returns the decoded form of the blob contents, if understandable.
  129. */
  130. public Content getContent() throws IOException;
  131. public interface Content
  132. {
  133. /**
  134. * Returns the type of this content.
  135. */
  136. public ContentType getType();
  137. /**
  138. * Returns the blob which owns this content.
  139. */
  140. public OleBlob getBlob();
  141. }
  142. /**
  143. * Intermediate sub-interface for Content which has a nested package.
  144. */
  145. public interface PackageContent extends Content
  146. {
  147. public String getPrettyName() throws IOException;
  148. public String getClassName() throws IOException;
  149. public String getTypeName() throws IOException;
  150. }
  151. /**
  152. * Intermediate sub-interface for Content which has embedded content.
  153. */
  154. public interface EmbeddedContent extends Content
  155. {
  156. public long length();
  157. public InputStream getStream() throws IOException;
  158. public void writeTo(OutputStream out) throws IOException;
  159. }
  160. /**
  161. * Sub-interface for Content which has the {@link ContentType#LINK} type.
  162. * The actual content is external to the access database and can be found at
  163. * {@link #getLinkPath}.
  164. */
  165. public interface LinkContent extends PackageContent
  166. {
  167. public String getFileName();
  168. public String getLinkPath();
  169. public String getFilePath();
  170. public InputStream getLinkStream() throws IOException;
  171. }
  172. /**
  173. * Sub-interface for Content which has the {@link
  174. * ContentType#SIMPLE_PACKAGE} type. The actual content is embedded within
  175. * the access database (but the original file source path can also be found
  176. * at {@link #getFilePath}).
  177. */
  178. public interface SimplePackageContent
  179. extends PackageContent, EmbeddedContent
  180. {
  181. public String getFileName();
  182. public String getFilePath();
  183. public String getLocalFilePath();
  184. }
  185. /**
  186. * Sub-interface for Content which has the {@link
  187. * ContentType#COMPOUND_STORAGE} type. Compound storage is a complex
  188. * embedding format also known as OLE2. In some situations (mostly
  189. * non-microsoft office file formats) the actual content is available from
  190. * the {@link #getContentsEntry} method (if {@link #hasContentsEntry}
  191. * returns {@code true}). In other situations (e.g. microsoft office file
  192. * formats), the actual content is most or all of the compound content (but
  193. * retrieving the final file may be a complex operation beyond the scope of
  194. * jackcess). Note that the CompoundContent type will only be available if
  195. * the POI library is in the classpath, otherwise compound content will be
  196. * returned as OtherContent.
  197. */
  198. public interface CompoundContent extends PackageContent, EmbeddedContent,
  199. Iterable<CompoundContent.Entry>
  200. {
  201. public Entry getEntry(String entryName) throws IOException;
  202. public boolean hasContentsEntry() throws IOException;
  203. public Entry getContentsEntry() throws IOException;
  204. /**
  205. * @return a Stream using the default Iterator.
  206. */
  207. default public Stream<CompoundContent.Entry> stream() {
  208. return StreamSupport.stream(spliterator(), false);
  209. }
  210. /**
  211. * A document entry in the compound storage.
  212. */
  213. public interface Entry extends EmbeddedContent
  214. {
  215. public String getName();
  216. /**
  217. * Returns the CompoundContent which owns this entry.
  218. */
  219. public CompoundContent getParent();
  220. }
  221. }
  222. /**
  223. * Sub-interface for Content which has the {@link ContentType#OTHER} type.
  224. * This may be a simple embedded file or some other, currently not
  225. * understood complex type.
  226. */
  227. public interface OtherContent extends PackageContent, EmbeddedContent
  228. {
  229. }
  230. /**
  231. * Builder style class for constructing an OleBlob. See {@link OleBlob} for
  232. * example usage.
  233. */
  234. public class Builder
  235. {
  236. public static final String PACKAGE_PRETTY_NAME = "Packager Shell Object";
  237. public static final String PACKAGE_TYPE_NAME = "Package";
  238. private ContentType _type;
  239. private byte[] _bytes;
  240. private InputStream _stream;
  241. private long _contentLen;
  242. private String _fileName;
  243. private String _filePath;
  244. private String _prettyName;
  245. private String _className;
  246. private String _typeName;
  247. public ContentType getType() {
  248. return _type;
  249. }
  250. public byte[] getBytes() {
  251. return _bytes;
  252. }
  253. public InputStream getStream() {
  254. return _stream;
  255. }
  256. public long getContentLength() {
  257. return _contentLen;
  258. }
  259. public String getFileName() {
  260. return _fileName;
  261. }
  262. public String getFilePath() {
  263. return _filePath;
  264. }
  265. public String getPrettyName() {
  266. return _prettyName;
  267. }
  268. public String getClassName() {
  269. return _className;
  270. }
  271. public String getTypeName() {
  272. return _typeName;
  273. }
  274. public Builder setSimplePackageBytes(byte[] bytes) {
  275. _bytes = bytes;
  276. _contentLen = bytes.length;
  277. setDefaultPackageType();
  278. _type = ContentType.SIMPLE_PACKAGE;
  279. return this;
  280. }
  281. public Builder setSimplePackageStream(InputStream in, long length) {
  282. _stream = in;
  283. _contentLen = length;
  284. setDefaultPackageType();
  285. _type = ContentType.SIMPLE_PACKAGE;
  286. return this;
  287. }
  288. public Builder setSimplePackageFileName(String fileName) {
  289. _fileName = fileName;
  290. setDefaultPackageType();
  291. _type = ContentType.SIMPLE_PACKAGE;
  292. return this;
  293. }
  294. public Builder setSimplePackageFilePath(String filePath) {
  295. _filePath = filePath;
  296. setDefaultPackageType();
  297. _type = ContentType.SIMPLE_PACKAGE;
  298. return this;
  299. }
  300. public Builder setSimplePackage(File f) throws FileNotFoundException {
  301. _fileName = f.getName();
  302. _filePath = f.getAbsolutePath();
  303. return setSimplePackageStream(new FileInputStream(f), f.length());
  304. }
  305. public Builder setLinkFileName(String fileName) {
  306. _fileName = fileName;
  307. setDefaultPackageType();
  308. _type = ContentType.LINK;
  309. return this;
  310. }
  311. public Builder setLinkPath(String link) {
  312. _filePath = link;
  313. setDefaultPackageType();
  314. _type = ContentType.LINK;
  315. return this;
  316. }
  317. public Builder setLink(File f) {
  318. _fileName = f.getName();
  319. _filePath = f.getAbsolutePath();
  320. setDefaultPackageType();
  321. _type = ContentType.LINK;
  322. return this;
  323. }
  324. private void setDefaultPackageType() {
  325. if(_prettyName == null) {
  326. _prettyName = PACKAGE_PRETTY_NAME;
  327. }
  328. if(_className == null) {
  329. _className = PACKAGE_TYPE_NAME;
  330. }
  331. }
  332. public Builder setOtherBytes(byte[] bytes) {
  333. _bytes = bytes;
  334. _contentLen = bytes.length;
  335. _type = ContentType.OTHER;
  336. return this;
  337. }
  338. public Builder setOtherStream(InputStream in, long length) {
  339. _stream = in;
  340. _contentLen = length;
  341. _type = ContentType.OTHER;
  342. return this;
  343. }
  344. public Builder setOther(File f) throws FileNotFoundException {
  345. return setOtherStream(new FileInputStream(f), f.length());
  346. }
  347. public Builder setPackagePrettyName(String prettyName) {
  348. _prettyName = prettyName;
  349. return this;
  350. }
  351. public Builder setPackageClassName(String className) {
  352. _className = className;
  353. return this;
  354. }
  355. public Builder setPackageTypeName(String typeName) {
  356. _typeName = typeName;
  357. return this;
  358. }
  359. public OleBlob toBlob() throws IOException {
  360. return OleUtil.createBlob(this);
  361. }
  362. public static OleBlob fromInternalData(byte[] bytes) throws IOException {
  363. return OleUtil.parseBlob(bytes);
  364. }
  365. }
  366. }