MCPcopy Create free account
hub / github.com/ReZeroS/git / getFileByteSource

Method getFileByteSource

src/main/java/club/qqtim/util/FileUtil.java:123–140  ·  view source on GitHub ↗
(String path, String type)

Source from the content-addressed store, hash-verified

121 }
122
123 public static ByteSource getFileByteSource(String path, String type) throws IOException {
124 char nullChar = 0;
125 ByteSource byteSource = Files.asByteSource(new File(path));
126 if (ConstantVal.NONE.equals(type)) {
127 return byteSource;
128 }
129 // todo: refactor the below code to extract a method like obj.partition in python
130 byte[] fileWithHeader = byteSource.read();
131 byte[] header = new byte[type.getBytes(Charsets.UTF_8).length];
132 byte[] nullBytes = new byte[Chars.toByteArray(nullChar).length];
133 byte[] fileContent = new byte[fileWithHeader.length - header.length - nullBytes.length];
134 ByteBuffer fileWithHeaderBuffer = ByteBuffer.wrap(fileWithHeader);
135 fileWithHeaderBuffer.get(header, 0, header.length);
136 log.debug("current object type is {}:", new String(header, Charsets.UTF_8));
137 fileWithHeaderBuffer.get(nullBytes, 0, nullBytes.length);
138 fileWithHeaderBuffer.get(fileContent, 0, fileContent.length);
139 return ByteSource.wrap(fileContent);
140 }
141
142
143

Callers 2

getObjectMethod · 0.95
getFileAsStringMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected