
br.com.objectos.io.AbstractRegularFile 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.checkNotNull;
import br.com.objectos.lang.Lang;
import br.com.objectos.lang.ShutdownHookListener;
import br.com.objectos.multirelease.Concrete;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.channels.FileChannel;
@Concrete(modifiers = "public final", simpleName = "RegularFile")
abstract class AbstractRegularFile
extends Node
implements
Comparable,
FileName,
InputStreamSource,
OutputStreamSource,
ShutdownHookListener {
private volatile Directory directory;
AbstractRegularFile() {}
@Override
public final R acceptFsObjectVisitor(
FsObjectVisitor visitor, P p) throws IOException {
return visitor.visitRegularFile(self(), p);
}
@Override
public final int compareTo(RegularFile o) {
if (o == null) {
return 1;
}
return compareToImpl(o);
}
@Override
public final void copyTo(Directory dest) throws IOException {
checkNotNull(dest, "dest == null");
RegularFile file;
file = dest.getOrCreateRegularFile(getName());
IOException rethrow;
rethrow = null;
FileChannel in;
in = openReadChannel();
FileChannel out;
out = null;
try {
out = file.openWriteChannel();
Io.copy(in, out);
} catch (IOException e) {
rethrow = e;
} finally {
rethrow = Lang.close(rethrow, in);
rethrow = Lang.close(rethrow, out);
}
Lang.rethrowIfNecessary(rethrow);
}
public final void copyTo(RegularFile dest) throws IOException {
checkNotNull(dest, "dest == null");
copyToImpl(dest);
}
@Override
public abstract void delete() throws IOException;
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof RegularFile)) {
return false;
}
RegularFile that = (RegularFile) obj;
return getClass().equals(that.getClass())
&& getDelegate().equals(that.getDelegate());
}
@Override
public final R execute(
InputStreamOperation operation)
throws IOException {
return Operation.execute(this, operation);
}
@Override
public final R execute(
InputStreamOperation1 operation,
T1 arg1)
throws IOException {
return Operation.execute(this, operation, arg1);
}
@Override
public final R execute(
InputStreamOperation2 operation,
T1 arg1, T2 arg2)
throws IOException {
return Operation.execute(this, operation, arg1, arg2);
}
@Override
public final long execute(
OutputStreamOperation operation,
T1 arg1)
throws IOException {
return Operation.execute(this, operation, arg1);
}
@Override
public final long execute(
OutputStreamOperation1 operation,
T1 arg1, T2 arg2)
throws IOException {
return Operation.execute(this, operation, arg1, arg2);
}
public final Directory getDirectory() {
if (directory == null) {
directory = getDirectory0();
}
return directory;
}
public abstract long getLastModifiedMillis() throws IOException;
@Override
public final boolean isRegularFile() {
return true;
}
@Override
public final void onShutdownHook() throws Exception {
delete();
}
@Override
public abstract InputStream openInputStream() throws IOException;
public abstract FileChannel openReadAndWriteChannel() throws IOException;
public abstract FileChannel openReadChannel() throws IOException;
public abstract FileChannel openWriteChannel() throws IOException;
public abstract long size() throws IOException;
@Override
public final RegularFile toRegularFile() {
return self();
}
public final void touch() throws IOException {
setLastModifiedTimeToNow();
}
@Override
public abstract URI toUri();
@Override
public final long transferTo(OutputStreamSource destination) throws IOException {
return Operation.transferTo(this, destination);
}
public final void truncate() throws IOException {
IOException rethrow;
rethrow = null;
FileChannel channel;
channel = openWriteChannel();
try {
channel.truncate(0);
} catch (IOException e) {
rethrow = e;
} finally {
rethrow = Lang.close(rethrow, channel);
}
Lang.rethrowIfNecessary(rethrow);
}
abstract int compareToImpl(RegularFile o);
abstract void copyToImpl(RegularFile dest) throws IOException;
abstract Directory getDirectory0();
final RegularFile self() {
return RegularFile.class.cast(this);
}
abstract void setLastModifiedTimeToNow() throws IOException;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy