Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

OleBlob.java 12KB

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