1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lfs;
11
12 import java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.attribute.BasicFileAttributes;
16
17 import org.eclipse.jgit.errors.LargeObjectException;
18 import org.eclipse.jgit.errors.MissingObjectException;
19 import org.eclipse.jgit.lib.Constants;
20 import org.eclipse.jgit.lib.ObjectLoader;
21 import org.eclipse.jgit.lib.ObjectStream;
22 import org.eclipse.jgit.storage.pack.PackConfig;
23 import org.eclipse.jgit.util.IO;
24
25
26
27
28
29
30
31 public class LfsBlobLoader extends ObjectLoader {
32
33 private Path mediaFile;
34
35 private BasicFileAttributes attributes;
36
37 private byte[] cached;
38
39
40
41
42
43
44
45
46
47 public LfsBlobLoader(Path mediaFile) throws IOException {
48 this.mediaFile = mediaFile;
49 this.attributes = Files.readAttributes(mediaFile,
50 BasicFileAttributes.class);
51 }
52
53 @Override
54 public int getType() {
55 return Constants.OBJ_BLOB;
56 }
57
58 @Override
59 public long getSize() {
60 return attributes.size();
61 }
62
63 @Override
64 public byte[] getCachedBytes() throws LargeObjectException {
65 if (getSize() > PackConfig.DEFAULT_BIG_FILE_THRESHOLD) {
66 throw new LargeObjectException();
67 }
68
69 if (cached == null) {
70 try {
71 cached = IO.readFully(mediaFile.toFile());
72 } catch (IOException ioe) {
73 throw new LargeObjectException(ioe);
74 }
75 }
76 return cached;
77 }
78
79 @Override
80 public ObjectStream openStream()
81 throws MissingObjectException, IOException {
82 return new ObjectStream.Filter(getType(), getSize(),
83 Files.newInputStream(mediaFile));
84 }
85
86 }