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.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.nio.channels.ReadableByteChannel;
26  import java.nio.channels.SeekableByteChannel;
27  import java.nio.file.CopyOption;
28  import java.nio.file.FileSystem;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.StandardCopyOption;
32  import java.nio.file.StandardOpenOption;
33  import java.nio.file.attribute.BasicFileAttributes;
34  import java.util.Objects;
35  
36  import org.apache.logging.log4j.LogManager;
37  import org.apache.logging.log4j.Logger;
38  import org.mycore.datamodel.niofs.MCRFileAttributes;
39  
40  /**
41   * MCRContent implementation that uses Java 7 {@link FileSystem} features.
42   * 
43   * @author Thomas Scheffler (yagee)
44   */
45  public class MCRPathContent extends MCRContent implements MCRSeekableChannelContent {
46  
47      private static final Logger LOGGER = LogManager.getLogger();
48  
49      private Path path;
50  
51      private BasicFileAttributes attrs;
52  
53      private static int BUFFER_SIZE = 8192;
54  
55      public MCRPathContent(Path path) {
56          this(path, null);
57      }
58  
59      public MCRPathContent(Path path, BasicFileAttributes attrs) {
60          this.path = Objects.requireNonNull(path).toAbsolutePath().normalize();
61          this.attrs = attrs;
62      }
63  
64      /* (non-Javadoc)
65       * @see org.mycore.common.content.MCRSeekableChannelContent#getSeekableByteChannel()
66       */
67      @Override
68      public SeekableByteChannel getSeekableByteChannel() throws IOException {
69          return Files.newByteChannel(path, StandardOpenOption.READ);
70      }
71  
72      @Override
73      public InputStream getInputStream() throws IOException {
74          return Files.newInputStream(path, StandardOpenOption.READ);
75      }
76  
77      @Override
78      public ReadableByteChannel getReadableByteChannel() throws IOException {
79          return getSeekableByteChannel();
80      }
81  
82      @Override
83      public byte[] asByteArray() throws IOException {
84          return Files.readAllBytes(path);
85      }
86  
87      @Override
88      public long length() throws IOException {
89          return attrs != null ? attrs.size() : Files.size(path);
90      }
91  
92      @Override
93      public long lastModified() throws IOException {
94          return (attrs != null ? attrs.lastModifiedTime() : Files.getLastModifiedTime(path)).toMillis();
95      }
96  
97      @Override
98      public String getETag() throws IOException {
99          if (attrs instanceof MCRFileAttributes) {
100             return ((MCRFileAttributes) attrs).md5sum();
101         }
102 
103         if (Files.getFileStore(path).supportsFileAttributeView("md5")) {
104             Object fileKey = Files.getAttribute(path, "md5:md5");
105             if (fileKey instanceof String) {
106                 return fileKey.toString();
107             }
108         }
109 
110         return super.getETag();
111     }
112 
113     @Override
114     public String getMimeType() throws IOException {
115         return mimeType == null ? Files.probeContentType(path) : mimeType;
116     }
117 
118     @Override
119     public String getSystemId() {
120         return path.toUri().toString();
121     }
122 
123     @Override
124     public String getName() {
125         return name == null ? path.getFileName().toString() : super.getName();
126     }
127 
128     @Override
129     public void sendTo(OutputStream out) throws IOException {
130         Files.copy(path, out);
131     }
132 
133     @Override
134     public void sendTo(File target) throws IOException {
135         Files.copy(path, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
136     }
137 
138     @Override
139     public void sendTo(Path target, CopyOption... options) throws IOException {
140         Files.copy(path, target, options);
141     }
142 }