下面列出了org.junit.jupiter.api.io.TempDir#java.nio.file.StandardOpenOption 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void saveProblemJson(final List<ClusterOverlapProblem> overlapProblems,
final File imageDir,
final Double z)
throws IOException {
final String jsonFileName = String.format("problem_overlap_bounds_%s_z%06.0f.json", parameters.stack, z);
final Path path = Paths.get(imageDir.getAbsolutePath(), jsonFileName);
final StringBuilder json = new StringBuilder();
json.append("[\n");
for (final ClusterOverlapProblem problem : overlapProblems) {
if (json.length() > 2) {
json.append(",\n");
}
json.append(problem.getBounds().toJson());
}
json.append("\n]");
Files.write(path, json.toString().getBytes(),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.APPEND);
LOG.info("saveProblemJson: saved {}", path);
}
private static int csize(int p) throws Exception {
try (SeekableByteChannel chan = Files.newByteChannel(
Paths.get(dfl(p)), StandardOpenOption.READ)) {
chan.position(6);
int cc = 0;
while (true) {
try {
if (AuthTime.readFrom(chan) != null) cc++;
} catch (BufferUnderflowException e) {
break;
}
}
return cc;
} catch (IOException ioe) {
return 0;
}
}
@Test
public void write() throws Exception {
byte output[] = new byte[data.length];
int crc;
try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
try (FileWriter writer = new Crc32AppendingFileWriter(new FileChannelWriter(fd, 0), 4)) {
ByteBuffer buf = ByteBuffer.wrap(data);
while (buf.hasRemaining())
writer.write(buf);
}
MappedByteBuffer mapped = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size());
mapped.order(ByteOrder.BIG_ENDIAN);
mapped.get(output);
for (int i = 0; i < padLen; ++i)
assertEquals((byte)0, mapped.get());
crc = mapped.getInt();
assertFalse(mapped.hasRemaining());
}
assertArrayEquals(data, output);
assertEquals(expectedCrc, crc);
}
BootFile(Path bootPath)
throws IOException
{
Objects.requireNonNull(bootPath);
_bootPath = bootPath;
long bootSize = Files.size(bootPath);
if (bootSize <= 0) {
throw new IllegalStateException("Unexpected boot size for " + bootPath);
}
if (bootSize >= Integer.MAX_VALUE - 1) {
throw new IllegalStateException("Mmapped file is too large for " + bootPath + " " + _bootSize);
}
_bootSize = (int) bootSize;
_bootChannel = (FileChannel) Files.newByteChannel(_bootPath, StandardOpenOption.READ);
_bootMap = _bootChannel.map(MapMode.READ_ONLY, 0, _bootSize);
readJar();
readManifest();
}
private void writeProcessId() throws IOException {
try {
// Create parent directory (i.e. ~/.cache/apiman/es-pid-{identifier})
Files.createDirectories(pidPath.getParent());
// Get the elasticServer instance variable
Field elasticServerField = elastic.getClass().getDeclaredField("elasticServer");
elasticServerField.setAccessible(true);
Object elasticServerInstance = elasticServerField.get(elastic); // ElasticServer package-scoped so we can't get the real type.
// Get the process ID (pid) long field from ElasticServer
Field pidField = elasticServerInstance.getClass().getDeclaredField("pid");
pidField.setAccessible(true);
pid = (int) pidField.get(elasticServerInstance); // Get the pid
// Write to the PID file
Files.write(pidPath, String.valueOf(pid).getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Test
public void testFileWithInvalidRecord() throws IOException {
List<Record> list = insertTestRecords();
// write a corrupted header to file.
try(FileChannel channel = FileChannel.open(Paths.get(directory.getCanonicalPath(), fileId + HaloDBFile.DATA_FILE_NAME).toAbsolutePath(), StandardOpenOption.APPEND)) {
ByteBuffer data = ByteBuffer.wrap("garbage".getBytes());
channel.write(data);
}
HaloDBFile.HaloDBFileIterator iterator = file.newIterator();
int count = 0;
while (iterator.hasNext() && count < 100) {
Record record = iterator.next();
Assert.assertEquals(record.getKey(), list.get(count++).getKey());
}
// 101th record's header is corrupted.
Assert.assertTrue(iterator.hasNext());
// Since header is corrupted we won't be able to read it and hence next will return null.
Assert.assertNull(iterator.next());
}
@Test
public void testSimpleSuccess() throws IOException {
final File sourceFile = new File("target/1.txt");
final byte[] content = "Hello, World!".getBytes();
Files.write(sourceFile.toPath(), content, StandardOpenOption.CREATE);
final TestRunner runner = TestRunners.newTestRunner(new FetchFile());
runner.setProperty(FetchFile.FILENAME, sourceFile.getAbsolutePath());
runner.setProperty(FetchFile.COMPLETION_STRATEGY, FetchFile.COMPLETION_NONE.getValue());
runner.enqueue(new byte[0]);
runner.run();
runner.assertAllFlowFilesTransferred(FetchFile.REL_SUCCESS, 1);
runner.getFlowFilesForRelationship(FetchFile.REL_SUCCESS).get(0).assertContentEquals(content);
assertTrue(sourceFile.exists());
}
static OpenOptions forNewInputStream(Collection<? extends OpenOption> options) {
if (options.isEmpty()) {
return new OpenOptions(true, false, false, false, false, false, Collections.<OpenOption>emptySet());
}
boolean deleteOnClose = false;
for (OpenOption option : options) {
if (option == StandardOpenOption.DELETE_ON_CLOSE) {
deleteOnClose = true;
} else if (option != StandardOpenOption.READ && option != StandardOpenOption.TRUNCATE_EXISTING && !isIgnoredOpenOption(option)) {
// TRUNCATE_EXISTING is ignored in combination with READ
throw Messages.fileSystemProvider().unsupportedOpenOption(option);
}
}
return new OpenOptions(true, false, false, false, false, deleteOnClose, options);
}
public static Long readTotalSamples(Path rawFile) {
try (SeekableByteChannel bch = Files.newByteChannel(rawFile, StandardOpenOption.READ)) {
if (bch.size() < 4) {
return null;
}
bch.position(bch.size() - 4);
ByteBuffer dst = ByteBuffer.allocate(4);
readFully(bch, dst);
long b4 = dst.get(0) & 0xFF;
long b3 = dst.get(1) & 0xFF;
long b2 = dst.get(2) & 0xFF;
long b1 = dst.get(3) & 0xFF;
return ((b1 << 24) | (b2 << 16) + (b3 << 8) + b4) / 2;
} catch (IOException e1) {
LOG.error("unable to get total number of samples", e1);
return null;
}
}
@Test
public void inferAvroSchemaFromCSVFile() throws Exception {
runner.assertValid();
// Read in the header
StringWriter writer = new StringWriter();
IOUtils.copy((Files.newInputStream(Paths.get("src/test/resources/ShapesHeader.csv"), StandardOpenOption.READ)), writer, "UTF-8");
runner.setProperty(InferAvroSchema.CSV_HEADER_DEFINITION, writer.toString());
runner.setProperty(InferAvroSchema.GET_CSV_HEADER_DEFINITION_FROM_INPUT, "false");
Map<String, String> attributes = new HashMap<>();
attributes.put(CoreAttributes.MIME_TYPE.key(), "text/csv");
runner.enqueue(new File("src/test/resources/Shapes_NoHeader.csv").toPath(), attributes);
runner.run();
runner.assertTransferCount(InferAvroSchema.REL_UNSUPPORTED_CONTENT, 0);
runner.assertTransferCount(InferAvroSchema.REL_FAILURE, 0);
runner.assertTransferCount(InferAvroSchema.REL_ORIGINAL, 1);
runner.assertTransferCount(InferAvroSchema.REL_SUCCESS, 1);
MockFlowFile data = runner.getFlowFilesForRelationship(InferAvroSchema.REL_SUCCESS).get(0);
data.assertContentEquals(unix2PlatformSpecificLineEndings(new File("src/test/resources/Shapes_header.csv.avro")));
data.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/avro-binary");
}
public static boolean appendText(File filePath, boolean addNewLines, List<String> data)
{
try
{
OutputStream out = Files.newOutputStream(filePath.toPath(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)))
{
for (String line: data)
{
writer.append(line);
if (addNewLines) writer.newLine();
}
}
}
catch (IOException e)
{
return false;
}
return true;
}
@Test
void truncatedDatabase() throws Exception {
db.put(Revision.INIT, randomCommitId());
db.close();
// Truncate the database file.
try (FileChannel f = FileChannel.open(new File(tempDir, "commit_ids.dat").toPath(),
StandardOpenOption.APPEND)) {
assertThat(f.size()).isEqualTo(24);
f.truncate(23);
}
assertThatThrownBy(() -> new CommitIdDatabase(tempDir))
.isInstanceOf(StorageException.class)
.hasMessageContaining("incorrect file length");
}
public Location writeToLocal(BytesReference data) throws IOException {
final long position;
final long generation;
try (ReleasableLock lock = writeLock.acquire()) {
ensureOpen();
if (writtenOffset > TRANSLOG_ROLLING_SIZE_BYTES) {
IOUtils.close(writeChannel);
tmpTranslogGeneration.incrementAndGet();
writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
writtenOffset = 0;
}
generation = tmpTranslogGeneration.get();
position = writtenOffset;
try {
data.writeTo(writeChannel);
} catch (Throwable e) {
throw e;
}
writtenOffset = writtenOffset + data.length();
}
return new Translog.Location(generation, position, data.length());
}
/**
* Unmarshal a RunTable from a file.
*
* @param path path to file
* @return unmarshalled run table
*/
public static RunTable unmarshal (Path path)
{
logger.debug("RunTable unmarshalling {}", path);
try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ)) {
Unmarshaller um = getJaxbContext().createUnmarshaller();
RunTable runTable = (RunTable) um.unmarshal(is);
logger.debug("Unmarshalled {}", runTable);
return runTable;
} catch (IOException |
JAXBException ex) {
logger.warn("RunTable. Error unmarshalling " + path + " " + ex, ex);
return null;
}
}
@Before
public void setUp() throws IOException, CertificateException {
MockitoAnnotations.initMocks(this);
when(certificate.getEncoded()).thenReturn("thumbprint".getBytes(UTF_8));
X500Principal cn = new X500Principal("CN=localhost");
when(certificate.getSubjectX500Principal()).thenReturn(cn);
knownHosts = Files.createTempFile("test", "knownHosts");
try (BufferedWriter writer = Files.newBufferedWriter(knownHosts, StandardOpenOption.APPEND)) {
writer.write("someaddress somethumbprint");
writer.newLine();
writer.write("localhost" + " " + CertificateUtil.create().thumbPrint(certificate));
writer.newLine();
}
trustManager = new WhiteListTrustManager(knownHosts);
}
@Override
public void addAll(Collection<? extends String> c) throws IOException {
if (file.exists())
Files.write(file.toPath(), c, defaultCharset,
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
else
Files.write(file.toPath(), c, defaultCharset);
}
/**
* Return a FileLock for a given file, or null if the lock cannot be
* obtained.
*
* @param file
* the file to obtain the lock for. It is assumed the file
* already exists when this is called.
* @param writeData
* if true then 8 bytes of zeroes will be written to the file.
* @return a FileLock for the given File, or null if the lock couldn't be
* obtained.
* @throws IOException
*/
public static FileLock getFileLock(File file, boolean writeData)
throws IOException {
Path path = Paths.get(file.getAbsolutePath());
FileChannel channel = FileChannel.open(path, StandardOpenOption.CREATE,
StandardOpenOption.READ, StandardOpenOption.WRITE);
FileLock lock = channel.tryLock();
if (writeData) {
// unfortunately: I forget exactly why this was added...
channel.write(ByteBuffer.wrap(new byte[8]));
channel.force(false);
}
return lock;
}
@Test
public void readAsynchronousFileChannelPosition() throws Exception {
URI uri = this.resource.getURI();
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(
() -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ),
9, this.bufferFactory, 3);
StepVerifier.create(flux)
.consumeNextWith(stringConsumer("qux"))
.expectComplete()
.verify(Duration.ofSeconds(5));
}
private static void write(File file, String s) throws IOException {
synchronized(KnoxSession.class) {
// Ensure the parent directory exists...
// This will attempt to create all missing directories. No failures will occur if the directories already exist.
Files.createDirectories(file.toPath().getParent());
try (FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
channel.tryLock();
FileUtils.write(file, s, StandardCharsets.UTF_8);
} catch (OverlappingFileLockException e) {
System.out.println("Unable to acquire write lock for: " + file.getAbsolutePath());
}
}
}
public void testFileFormat() throws Exception {
FileChannel channel = FileChannel.open(new File("src/test/resources/example_binaries/libsubstrate_arm64.dylib").toPath(), StandardOpenOption.READ);
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
MachO machO = new MachO(new ByteBufferKaitaiStream(buffer));
checkMachO(buffer, machO, MachO.CpuType.ARM64);
channel.close();
}
/**
* Save public key to PEM file.
*
* @param path
* File path.
* @throws IOException
* IO exception.
*/
public void save(final Path path) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(
"-----BEGIN CERTIFICATE REQUEST-----" + System.lineSeparator());
sb.append(GXCommon.toBase64(getEncoded()));
sb.append(System.lineSeparator() + "-----END CERTIFICATE REQUEST-----");
Files.write(path, sb.toString().getBytes(), StandardOpenOption.CREATE);
}
private static SeekableByteChannel createNoClose(Path p)
throws IOException {
SeekableByteChannel newChan = Files.newByteChannel(
p, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE);
ByteBuffer buffer = ByteBuffer.allocate(6);
buffer.putShort((short)KRB5_RV_VNO);
buffer.order(ByteOrder.nativeOrder());
buffer.putInt(KerberosTime.getDefaultSkew());
buffer.flip();
newChan.write(buffer);
return newChan;
}
/**
* @param args used to take arguments from the running environment; args[0] is the name of the launcher-build.properties file to generate for Ant
* @throws Exception if any error occurs
*/
public static void main( final String[] args ) throws Exception {
final String BUILD_INFO_FILE = "/hu/sllauncher/r/bean/build-info.xml";
// Read current launcher build info
final BuildInfoBean b = JAXB.unmarshal( ScelightLauncher.class.getResource( BUILD_INFO_FILE ), BuildInfoBean.class );
System.out.println( "Current: " + b );
// Increment build
b.setBuild( b.getBuildNumber() + 1 );
b.setDate( new Date() );
System.out.println( "New: " + b );
// Archive to launcher-build-history.txt and save new build info
// to both src-launcher and bin-launcher folders (so no refresh is required in Eclipse)
try ( final BufferedWriter out = Files.newBufferedWriter( Paths.get( "dev-data", "launcher-build-history.txt" ), Charset.forName( "UTF-8" ),
StandardOpenOption.APPEND ) ) {
out.write( b.getBuildNumber() + " " + b.getDate() );
out.newLine();
}
JAXB.marshal( b, Paths.get( "src-launcher", BUILD_INFO_FILE ).toFile() );
JAXB.marshal( b, Paths.get( "bin-launcher", BUILD_INFO_FILE ).toFile() );
// Create properties file for Ant
final Properties p = new Properties();
p.setProperty( "homePageUrl", LConsts.URL_HOME_PAGE.toString() );
p.setProperty( "launcherVer", LConsts.LAUNCHER_VERSION.toString() );
p.setProperty( "launcherBuildNumber", b.getBuildNumber().toString() );
try ( final FileOutputStream out = new FileOutputStream( args[ 0 ] ) ) {
p.store( out, null );
}
}
@Override
protected ByteChannel newStream() throws IOException {
try {
return Files.newByteChannel(path, StandardOpenOption.READ);
} catch (NoSuchFileException e) {
return null;
}
}
@Override
public byte[] get(String key) throws IOException {
try {
FileChannel fileChannel = this.getChannelForKey(key, StandardOpenOption.READ);
int size = (int) fileChannel.size(); // assume < 2GB
ByteBuffer allocate = ByteBuffer.allocate(size);
fileChannel.read(allocate);
return allocate.array();
} catch (NoSuchFileException nex) {
return null;
}
}
@Override
public void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws IOException {
if (failIfAlreadyExists == false) {
deleteBlobIgnoringIfNotExists(blobName);
}
final Path file = path.resolve(blobName);
try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW)) {
Streams.copy(inputStream, outputStream);
}
IOUtils.fsync(file, false);
IOUtils.fsync(path, true);
}
private void createTestData(String name) throws IOException {
deleteDirectorySubtree(Paths.get(basePathTestData(), name));
Files.createDirectory(Paths.get(basePathTestData(), name));
byte[] bytes = new byte[1024];
Random random = new Random();
random.nextBytes(bytes);
Files.write(Paths.get(basePathTestData(), name, "file.bin"), bytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
}
/**
* The try lock
*
* @return true success
*/
public boolean tryLock() {
boolean success = false;
try {
Path path = Paths.get(file.getPath());
if (channel != null && channel.isOpen()) {
return false;
}
channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ);
lock = channel.tryLock();
if (lock != null) {
success = true;
return true;
}
} catch (Exception e) {
return false;
} finally {
if (!success) {
if (channel != null) {
try {
channel.close();
} catch (IOException ignored) {
}
}
}
}
return false;
}
@Test
public void testSmallerThanBufferSize() throws IOException {
final byte[] data = new byte[1024];
for (int i = 0; i < 1020; i++) {
data[i] = 'A';
}
Files.write(targetFile.toPath(), data, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
final int zeroCount = RepairCorruptedFileEndings.countTrailingZeroes(targetFile);
assertEquals(4, zeroCount);
}
/**
* Writes the content stream to the fileLocation.
* Any conflicting files and folders inside the rootFolder are deleted.
*
* @param content
* the file content stream, which is <b>not</b> closed by this method
* @param rootFolder
* only files and folders in this path are deleted
* @param fileLocation
* the file location, must be inside the rootFolder
* @return the number of written bytes
* @throws IOException if anything goes wrong
* @since 9.7.0
*/
public static long forceWriteFile(InputStream content, Path rootFolder, Path fileLocation) throws IOException {
rootFolder = rootFolder.normalize();
fileLocation = rootFolder.resolve(fileLocation).normalize();
if (!fileLocation.startsWith(rootFolder) || fileLocation.getNameCount() <= rootFolder.getNameCount()) {
throw new IOException("fileLocation \"" + fileLocation + "\" is not inside the rootFolder \"" + rootFolder + '"');
}
// remove file -> folder conflicts
if (Files.exists(rootFolder.resolve(fileLocation.getName(rootFolder.getNameCount())))) {
for (Path p = fileLocation.getParent(); p.getNameCount() > rootFolder.getNameCount(); p = p.getParent()) {
if (Files.exists(p) && !Files.isDirectory(p)) {
Files.delete(p);
// if it's a file the parent must be a folder
break;
}
}
}
// remove folder -> file conflicts
if (Files.isDirectory(fileLocation)) {
FileUtils.forceDelete(fileLocation.toFile());
}
// create parent folders
if (!Files.exists(fileLocation.getParent())) {
Files.createDirectories(fileLocation.toAbsolutePath().getParent());
}
try (OutputStream out = Files.newOutputStream(fileLocation, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
return IOUtils.copy(content, out, COPY_BUFFER_SIZE);
}
}