public static Path createFile( Path root, byte[] content, String one, String... names ) { Path ret = root.resolve( one ); for( String name : names ) { ret = ret.resolve( name ); } Filess.createDirectories( childGetParent( ret)); Filess.write( ret, content ); return ret; }
protected Path srcDir() { Path ret = src(); if( Files.exists( ret ) ) { return ret; } Filess.createDirectory( ret ); return ret; }
/** * Read all lines from a file as a {@code Stream}. Bytes from the file are * decoded into characters using the {@link StandardCharsets#UTF_8 UTF-8} * {@link Charset charset}. * <p> * This method works as if invoking it were equivalent to evaluating the * expression: * <pre>{@code * Files.lines(path, StandardCharsets.UTF_8) * }</pre> * * @param path the path to the file * @return the lines from the file as a {@code Stream} * @throws SecurityException In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkRead(String) checkRead} * method is invoked to check read access to the file. * @since 1.8 */ public static Stream<String> lines( Path path ) { return lines( path, StandardCharsets.UTF_8 ); } }
@Override public SaveResult put( Optional<String> idin, long version, InputStream is, long length ) { Log.info( "putting " + idin ); if( offline() ) { throw new NoElsewhereException( "" ); } String id = idin.orElse( UUID.randomUUID().toString()); Path host = getHost( id, version ); Filess.createDirectories( childGetParent( host )); // todo delete old Filess.copy( is, host, REPLACE_EXISTING ); Filess.setLastModifiedTime( host, FileTime.fromMillis( version ) ); return SaveResult.immediate( id ); }
@Override public void write( Elsewhere elsew, Path path ) { Filess.createDirectories( path ); } }
/** * Write lines of text to a file. Characters are encoded into bytes using * the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}. * <p> * This method works as if invoking it were equivalent to evaluating the * expression: * <pre>{@code * Files.write(path, lines, StandardCharsets.UTF_8, options); * }</pre> * * @param path the path to the file * @param lines an object to iterate over the char sequences * @param options options specifying how the file is opened * @return the path * @throws UnsupportedOperationException if an unsupported option is specified * @throws SecurityException In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkWrite(String) checkWrite} * method is invoked to check write access to the file. * @since 1.8 */ public static Path write( Path path, Iterable<? extends CharSequence> lines, OpenOption... options ) { return write( path, lines, StandardCharsets.UTF_8, options ); }
@Test @Category( Writable.class ) public void testWriteSmallChunks() { Path target = absTA(); Filess.write( target, Arrays.copyOfRange( CONTENT, 0, 3 ) ); Filess.write( target, Arrays.copyOfRange( CONTENT, 3, CONTENT.length ), APPEND ); assertThat( Filess.readAllBytes( target ) ).isEqualTo( CONTENT ); }
/** * Moves the content of source to path * This method works in mostly the manner * specified by the {@link java.nio.file.Files#move} method. * but: * source and target are absolute paths of the same filesystem * source path is sym link free * target does not exist * target parent does exist * * @param source * @param target */ default void move( EightyPath source, EightyPath target ) { FileTime lastModi = Filess.getLastModifiedTime( source ); Pathss.copyRecursive( source, target ); Pathss.deleteRecursive( source ); Filess.setLastModifiedTime( target, lastModi ); }
@Override public void get( String id, long version, OutputStream os ) { if( offline() ) { throw new NoElsewhereException( "" ); } if ( !exists( id, version )) { throw u( new NoSuchFileException( "file not on storage " + id ) ); } Filess.copy( getHost( id, version ), os ); }
Filess.setLastModifiedTime( childGetParent( source ), FileTime.from( Clock.systemUTC().instant() ) );
protected static boolean isKid( Path dir, Path kid ) { return Filess.list( dir ). filter( k -> k.equals( kid ) ). findAny().isPresent(); }
@Override public long getTotalSpace() { try { return Filess.getFileStore( root ).getTotalSpace(); } catch( IOException e ) { throw u( e ); } }
@Override public boolean remove( String id ) { if( offline() ) { throw new NoElsewhereException( "" ); } Path dir = childGetParent( getHost( id, 1L )); try ( DirectoryStream<Path> stream = Files.newDirectoryStream( dir )) { for ( Path kid : stream ) { Filess.delete( kid ); } } catch( IOException e ) { return false; } return true; }
public static Path createDir( Path root, String one, String... names ) { Path ret = root.resolve( one ); for( String name : names ) { ret = ret.resolve( name ); } Filess.createDirectories( ret ); return ret; } }
public Path fileTB() { Path ret = absTB(); if( !Files.exists( ret ) ) { Filess.write( ret, CONTENT ); } return ret; }
protected Path otherProviderFileA() { Path ret = otherProviderAbsA(); if( !Files.exists( ret ) ) { Filess.createDirectories( childGetParent( ret ) ); Filess.write( ret, CONTENT ); } return ret; }
protected Path symLinkSpace() { Path ret = absT().resolve( "forsyml" ); Filess.createDirectories( ret ); return ret; }
protected Path orig() { Path ret = dirTA().resolve( "orig" ); if( !Files.exists( ret ) ) { Filess.write( ret, CONTENT ); } return ret; }
protected Path targetFile() { Path target = absT().resolve( "targetSpace" ).resolve( "target" ); if( Files.exists( target ) ) { return target; } Filess.createDirectories( childGetParent( target ) ); Filess.write( target, CONTENT ); return target; }