java.nio.file.StandardOpenOption Java Examples

The following examples show how to use java.nio.file.StandardOpenOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: render   Author: saalfeldlab   File: ClusterOverlapClient.java    License: GNU General Public License v2.0 7 votes vote down vote up
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);
}
 
Example #2
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: ReplayCacheTestProc.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #3
Source Project: monsoon   Author: groupon   File: Crc32AppendingFileWriterTest.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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);
}
 
Example #4
Source Project: baratine   Author: baratine   File: BootFile.java    License: GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example #5
Source Project: apiman   Author: apiman   File: ApimanEmbeddedElastic.java    License: Apache License 2.0 6 votes vote down vote up
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);
    }

}
 
Example #6
Source Project: HaloDB   Author: yahoo   File: HaloDBFileTest.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #7
Source Project: nifi   Author: apache   File: TestFetchFile.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #8
Source Project: sftp-fs   Author: robtimus   File: OpenOptions.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #9
Source Project: r2cloud   Author: dernasherbrezon   File: Util.java    License: Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #10
Source Project: localization_nifi   Author: wangrenlei   File: TestInferAvroSchema.java    License: Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #11
Source Project: fabric-carpet   Author: gnembon   File: FileModule.java    License: MIT License 6 votes vote down vote up
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;
}
 
Example #12
Source Project: centraldogma   Author: line   File: CommitIdDatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #13
Source Project: Elasticsearch   Author: baidu   File: LocalTranslog.java    License: Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #14
Source Project: audiveris   Author: Audiveris   File: RunTable.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #15
Source Project: tessera   Author: jpmorganchase   File: WhiteListTrustManagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);

}
 
Example #16
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ToolBox.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source Project: pumpernickel   Author: mickleness   File: IOUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source Project: java-technology-stack   Author: codeEngraver   File: DataBufferUtilsTests.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example #19
Source Project: knox   Author: apache   File: KnoxSession.java    License: Apache License 2.0 5 votes vote down vote up
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());
    }
  }
}
 
Example #20
Source Project: unidbg   Author: zhkl0228   File: MachOTest.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #21
Source Project: gurux.dlms.java   Author: Gurux   File: GXPkcs10.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source Project: openjdk-8   Author: bpupadhyaya   File: DflCache.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #23
Source Project: scelight   Author: icza   File: PrepNextLauncherRelease.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @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 );
	}
}
 
Example #24
Source Project: armeria   Author: line   File: FileSystemHttpFile.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected ByteChannel newStream() throws IOException {
    try {
        return Files.newByteChannel(path, StandardOpenOption.READ);
    } catch (NoSuchFileException e) {
        return null;
    }
}
 
Example #25
Source Project: twister2   Author: DSC-SPIDAL   File: LocalFileStateStore.java    License: Apache License 2.0 5 votes vote down vote up
@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;
  }
}
 
Example #26
Source Project: crate   Author: crate   File: FsBlobContainer.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source Project: jdrivesync   Author: siom79   File: ITUpdateMetadata.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #28
Source Project: neural   Author: yu120   File: FileLock.java    License: MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #29
Source Project: nifi   Author: apache   File: TestRepairCorruptedFileEndings.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source Project: rapidminer-studio   Author: rapidminer   File: Tools.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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);
	}
}