| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.openpackaging.parts.WordprocessingML; |
|---|
| 22 | |
|---|
| 23 | import java.io.IOException; |
|---|
| 24 | import java.io.InputStream; |
|---|
| 25 | import java.io.OutputStream; |
|---|
| 26 | import java.lang.ref.Reference; |
|---|
| 27 | import java.lang.ref.SoftReference; |
|---|
| 28 | import java.nio.ByteBuffer; |
|---|
| 29 | import java.util.zip.ZipFile; |
|---|
| 30 | |
|---|
| 31 | import org.apache.commons.io.IOUtils; |
|---|
| 32 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 33 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 34 | import org.docx4j.openpackaging.parts.ExternalTarget; |
|---|
| 35 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 36 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 37 | import org.docx4j.utils.BufferUtil; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | public class BinaryPart extends Part { |
|---|
| 41 | |
|---|
| 42 | public BinaryPart(PartName partName) throws InvalidFormatException { |
|---|
| 43 | super(partName); |
|---|
| 44 | |
|---|
| 45 | // Can't setContentType or setRelationshipType, since |
|---|
| 46 | // these will differ depending on the nature of the data. |
|---|
| 47 | // Common binary parts should extend this class to |
|---|
| 48 | // provide that information. |
|---|
| 49 | |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | ExternalTarget externalTarget = null; |
|---|
| 54 | public BinaryPart(ExternalTarget externalTarget) { |
|---|
| 55 | |
|---|
| 56 | this.externalTarget = externalTarget; |
|---|
| 57 | |
|---|
| 58 | } |
|---|
| 59 | public ExternalTarget getExternalTarget() { |
|---|
| 60 | return this.externalTarget; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | // private InputStream binaryData; |
|---|
| 64 | // |
|---|
| 65 | // public InputStream getBinaryData() { |
|---|
| 66 | // return binaryData; |
|---|
| 67 | // } |
|---|
| 68 | |
|---|
| 69 | java.nio.ByteBuffer bb; |
|---|
| 70 | public void setBinaryData(InputStream binaryData) { |
|---|
| 71 | log.debug("reading input stream"); |
|---|
| 72 | try { |
|---|
| 73 | this.bb = org.docx4j.utils.BufferUtil.readInputStream(binaryData); |
|---|
| 74 | log.debug(".. done" ); |
|---|
| 75 | } catch (IOException e) { |
|---|
| 76 | //e.printStackTrace(); |
|---|
| 77 | log.error(e); |
|---|
| 78 | } finally { |
|---|
| 79 | try { |
|---|
| 80 | log.debug("closing binary input stream"); |
|---|
| 81 | binaryData.close(); |
|---|
| 82 | log.info(".. closed."); |
|---|
| 83 | } catch (Exception nested) { |
|---|
| 84 | // ignored |
|---|
| 85 | log.error(nested); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | public void setBinaryData(byte[] bytes) { |
|---|
| 91 | this.bb = java.nio.ByteBuffer.wrap(bytes); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public void setBinaryData(ByteBuffer bb) { |
|---|
| 95 | this.bb = bb; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | private String zipFileName = null; |
|---|
| 99 | private String resolvedPartUri = null; |
|---|
| 100 | private Reference<ByteBuffer> bbRef = null; |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Sets the values required to load part data on demand. |
|---|
| 104 | * |
|---|
| 105 | * @param zipFileName the zip file name |
|---|
| 106 | * @param resolvedPartUri the resolved part uri |
|---|
| 107 | */ |
|---|
| 108 | public void setBinaryDataRef(String zipFileName, String resolvedPartUri) { |
|---|
| 109 | this.zipFileName = zipFileName; |
|---|
| 110 | this.resolvedPartUri = resolvedPartUri; |
|---|
| 111 | this.bbRef = null; |
|---|
| 112 | log.debug("set binary part data reference: " |
|---|
| 113 | + this.zipFileName + "!" + this.resolvedPartUri); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | public ByteBuffer getBuffer() { |
|---|
| 118 | ByteBuffer res = null; |
|---|
| 119 | if (this.bb != null) { |
|---|
| 120 | // use buffer loaded during package load |
|---|
| 121 | res = this.bb; |
|---|
| 122 | |
|---|
| 123 | } else if ((this.zipFileName != null) |
|---|
| 124 | && (this.resolvedPartUri != null)) { |
|---|
| 125 | // use on-demand buffer |
|---|
| 126 | res = (this.bbRef != null) ? this.bbRef.get() : null; |
|---|
| 127 | if (res == null) { |
|---|
| 128 | // no cached buffer, load part data now |
|---|
| 129 | log.debug("loading binary part data: " |
|---|
| 130 | + this.zipFileName + "!" + this.resolvedPartUri); |
|---|
| 131 | ZipFile zf = null; |
|---|
| 132 | InputStream in = null; |
|---|
| 133 | try { |
|---|
| 134 | zf = new ZipFile(this.zipFileName); |
|---|
| 135 | in = zf.getInputStream(zf.getEntry(this.resolvedPartUri)); |
|---|
| 136 | res = BufferUtil.readInputStream(in); |
|---|
| 137 | // Store buffer thru soft reference so it could be |
|---|
| 138 | // unloaded by the java vm if free memory is low. |
|---|
| 139 | this.bbRef = new SoftReference<ByteBuffer>(res); |
|---|
| 140 | } catch (IOException ex) { |
|---|
| 141 | log.error(ex); |
|---|
| 142 | } finally { |
|---|
| 143 | IOUtils.closeQuietly(in); |
|---|
| 144 | if (zf != null) { |
|---|
| 145 | try { |
|---|
| 146 | zf.close(); |
|---|
| 147 | } catch (IOException ex) { |
|---|
| 148 | // ignored |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | res.rewind(); // Don't forget this! |
|---|
| 156 | return res; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Copy the ByteBuffer containing this part's binary data |
|---|
| 161 | * to an output stream. |
|---|
| 162 | * |
|---|
| 163 | * @param out |
|---|
| 164 | * @throws IOException |
|---|
| 165 | */ |
|---|
| 166 | public void writeDataToOutputStream(OutputStream out) throws IOException { |
|---|
| 167 | ByteBuffer buf = this.getBuffer(); |
|---|
| 168 | |
|---|
| 169 | buf.clear(); |
|---|
| 170 | byte[] bytes = new byte[buf.capacity()]; |
|---|
| 171 | buf.get(bytes, 0, bytes.length); |
|---|
| 172 | |
|---|
| 173 | out.write( bytes ); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public boolean isContentEqual(Part other) throws Docx4JException { |
|---|
| 177 | |
|---|
| 178 | if (!(other instanceof BinaryPart)) |
|---|
| 179 | return false; |
|---|
| 180 | |
|---|
| 181 | ByteBuffer thisBB = getBuffer(); |
|---|
| 182 | ByteBuffer thatBB = ((BinaryPart)other).getBuffer(); |
|---|
| 183 | |
|---|
| 184 | return thisBB.equals(thatBB); |
|---|
| 185 | |
|---|
| 186 | // if (m_ContentType != arg.m_ContentType) |
|---|
| 187 | // return false; |
|---|
| 188 | |
|---|
| 189 | // if (m_Image.GetLongLength(0) != arg.m_Image.GetLongLength(0)) |
|---|
| 190 | // return false; |
|---|
| 191 | // |
|---|
| 192 | // // Compare the arrays byte by byte |
|---|
| 193 | // long length = m_Image.GetLongLength(0); |
|---|
| 194 | // for (long n = 0; n < length; n++) |
|---|
| 195 | // if (m_Image[n] != arg.m_Image[n]) |
|---|
| 196 | // return false; |
|---|
| 197 | // return true; |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | } |
|---|