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 File: ClusterOverlapClient.java From render with GNU General Public License v2.0 | 7 votes |
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 File: CommitIdDatabaseTest.java From centraldogma with Apache License 2.0 | 6 votes |
@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 #3
Source File: TestInferAvroSchema.java From localization_nifi with Apache License 2.0 | 6 votes |
@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 #4
Source File: Util.java From r2cloud with Apache License 2.0 | 6 votes |
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 #5
Source File: BootFile.java From baratine with GNU General Public License v2.0 | 6 votes |
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 #6
Source File: FileModule.java From fabric-carpet with MIT License | 6 votes |
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 #7
Source File: OpenOptions.java From sftp-fs with Apache License 2.0 | 6 votes |
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 #8
Source File: TestFetchFile.java From nifi with Apache License 2.0 | 6 votes |
@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 #9
Source File: HaloDBFileTest.java From HaloDB with Apache License 2.0 | 6 votes |
@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 #10
Source File: ApimanEmbeddedElastic.java From apiman with Apache License 2.0 | 6 votes |
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 #11
Source File: Crc32AppendingFileWriterTest.java From monsoon with BSD 3-Clause "New" or "Revised" License | 6 votes |
@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 #12
Source File: LocalTranslog.java From Elasticsearch with Apache License 2.0 | 6 votes |
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 #13
Source File: RunTable.java From audiveris with GNU Affero General Public License v3.0 | 6 votes |
/** * 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 #14
Source File: ReplayCacheTestProc.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
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 #15
Source File: WhiteListTrustManagerTest.java From tessera with Apache License 2.0 | 6 votes |
@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 File: EditorConfigProcessorTest.java From editorconfig-netbeans with MIT License | 5 votes |
@Test public void itDoesNotLoopOnTrimTrailingWhiteSpaceOperation() throws Exception { // Setup test file DataObject dataObject = null; File file = null; String content = "alert('Hello World!'); "; try { file = File.createTempFile(this.getClass().getSimpleName(), ".js"); Path path = Paths.get(Utilities.toURI(file)); Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); dataObject = DataObject.find(FileUtil.toFileObject(file)); } catch (IOException ex) { Exceptions.printStackTrace(ex); } // Setup EditorConfig MappedEditorConfig config = new MappedEditorConfig(); config.setEndOfLine(System.lineSeparator()); config.setTrimTrailingWhiteSpace(true); // Run processor EditorConfigProcessor proc = new EditorConfigProcessor(); FileInfo info = proc.excuteOperations(dataObject, config); assertEquals(true, info.isFileChangeNeeded()); /* Run the processor a second time and test that a file change is NOT needed (because it has been already performed during the first run). */ proc.flushFile(info); info = proc.excuteOperations(dataObject, config); assertEquals(false, info.isFileChangeNeeded()); // Delete test file assertEquals(true, file.delete()); }
Example #17
Source File: SizeVerifyingReaderTest.java From monsoon with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void read() throws Exception { byte output[] = new byte[data.length]; try (FileChannel fd = FileChannel.open(file, StandardOpenOption.READ)) { try (FileReader reader = new SizeVerifyingReader(new FileChannelReader(fd, 0), data.length)) { int i = 0; while (i < output.length) i += reader.read(ByteBuffer.wrap(output, i, Integer.min(output.length - i, 128))); } } assertArrayEquals(data, output); }
Example #18
Source File: MultipartStream.java From oxygen with Apache License 2.0 | 5 votes |
private void readFile(byte[] line, MultipartItem item) throws IOException { Path path = Paths.get(BASE_DIR.getPath(), String.valueOf(SnowflakeId.defaultNextId())); cleaner.track(path); item.setPath(path); while (true) { Files.write(path, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND); line = readLine(); if (Arrays.equals(line, boundary) || Arrays.equals(line, endOfBoundary)) { break; } Files.write(path, LINE_SEPARATOR, StandardOpenOption.APPEND); } }
Example #19
Source File: Dotenv.java From openvidu with Apache License 2.0 | 5 votes |
public void write(Path envFile) throws IOException { List<String> outLines = new ArrayList<>(); int lineNumber = 1; for (String line : lines) { try { Pair<String, String> propValue = parseLine(envFile, line, lineNumber); if (propValue == null) { outLines.add(line); } else { outLines.add(propValue.getKey() + "=" + properties.get(propValue.getKey())); } } catch (DotenvFormatException e) { log.error("Previously parsed line is producing a parser error", e); } lineNumber++; } if (!additionalProperties.isEmpty()) { for (String prop : additionalProperties) { outLines.add(prop + "=" + properties.get(prop)); } } Files.write(envFile, outLines, Charset.defaultCharset(), StandardOpenOption.CREATE); }
Example #20
Source File: FilesNewByteChannelTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Test public void gitByteChannelPartialOverwriteFromBeginningTest() throws IOException { byte[] data = encodeASCII("test"); try(SeekableByteChannel channel = Files.newByteChannel(file, StandardOpenOption.WRITE)) { ByteBuffer buf = ByteBuffer.wrap(data); assertEquals(data.length, channel.write(buf)); } byte[] expect = new byte[ORIGINAL_TEXT_BYTES.length]; System.arraycopy(data, 0, expect, 0, data.length); System.arraycopy(ORIGINAL_TEXT_BYTES, data.length, expect, data.length, ORIGINAL_TEXT_BYTES.length - data.length); byte[] actual = Files.readAllBytes(file); assertArrayEquals(expect, actual); }
Example #21
Source File: TestFileSystemRepository.java From localization_nifi with Apache License 2.0 | 5 votes |
@Test public void testSize() throws IOException { final ContentClaim claim = repository.create(true); final Path path = getPath(claim); Files.createDirectories(path.getParent()); final byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(); try (final OutputStream out = Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) { out.write(data); } assertEquals(data.length, repository.size(claim)); }
Example #22
Source File: Sysfs.java From ev3dev-lang-java with MIT License | 5 votes |
/** * Method to write bytes in a path * * @param path path * @param value value to write * @return Result */ public static boolean writeBytes(final String path, final byte[] value) { try { Files.write(Paths.get(path), value, StandardOpenOption.WRITE); } catch (IOException e) { throw new RuntimeException("Unable to draw the LCD", e); } return true; }
Example #23
Source File: IgfsLocalSecondaryFileSystemTestAdapter.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public OutputStream openOutputStream(final String path, final boolean append) throws IOException { if (append) return Files.newOutputStream(path(path), StandardOpenOption.APPEND); else return Files.newOutputStream(path(path)); }
Example #24
Source File: ITRemoteRootDir.java From jdrivesync with Apache License 2.0 | 5 votes |
private void createTestData(String name) throws IOException { deleteDirectorySubtree(Paths.get(basePathTestData(), name)); Files.createDirectory(Paths.get(basePathTestData(), name)); Files.write(Paths.get(basePathTestData(), name, "test1.txt"), Arrays.asList("test1"), Charset.defaultCharset(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); Files.createDirectory(Paths.get(basePathTestData(), name, "folder")); Files.write(Paths.get(basePathTestData(), name, "folder", "test2.txt"), Arrays.asList("test2"), Charset.defaultCharset(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); }
Example #25
Source File: ComposableService.java From gitpitch with MIT License | 5 votes |
private boolean overwrite(PitchParams pp, Path mdPath, String stitched) { try { Files.write(mdPath, stitched.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING ); return true; } catch(Exception fex) { log.warn("overwrite: pp={}, write error={}", pp, fex); return false; } }
Example #26
Source File: KeytoolReaderP12Test.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Decodes the base64 encoded keystore and writes into new file * @param name base64 encoded keystore name */ private static void convertToPFX(String name) throws IOException{ File base64File = new File(SOURCE_DIRECTORY, name); File pkcs12File = new File(WORKING_DIRECTORY, name); byte[] input = Files.readAllBytes(base64File.toPath()); Files.write(pkcs12File.toPath(), Base64.getMimeDecoder(). decode(input), StandardOpenOption.CREATE); }
Example #27
Source File: RelinkerRule.java From buck with Apache License 2.0 | 5 votes |
private void writeVersionScript(ProcessExecutor executor, ImmutableSet<String> symbolsNeeded) throws IOException, InterruptedException { Symbols sym = getSymbols(executor, getBaseLibPath()); Set<String> defined = Sets.difference(sym.all, sym.undefined); String versionScript = getVersionScript(symbolsNeeded, defined, symbolWhitelist); Files.write( absolutify(getRelativeVersionFilePath()), versionScript.getBytes(Charsets.UTF_8), StandardOpenOption.CREATE); }
Example #28
Source File: FileUtils.java From twister2 with Apache License 2.0 | 5 votes |
public static boolean writeToFile(String filename, byte[] contents, boolean overwrite) { // default Files behavior is to overwrite. If we specify no overwrite then CREATE_NEW fails // if the file exist. This operation is atomic. OpenOption[] options = overwrite ? new OpenOption[]{} : new OpenOption[]{StandardOpenOption.CREATE_NEW}; try { Files.write(new File(filename).toPath(), contents, options); } catch (IOException e) { LOG.log(Level.SEVERE, "Failed to write content to file. ", e); return false; } return true; }
Example #29
Source File: ToolBox.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@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 #30
Source File: SegmentedJournal.java From atomix with Apache License 2.0 | 5 votes |
private FileChannel openChannel(File file) { try { return FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); } catch (IOException e) { throw new StorageException(e); } }