
br.com.objectos.io.Io Maven / Gradle / Ivy
/*
* Copyright (C) 2011-2021 Objectos Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.objectos.io;
import static br.com.objectos.lang.Lang.checkArgument;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.collections.ImmutableList;
import br.com.objectos.multirelease.Singleton;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
public final class Io {
/*
@startuml
' config
left to right direction
skinparam shadowing false
' actors
' usecases
usecase WatchDirectory as "Watch a Directory for
specific events.
Notify registered listener(s)
on the event occurance."
@enduml
*/
/*
@startuml
' config
hide empty members
' left to right direction
skinparam genericDisplay old
' skinparam monochrome true
skinparam shadowing false
' skinparam style strictuml
class Io {
+ {static} WatchService\lcreateWatchService(\lWatchServiceOption... options)\lthrows IOException
+ {static} LoggerOption\llogger(\lLogger logger)
+ {static} WatchServiceOption\lwatchDirectory(\lDirectory d,\lWatchServiceListener l,\lEventKind event)
}
interface WatchService extends o7.lang.ShutdownHookListener {
+ void start() throws IOException
+ void shutdown() throws IOException
}
interface WatchServiceBuilder {
}
interface WatchServiceOption {
}
@enduml
*/
public static final Mode BLOCKING = Mode.BLOCKING;
public static final EventKind CREATED = EventKind.CREATED;
public static final EventKind DELETED = EventKind.DELETED;
public static final EventKind MODIFIED = EventKind.MODIFIED;
public static final Mode NON_BLOCKING = Mode.NON_BLOCKING;
private static final int DEFAULT_BUFFER_SIZE = 8192;
private Io() {}
public static InputStreamSource asInputStreamSource(final InputStream inputStream) {
checkNotNull(inputStream, "inputStream == null");
return new AbstractInputStreamSource() {
@Override
public final InputStream openInputStream() throws IOException {
return inputStream;
}
};
}
public static InputStreamSource asInputStreamSource(final URL url) {
checkNotNull(url, "iurl == null");
return new AbstractInputStreamSource() {
@Override
public final InputStream openInputStream() throws IOException {
return url.openStream();
}
};
}
public static void copy(FileChannel source, FileChannel target) throws IOException {
checkNotNull(source, "source == null");
checkNotNull(target, "target == null");
long position;
position = 0;
long size;
size = source.size();
long remaining;
remaining = size;
while (remaining > 0) {
long count;
count = source.transferTo(position, remaining, target);
position = position + count;
remaining = size - position;
}
}
public static long copy(InputStream in, OutputStream out, byte[] buffer) throws IOException {
checkNotNull(in, "in == null");
checkNotNull(out, "out == null");
checkNotNull(buffer, "buffer == null");
return copyUnchecked(in, out, buffer);
}
public static Directory createTempDirectory() throws IOException {
Directory tmpDir;
tmpDir = Directory.systemTempDirectory();
return tmpDir.createTempDir();
}
public static WatchService createWatchService(WatchServiceOption... options) throws IOException {
return WatchServiceFactorySingleton.INSTANCE.create(options);
}
public static Directory getDirectory(File file) throws IOException {
return IoFsSingleton.INSTANCE.getDirectory(file);
}
public static Directory getDirectory(String path) throws IOException {
return IoFsSingleton.INSTANCE.getDirectory(path);
}
public static Directory getDirectory(String first, String... more) throws IOException {
return IoFsSingleton.INSTANCE.getDirectory(first, more);
}
public static String getExtension(String name) {
int lastIndex;
lastIndex = name.lastIndexOf('.');
if (lastIndex == -1) {
return "";
}
return name.substring(lastIndex + 1);
}
public static Directory getOrCreateDirectory(String path) throws IOException {
return IoFsSingleton.INSTANCE.getOrCreateDirectory(path);
}
public static RegularFile getRegularFile(Resource resource) throws IOException {
try {
URI uri;
uri = resource.toUri();
return getRegularFile(uri);
} catch (URISyntaxException e) {
throw new NotRegularFileException(e);
}
}
public static RegularFile getRegularFile(String path) throws IOException {
return IoFsSingleton.INSTANCE.getRegularFile(path);
}
public static RegularFile getRegularFile(URI uri) throws IOException {
return IoFsSingleton.INSTANCE.getRegularFile(uri);
}
public static PosixFileModeOption ownerExecutable() {
return IoImplSingleton.INSTANCE.ownerExecutable();
}
public static PosixFileModeOption ownerReadable() {
return IoImplSingleton.INSTANCE.ownerReadable();
}
public static PosixFileModeOption ownerWritable() {
return IoImplSingleton.INSTANCE.ownerWritable();
}
public static byte[] readByteArray(InputStreamSource source) throws IOException {
checkNotNull(source, "source == null");
return source.execute(ReadByteArray.INSTANCE);
}
public static ImmutableList readLines(
InputStreamSource source, Charset charset)
throws IOException {
checkNotNull(source, "source == null");
return source.execute(ReadLines.INSTANCE, charset);
}
public static ImmutableList readLinesUtf8(
InputStreamSource source)
throws IOException {
checkNotNull(source, "source == null");
return source.execute(ReadLinesUtf8.INSTANCE);
}
public static String readString(
InputStreamSource source, Charset charset) throws IOException {
checkNotNull(source, "source == null");
return source.execute(ReadString.INSTANCE, charset);
}
public static String readStringUtf8(InputStreamSource source) throws IOException {
checkNotNull(source, "source == null");
return source.execute(ReadStringUtf8.INSTANCE);
}
public static RecursePaths recursePaths() {
return RecursePaths.INSTANCE;
}
public static FsObject resolve(String path) {
return IoFsSingleton.INSTANCE.resolve(path);
}
public static FsObject resolve(String first, String... more) {
return IoFsSingleton.INSTANCE.resolve(first, more);
}
public static Directory systemTempDirectory() {
return Directory.systemTempDirectory();
}
public static void unzip(
Directory workingDirectory, RegularFile zipFile) throws IOException {
Unzip.unzip(workingDirectory, zipFile);
}
public static WatchServiceOption watchDirectory(
final Directory directory,
final WatchServiceListener listener,
final EventKind event) {
return new WatchServiceOption() {
@Override
public final void acceptWatchServiceBuilder(WatchServiceBuilder builder) {
builder.watchDirectory(directory, listener, event);
}
};
}
public static long writeByteArray(OutputStreamSource source, byte[] bytes) throws IOException {
checkNotNull(source, "source == null");
return source.execute(WriteByteArray.INSTANCE, bytes);
}
public static long writeInputStream(
OutputStreamSource source, InputStream inputStream) throws IOException {
checkNotNull(source, "source == null");
return source.execute(WriteInputStream.INSTANCE, inputStream);
}
public static long writeString(
OutputStreamSource source, String string, Charset charset) throws IOException {
checkNotNull(source, "source == null");
return source.execute(WriteString.INSTANCE, string, charset);
}
public static long writeStringUtf8(
OutputStreamSource source, String string) throws IOException {
checkNotNull(source, "source == null");
return source.execute(WriteStringUtf8.INSTANCE, string);
}
public static RegularFile zip(
Directory workingDirectory, NotFound zipFile,
ZipOption option,
String... files) throws IOException {
return Zip.zip(workingDirectory, zipFile, option, files);
}
static void checkBuffer(byte[] buffer) {
checkNotNull(buffer, "buffer == null");
checkArgument(buffer.length > 0, "buffer.length == 0");
}
static long copyUnchecked(InputStream in, OutputStream out, byte[] buffer) throws IOException {
long total;
total = 0;
int count;
count = in.read(buffer);
while (count > 0) {
out.write(buffer, 0, count);
total += count;
count = in.read(buffer);
}
return total;
}
static byte[] createBuffer() {
return new byte[DEFAULT_BUFFER_SIZE];
}
public enum Mode {
BLOCKING,
NON_BLOCKING;
}
@Singleton
abstract static class Fs {
public abstract Directory getDirectory(File file) throws IOException;
public final Directory getDirectory(String path) throws IOException {
checkNotNull(path, "path == null");
T delegate;
delegate = getDelegate(path);
return getDirectoryImpl(delegate);
}
public Directory getDirectory(String first, String[] more) throws IOException {
checkNotNull(first, "first == null");
checkNotNull(more, "more == null");
T delegate;
delegate = getDelegate(first, more);
return getDirectoryImpl(delegate);
}
public final Directory getOrCreateDirectory(String path) throws IOException {
checkNotNull(path, "path == null");
T delegate;
delegate = getDelegate(path);
if (!exists(delegate)) {
createDirectories(delegate);
return newDirectory(delegate);
}
if (!isDirectory(delegate)) {
throw new NotDirectoryException(path);
}
return newDirectory(delegate);
}
public final RegularFile getRegularFile(String path) throws IOException {
checkNotNull(path, "path == null");
T delegate;
delegate = getDelegate(path);
if (!exists(delegate)) {
throw new RegularFileNotFoundException(path);
}
if (!isRegularFile(delegate)) {
throw new NotRegularFileException(path);
}
return newRegularFile(delegate);
}
public final RegularFile getRegularFile(URI uri) throws IOException {
checkNotNull(uri, "uri == null");
T delegate;
delegate = getDelegate(uri);
if (!exists(delegate)) {
String message;
message = uri.toString();
throw new RegularFileNotFoundException(message);
}
if (!isRegularFile(delegate)) {
String message;
message = uri.toString();
throw new NotRegularFileException(message);
}
return newRegularFile(delegate);
}
public final FsObject resolve(String path) {
checkNotNull(path, "path == null");
T delegate;
delegate = getDelegate(path);
return resolve0(delegate);
}
public final FsObject resolve(String first, String[] more) {
checkNotNull(first, "first == null");
checkNotNull(more, "more == null");
T delegate;
delegate = getDelegate(first, more);
return resolve0(delegate);
}
abstract void createDirectories(T delegate) throws IOException;
abstract boolean exists(T delegate);
abstract T getDelegate(String path);
abstract T getDelegate(String path, String[] more);
abstract T getDelegate(URI uri);
final Directory getDirectoryImpl(T delegate) throws IOException {
if (!exists(delegate)) {
String message;
message = delegate.toString();
throw new DirectoryNotFoundException(message);
}
if (!isDirectory(delegate)) {
String message;
message = delegate.toString();
throw new NotDirectoryException(message);
}
return newDirectory(delegate);
}
abstract boolean isDirectory(T delegate);
abstract boolean isRegularFile(T delegate);
abstract Directory newDirectory(T delegate);
abstract NotFound newNotFound(T delegate);
abstract RegularFile newRegularFile(T delegate);
private FsObject resolve0(T delegate) {
if (isRegularFile(delegate)) {
return newRegularFile(delegate);
}
if (isDirectory(delegate)) {
return newDirectory(delegate);
}
return newNotFound(delegate);
}
}
@Singleton
abstract static class Impl {
public abstract PosixFileModeOption ownerExecutable();
public abstract PosixFileModeOption ownerReadable();
public abstract PosixFileModeOption ownerWritable();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy