/**
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see .
*/
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class FileDescriptorDemo {
private static FileDescriptor najdiFD(int fd) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor c = FileDescriptor.class.getDeclaredConstructor(Integer.TYPE);
c.setAccessible(true);
return c.newInstance(fd);
}
private static void čtemeSTDIN() throws Exception {
System.out.println("Java: čteme ze System.in");
int vstup = System.in.read();
System.out.println("Java: načteno: " + vstup);
}
private static void čtemeProcSelfFD() throws Exception {
String fd = "/proc/self/fd/4";
System.out.println("Java: čteme z " + fd);
try (FileInputStream fis = new FileInputStream(fd)) {
int vstup = fis.read();
System.out.println("Java: načteno: " + vstup);
Thread.sleep(10000); // lsof -p `pidof start`
}
}
private static void čtemeFD() throws Exception {
int fd = 5;
System.out.println("Java: čteme z FD " + fd);
try (FileInputStream fis = new FileInputStream(najdiFD(fd))) {
int vstup = fis.read();
System.out.println("Java: načteno: " + vstup);
System.out.println("Zděděno: " + System.inheritedChannel());
Thread.sleep(10000); // lsof -p `pidof start`
}
}
public static void main(String[] args) throws Exception {
// čtemeSTDIN();
// čtemeProcSelfFD();
čtemeFD();
}
}