List of usage examples for java.nio.file Path getFileName
Path getFileName();
From source file:com.streamsets.pipeline.lib.io.TestSingleLineLiveFileReader.java
@Test public void testOneLineReadFromExactOffsetFullLinesNoTruncate() throws Exception { Path file = createFile(Arrays.asList("Hello1\n", "Hello2\n")); LiveFile lf = new LiveFile(file); LiveFileReader lfr = new SingleLineLiveFileReader( LogRollModeFactory.REVERSE_COUNTER.get(file.getFileName().toString(), ""), null, lf, Charset.defaultCharset(), 7, 10); Assert.assertTrue(lfr.hasNext());/*from w ww . ja va 2 s.co m*/ LiveFileChunk chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(7, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello2\n", readChunk(chunk)); Assert.assertEquals(14, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(14, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(14, lfr.getOffset()); Files.move(file, Paths.get(file.getParent().toString(), UUID.randomUUID().toString())); Thread.sleep(SingleLineLiveFileReader.REFRESH_INTERVAL + 1); Assert.assertFalse(lfr.hasNext()); lfr.close(); }
From source file:ch.ifocusit.livingdoc.plugin.publish.HtmlPostProcessor.java
public String getPageTitle(Path path) { String pageContent = null;//from w w w . ja v a 2 s .c om try { pageContent = IOUtils.readFull(new FileInputStream(path.toFile())); } catch (FileNotFoundException e) { throw new IllegalStateException("Unable to read page title !", e); } try { if (isAdoc(path)) { return getTitle(asciidoctor, pageContent).orElseThrow( () -> new IllegalStateException("top-level heading or title meta information must be set")); } // try to read h1 tag return tagText(pageContent, "h1"); } catch (IllegalStateException e) { return FilenameUtils.removeExtension(path.getFileName().toString()); } }
From source file:codes.thischwa.c5c.impl.LocalConnector.java
/** * Builds the folder and check it./*from w w w . ja v a 2s .c o m*/ * * @param backendPath the url path * @return the file * @throws FilemanagerException the known exception */ private Path buildRealPathAndCheck(String backendPath) throws FilemanagerException { Path parentFolder = buildRealPath(backendPath); if (!Files.exists(parentFolder)) { logger.error("Source file not found: {}", parentFolder.toAbsolutePath()); FilemanagerException.Key key = (Files.isDirectory(parentFolder)) ? FilemanagerException.Key.DirectoryNotExist : FilemanagerException.Key.FileNotExists; throw new FilemanagerException(FilemanagerAction.CREATEFOLDER, key, parentFolder.getFileName().toString()); } return parentFolder; }
From source file:at.tfr.securefs.process.ProcessFilesTest.java
private void generateHierachyCopy(ProcessFiles pf, ProcessFilesData cfd) throws IOException { cfd.setFromRootPath(fromRoot.toString()).setToRootPath(toRoot.toString()).setAllowOverwriteExisting(false) .setUpdate(false).setProcessActive(true); // When: copy of fromRoot to toRoot pf.copy(fromRoot, toRoot, cp, newSecret, cfd); // Then: a target files are created in same hierarchy like sourceFiles: Path subDir = toRoot;//from w w w.j a va2 s . c om Path someFile = toRoot; for (int i = 0; i < MAX_DIR_DEPTH; i++) { subDir = Files.createDirectories(subDir.resolve(SUBDIR_PFX + i)); } someFile = generateSomeFile(toRoot); Assert.assertTrue("subpaths are not created: " + subDir, Files.exists(subDir)); Assert.assertTrue("target file not created: " + someFile, Files.exists(someFile)); // Then: the content of target file is decryptable with newSecret // and equals content of source file byte[] buf = someFile.getFileName().toString().getBytes(); try (InputStream is = cp.getDecrypter(someFile, newSecret)) { IOUtils.readFully(is, buf); } Assert.assertEquals("failed to decrypt data", someFile.getFileName().toString(), new String(buf)); }
From source file:com.streamsets.pipeline.lib.io.TestSingleLineLiveFileReader.java
@Test public void testMultiLineLineReadFromBeginningLastLineNoEOLNoTruncate() throws Exception { Path file = createFile(Arrays.asList("Hello1\n", "Hello2")); LiveFile lf = new LiveFile(file); LiveFileReader lfr = new SingleLineLiveFileReader( LogRollModeFactory.REVERSE_COUNTER.get(file.getFileName().toString(), ""), null, lf, Charset.defaultCharset(), 0, 20); Assert.assertTrue(lfr.hasNext());/* w w w . ja v a2 s .c o m*/ LiveFileChunk chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(0, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello1\n", readChunk(chunk)); Assert.assertEquals(7, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(7, lfr.getOffset()); Files.move(file, Paths.get(file.getParent().toString(), UUID.randomUUID().toString())); Thread.sleep(SingleLineLiveFileReader.REFRESH_INTERVAL + 1); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(7, chunk.getOffset()); Assert.assertEquals(13, lfr.getOffset()); Assert.assertEquals("Hello2", readChunk(chunk)); Assert.assertFalse(lfr.hasNext()); Assert.assertEquals(13, lfr.getOffset()); lfr.close(); }
From source file:com.streamsets.pipeline.lib.io.TestSingleLineLiveFileReader.java
@Test public void testOneLineReadFromBeginningLastLineNoEOLNoTruncate() throws Exception { Path file = createFile(Arrays.asList("Hello1\n", "Hello2")); LiveFile lf = new LiveFile(file); LiveFileReader lfr = new SingleLineLiveFileReader( LogRollModeFactory.REVERSE_COUNTER.get(file.getFileName().toString(), ""), null, lf, Charset.defaultCharset(), 0, 10); Assert.assertTrue(lfr.hasNext());// w w w .j ava 2 s. c o m LiveFileChunk chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(0, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello1\n", readChunk(chunk)); Assert.assertEquals(7, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(7, lfr.getOffset()); Files.move(file, Paths.get(file.getParent().toString(), UUID.randomUUID().toString())); Thread.sleep(SingleLineLiveFileReader.REFRESH_INTERVAL + 1); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(7, chunk.getOffset()); Assert.assertEquals(6, chunk.getLength()); Assert.assertEquals("Hello2", readChunk(chunk)); Assert.assertEquals(13, lfr.getOffset()); Assert.assertFalse(lfr.hasNext()); Assert.assertEquals(13, lfr.getOffset()); lfr.close(); }
From source file:org.elasticsearch.plugins.PluginManagerIT.java
/** creates a plugin .zip and returns the url for testing */ private String createPlugin(final Path structure, String... properties) throws IOException { writeProperties(structure, properties); Path zip = createTempDir().resolve(structure.getFileName() + ".zip"); try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) { Files.walkFileTree(structure, new SimpleFileVisitor<Path>() { @Override//from ww w . j a va 2 s.co m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { stream.putNextEntry(new ZipEntry(structure.relativize(file).toString())); Files.copy(file, stream); return FileVisitResult.CONTINUE; } }); } if (randomBoolean()) { writeSha1(zip, false); } else if (randomBoolean()) { writeMd5(zip, false); } return zip.toUri().toURL().toString(); }
From source file:org.elasticsearch.plugins.PluginManagerIT.java
/** creates a plugin .zip and bad checksum file and returns the url for testing */ private String createPluginWithBadChecksum(final Path structure, String... properties) throws IOException { writeProperties(structure, properties); Path zip = createTempDir().resolve(structure.getFileName() + ".zip"); try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) { Files.walkFileTree(structure, new SimpleFileVisitor<Path>() { @Override//from w ww .j av a 2 s . c om public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { stream.putNextEntry(new ZipEntry(structure.relativize(file).toString())); Files.copy(file, stream); return FileVisitResult.CONTINUE; } }); } if (randomBoolean()) { writeSha1(zip, true); } else { writeMd5(zip, true); } return zip.toUri().toURL().toString(); }
From source file:com.streamsets.pipeline.lib.io.TestSingleLineLiveFileReader.java
@Test public void testOneLineReadFromBeginningFullLinesNoTruncate() throws Exception { Path file = createFile(Arrays.asList("Hello1\n", "Hello2\n")); LiveFile lf = new LiveFile(file); LiveFileReader lfr = new SingleLineLiveFileReader( LogRollModeFactory.REVERSE_COUNTER.get(file.getFileName().toString(), ""), null, lf, Charset.defaultCharset(), 0, 10); Assert.assertTrue(lfr.hasNext());/* w w w .j av a 2 s . c om*/ LiveFileChunk chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(0, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello1\n", readChunk(chunk)); Assert.assertEquals(7, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(7, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello2\n", readChunk(chunk)); Assert.assertEquals(14, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(14, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(14, lfr.getOffset()); Files.move(file, Paths.get(file.getParent().toString(), UUID.randomUUID().toString())); Thread.sleep(SingleLineLiveFileReader.REFRESH_INTERVAL + 1); Assert.assertFalse(lfr.hasNext()); Assert.assertEquals(14, lfr.getOffset()); // next() after EOF returns null Assert.assertNull(lfr.next(0)); lfr.close(); }
From source file:com.streamsets.pipeline.lib.io.TestSingleLineLiveFileReader.java
@Test public void testOneLineReadFromBeginningFullLinesTruncate() throws Exception { Path file = createFile(Arrays.asList("Hello123456\n", "Hello2\n")); LiveFile lf = new LiveFile(file); LiveFileReader lfr = new SingleLineLiveFileReader( LogRollModeFactory.REVERSE_COUNTER.get(file.getFileName().toString(), ""), null, lf, Charset.defaultCharset(), 0, 10); Assert.assertTrue(lfr.hasNext());/* w w w . jav a 2s .com*/ LiveFileChunk chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertTrue(chunk.isTruncated()); Assert.assertEquals(0, chunk.getOffset()); Assert.assertEquals(10, chunk.getLength()); Assert.assertEquals("Hello12345", readChunk(chunk)); Assert.assertEquals(-10, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNotNull(chunk); Assert.assertFalse(chunk.isTruncated()); Assert.assertEquals(12, chunk.getOffset()); Assert.assertEquals(7, chunk.getLength()); Assert.assertEquals("Hello2\n", readChunk(chunk)); Assert.assertEquals(19, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(19, lfr.getOffset()); Assert.assertTrue(lfr.hasNext()); chunk = lfr.next(0); Assert.assertNull(chunk); Assert.assertEquals(19, lfr.getOffset()); Files.move(file, Paths.get(file.getParent().toString(), UUID.randomUUID().toString())); Thread.sleep(SingleLineLiveFileReader.REFRESH_INTERVAL + 1); Assert.assertFalse(lfr.hasNext()); Assert.assertEquals(19, lfr.getOffset()); lfr.close(); }