View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.InputStream;
23  import java.security.MessageDigest;
24  import java.util.Arrays;
25  
26  import org.mycore.common.content.streams.MCRMD5InputStream;
27  
28  /**
29   * Reads MCRContent from a byte[] array.
30   * 
31   * @author Frank L\u00FCtzenkichen
32   */
33  public class MCRByteContent extends MCRContent {
34  
35      private byte[] bytes;
36  
37      private int offset;
38  
39      private int length;
40  
41      private long lastModified;
42  
43      public MCRByteContent(byte[] bytes) {
44          this(bytes, 0, bytes.length);
45      }
46  
47      public MCRByteContent(byte[] bytes, int offset, int length) {
48          this(bytes, offset, length, System.currentTimeMillis());
49      }
50  
51      public MCRByteContent(byte[] bytes, long lastModified) {
52          this(bytes, 0, bytes.length, lastModified);
53      }
54  
55      public MCRByteContent(byte[] bytes, int offset, int length, long lastModified) {
56          this.bytes = bytes;
57          this.offset = offset;
58          this.length = length;
59          this.lastModified = lastModified;
60      }
61  
62      @Override
63      public void setSystemId(String systemId) {
64          this.systemId = systemId;
65      }
66  
67      @Override
68      public InputStream getInputStream() {
69          return new ByteArrayInputStream(bytes, offset, length);
70      }
71  
72      @Override
73      public byte[] asByteArray() {
74          if (offset == 0 && length == 0) {
75              return bytes;
76          }
77          synchronized (this) {
78              if (offset == 0 && length == bytes.length) {
79                  return bytes;
80              }
81              bytes = Arrays.copyOfRange(bytes, offset, offset + length);
82              offset = 0;
83              length = bytes.length;
84          }
85          return bytes;
86      }
87  
88      @Override
89      public long length() {
90          return length;
91      }
92  
93      @Override
94      public long lastModified() {
95          return lastModified;
96      }
97  
98      @Override
99      public String getETag() {
100         MessageDigest md5Digest = MCRMD5InputStream.buildMD5Digest();
101         md5Digest.update(bytes, offset, length);
102         byte[] digest = md5Digest.digest();
103         String md5String = MCRMD5InputStream.getMD5String(digest);
104         return '"' + md5String + '"';
105     }
106 }