下面列出了怎么用org.apache.commons.io.input.CharSequenceInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void md5Sum() throws IOException {
MCRObjectID derId = MCRObjectID.getInstance("MCR_derivate_00000002");
String fileName = "hallo.txt";
MCRPath filePath = MCRPath.getPath(derId.toString(), fileName);
Files.createFile(filePath);
try (InputStream is = new CharSequenceInputStream("Hello World!", StandardCharsets.UTF_8)) {
Files.copy(is, filePath, StandardCopyOption.REPLACE_EXISTING);
}
startNewTransaction();
MCRFileAttributes attrs = Files.readAttributes(filePath, MCRFileAttributes.class);
MCRCStoreIFS2 ifs2 = (MCRCStoreIFS2) MCRContentStoreFactory.getStore("IFS2");
MCRFileCollection fileCollection = ifs2.getIFS2FileCollection(derId);
org.mycore.datamodel.ifs2.MCRFile file2 = (org.mycore.datamodel.ifs2.MCRFile) fileCollection.getChild(fileName);
Assert.assertEquals("MD5 mismatch.", attrs.md5sum(), file2.getMD5());
}
@Test
public void testMD5CopyCommand() throws IOException {
MCRObjectID derId = MCRObjectID.getInstance("MCR_derivate_00000003");
String fileName = "hallo.txt";
MCRPath filePath = MCRPath.getPath(derId.toString(), fileName);
Files.createFile(filePath);
try (InputStream is = new CharSequenceInputStream("Hello World!", StandardCharsets.UTF_8)) {
Files.copy(is, filePath, StandardCopyOption.REPLACE_EXISTING);
}
startNewTransaction();
MCRFileAttributes attrs = Files.readAttributes(filePath, MCRFileAttributes.class);
MCRCStoreIFS2 ifs2 = (MCRCStoreIFS2) MCRContentStoreFactory.getStore("IFS2");
MCRFileCollection fileCollection = ifs2.getIFS2FileCollection(derId);
org.mycore.datamodel.ifs2.MCRFile file2 = (org.mycore.datamodel.ifs2.MCRFile) fileCollection.getChild(fileName);
Assert.assertEquals("MD5 mismatch.", attrs.md5sum(), file2.getMD5());
file2.setMD5("invalid");
file2 = (org.mycore.datamodel.ifs2.MCRFile) fileCollection.getChild(fileName);
Assert.assertNotEquals("MD5 was not updated.", attrs.md5sum(), file2.getMD5());
MCRIFSCommands.copyMD5ToIFS2();
file2 = (org.mycore.datamodel.ifs2.MCRFile) fileCollection.getChild(fileName);
Assert.assertEquals("MD5 mismatch.", attrs.md5sum(), file2.getMD5());
}
@Test
public void testReadBytesFromStream() throws Exception {
final String testString = "fuzzy wuzzy was a bear; fuzzy wuzzy had no hair...";
final byte[] testBytes = testString.getBytes("UTF-8");
final InputStream inputStream = new CharSequenceInputStream(testString, "UTF-8");
final ByteArrayOutputStream copyStream = new ByteArrayOutputStream();
// this takes what was in inputStream, copies it into copyStream, and either resets inputStream (if supported)
// or returns a new stream around the bytes read
final InputStream backupStream = StreamUtil.cloneContent(inputStream, StreamUtil.ONE_MB, copyStream);
if (backupStream == inputStream) {
System.out.println("same stream returned (reset)");
// verify readBytesFromStream gets everything from the inputStream (it also verifies cloneContent resets the source)
byte[] streamBytes = StreamUtil.readBytesFromStream(inputStream);
Assert.assertArrayEquals(testBytes, streamBytes); // it's all US-ASCII so it should match UTF-8 bytes
} else {
System.out.println("new stream returned");
byte[] backupBytes = StreamUtil.readBytesFromStream(backupStream);
Assert.assertArrayEquals(testBytes, backupBytes);
}
Assert.assertArrayEquals(testBytes, copyStream.toByteArray());
}
@NotNull
public static InputStream asStream(@NotNull String content) {
return new CharSequenceInputStream(content, StandardCharsets.UTF_8);
}