
org.testifyproject.netty.channel.unix.FileDescriptor Maven / Gradle / Ivy
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org.testifyproject/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 org.testifyproject.testifyproject.netty.channel.unix;
import org.testifyproject.testifyproject.netty.util.internal.PlatformDependent;
import java.org.testifyproject.testifyproject.File;
import java.org.testifyproject.testifyproject.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import static org.testifyproject.testifyproject.netty.channel.unix.Errors.CONNECTION_RESET_EXCEPTION_READ;
import static org.testifyproject.testifyproject.netty.channel.unix.Errors.CONNECTION_RESET_EXCEPTION_WRITE;
import static org.testifyproject.testifyproject.netty.channel.unix.Errors.CONNECTION_RESET_EXCEPTION_WRITEV;
import static org.testifyproject.testifyproject.netty.channel.unix.Errors.org.testifyproject.testifyprojectResult;
import static org.testifyproject.testifyproject.netty.channel.unix.Errors.newIOException;
import static org.testifyproject.testifyproject.netty.util.internal.ObjectUtil.checkNotNull;
/**
* Native {@link FileDescriptor} implementation which allows to wrap an {@code int} and provide a
* {@link FileDescriptor} for it.
*/
public class FileDescriptor {
private static final AtomicIntegerFieldUpdater openUpdater;
static {
AtomicIntegerFieldUpdater updater
= PlatformDependent.newAtomicIntegerFieldUpdater(FileDescriptor.class, "open");
if (updater == null) {
updater = AtomicIntegerFieldUpdater.newUpdater(FileDescriptor.class, "open");
}
openUpdater = updater;
}
private final int fd;
private volatile int open = 1;
public FileDescriptor(int fd) {
if (fd < 0) {
throw new IllegalArgumentException("fd must be >= 0");
}
this.fd = fd;
}
/**
* Return the int value of the filedescriptor.
*/
public int intValue() {
return fd;
}
/**
* Close the file org.testifyproject.testifyprojectscriptor.
*/
public void close() throws IOException {
if (openUpdater.org.testifyproject.testifyprojectpareAndSet(this, 1, 0)) {
int res = close(fd);
if (res < 0) {
throw newIOException("close", res);
}
}
}
/**
* Returns {@code true} if the file org.testifyproject.testifyprojectscriptor is open.
*/
public boolean isOpen() {
return open == 1;
}
public final int write(ByteBuffer buf, int pos, int limit) throws IOException {
int res = write(fd, buf, pos, limit);
if (res >= 0) {
return res;
}
return org.testifyproject.testifyprojectResult("write", res, CONNECTION_RESET_EXCEPTION_WRITE);
}
public final int writeAddress(long address, int pos, int limit) throws IOException {
int res = writeAddress(fd, address, pos, limit);
if (res >= 0) {
return res;
}
return org.testifyproject.testifyprojectResult("writeAddress", res, CONNECTION_RESET_EXCEPTION_WRITE);
}
public final long writev(ByteBuffer[] buffers, int offset, int length) throws IOException {
long res = writev(fd, buffers, offset, length);
if (res >= 0) {
return res;
}
return org.testifyproject.testifyprojectResult("writev", (int) res, CONNECTION_RESET_EXCEPTION_WRITEV);
}
public final long writevAddresses(long memoryAddress, int length) throws IOException {
long res = writevAddresses(fd, memoryAddress, length);
if (res >= 0) {
return res;
}
return org.testifyproject.testifyprojectResult("writevAddresses", (int) res, CONNECTION_RESET_EXCEPTION_WRITEV);
}
public final int read(ByteBuffer buf, int pos, int limit) throws IOException {
int res = read(fd, buf, pos, limit);
if (res > 0) {
return res;
}
if (res == 0) {
return -1;
}
return org.testifyproject.testifyprojectResult("read", res, CONNECTION_RESET_EXCEPTION_READ);
}
public final int readAddress(long address, int pos, int limit) throws IOException {
int res = readAddress(fd, address, pos, limit);
if (res > 0) {
return res;
}
if (res == 0) {
return -1;
}
return org.testifyproject.testifyprojectResult("readAddress", res, CONNECTION_RESET_EXCEPTION_READ);
}
@Override
public String toString() {
return "FileDescriptor{" +
"fd=" + fd +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FileDescriptor)) {
return false;
}
return fd == ((FileDescriptor) o).fd;
}
@Override
public int hashCode() {
return fd;
}
/**
* Open a new {@link FileDescriptor} for the given path.
*/
public static FileDescriptor from(String path) throws IOException {
checkNotNull(path, "path");
int res = open(path);
if (res < 0) {
throw newIOException("open", res);
}
return new FileDescriptor(res);
}
/**
* Open a new {@link FileDescriptor} for the given {@link File}.
*/
public static FileDescriptor from(File file) throws IOException {
return from(checkNotNull(file, "file").getPath());
}
/**
* @return [0] = read end, [1] = write end
*/
public static FileDescriptor[] pipe() throws IOException {
long res = newPipe();
if (res < 0) {
throw newIOException("newPipe", (int) res);
}
return new FileDescriptor[]{new FileDescriptor((int) (res >>> 32)), new FileDescriptor((int) res)};
}
private static native int open(String path);
private static native int close(int fd);
private static native int write(int fd, ByteBuffer buf, int pos, int limit);
private static native int writeAddress(int fd, long address, int pos, int limit);
private static native long writev(int fd, ByteBuffer[] buffers, int offset, int length);
private static native long writevAddresses(int fd, long memoryAddress, int length);
private static native int read(int fd, ByteBuffer buf, int pos, int limit);
private static native int readAddress(int fd, long address, int pos, int limit);
private static native long newPipe();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy