Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.DataInputStream;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.attribute.BasicFileAttributes;
- import java.util.Scanner;
- import javax.imageio.ImageIO;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.UnsupportedAudioFileException;
- public class Main {
- public static void main (String[] args) {
- Scanner scanner = new Scanner(System.console().reader());
- File root = null;
- FileInputStream reading = null;
- while (true) {
- if (scanner.hasNextLine()) {
- String next = scanner.nextLine();
- if (next.equals("/view-files")) {
- root = File.listRoots()[0];
- print(root);
- } else if (next.startsWith("/view-file ")) {
- try {
- int number = Integer.parseInt(next.substring(11));
- root = root.listFiles()[number];
- } catch (NumberFormatException nfe) {
- String name = next.substring(11);
- for (File f : root.listFiles()) {
- if (f.getName().endsWith(name)) {
- root = f;
- break;
- }
- if (f.getName().endsWith(name + "/")) {
- root = f;
- break;
- }
- }
- }
- print(root);
- } else if (next.startsWith("/move-up")) {
- root = root.getParentFile();
- print(root);
- } else if (next.startsWith("/inspect-file")) {
- if (!root.isFile()) continue;
- inspect(root);
- } else if (next.startsWith("/inspect-text")) {
- if (!root.isFile()) continue;
- inspectAsText(root);
- } else if (next.startsWith("/jump-to ")) {
- File f = new File(next.substring(9));
- if (f.exists()) {
- root = f;
- print(root);
- }
- } else if (next.startsWith("/view-root")) {
- System.out.println(root);
- } else if (next.startsWith("/open")) {
- try {
- reading = new FileInputStream(root);
- System.out.println("file opened");
- } catch (IOException ioe) {}
- } else if (next.startsWith("/read ")) {
- read(reading, Integer.parseInt(next.substring(6)));
- }
- if (root == null) {
- System.out.println("internal error: file accesed does not exist");
- root = File.listRoots()[0];
- }
- }
- }
- }
- public static void print (File file) {
- if (!file.isDirectory()) printFile(file);
- File[] files = file.listFiles();
- if (files == null) return;
- if (files.length == 0) {
- System.out.println("-> empty directory\n");
- return;
- }
- for (int i = 0; i < files.length; i++)
- System.out.println("[" + i + "] -> " + files[i] + (files[i].isDirectory() ? "/" : ""));
- System.out.println();
- }
- public static void printFile (File f) {
- BasicFileAttributes attrs;
- try {
- attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
- } catch (IOException ioe) {
- System.out.println(ioe);
- return;
- }
- System.out.println("-> path: " + f.toString());
- String name = f.getName();
- String ext = (name.contains(".") ? name.substring(name.lastIndexOf(".")) : "none");
- name = name.contains(".") ? name.substring(0, name.lastIndexOf(".")) : name;
- if (f.getName().startsWith(".")) {
- name = ext;
- }
- System.out.println("-> name: " + name);
- System.out.println("-> extension: " + ext);
- System.out.println();
- System.out.println("-> time created: " + attrs.creationTime());
- System.out.println("-> time last modified: " + attrs.lastModifiedTime());
- System.out.println("-> time last accessed: " + attrs.lastAccessTime());
- System.out.println();
- System.out.println("-> executable: " + f.canExecute());
- System.out.println("-> readable: " + f.canRead());
- System.out.println("-> writable: " + f.canWrite());
- System.out.println("-> hidden: " + f.isHidden());
- System.out.println();
- String type = "file";
- if (attrs.isSymbolicLink()) type = "symlink";
- if (attrs.isOther()) type = "other";
- System.out.println("-> type: " + type);
- System.out.println("-> file size: " + attrs.size());
- System.out.println();
- System.out.println("-> partition size: " + f.getTotalSpace());
- }
- private static void inspect (File f) {
- if (tryInspectImage(f)) return;
- if (tryInspectAudio(f)) return;
- try {
- DataInputStream d = new DataInputStream(new FileInputStream(f));
- StringBuilder sb = new StringBuilder();
- sb.append("-> ");
- int index = 0;
- int line = 0;
- int chunk = 0;
- while (true) {
- try {
- String b = Integer.toHexString(d.readByte());
- if (b.length() < 2) b = "0".concat(b);
- if (b.length() > 2) b = b.substring(b.length() - 2);
- sb.append(b);
- index++;
- if (index == 16) {
- index = 0;
- line++;
- if (line == 16) {
- line = 0;
- chunk++;
- if (chunk == 3) {
- System.out.println(sb.toString());
- System.out.println("\n...\n");
- return;
- }
- System.out.println(sb.toString());
- sb = new StringBuilder();
- }
- sb.append("\n-> ");
- } else {
- sb.append(" ");
- }
- } catch (EOFException eof) {
- System.out.println(sb.toString());
- }
- }
- } catch (IOException ioe) {
- System.out.println(ioe);
- }
- }
- private static void inspectAsText (File f) {
- try {
- DataInputStream d = new DataInputStream(new FileInputStream(f));
- StringBuilder sb = new StringBuilder();
- sb.append("-> ");
- int index = 0;
- int line = 0;
- int chunk = 0;
- while (true) {
- try {
- sb.append(toChar(d.readByte()));
- index++;
- if (index == 16) {
- index = 0;
- line++;
- if (line == 16) {
- line = 0;
- chunk++;
- if (chunk == 3) {
- System.out.println(sb.toString());
- System.out.println("\n-> ...\n");
- return;
- }
- System.out.println(sb.toString());
- sb = new StringBuilder();
- }
- sb.append("\n-> ");
- }
- } catch (EOFException eof) {
- System.out.println(sb.toString());
- }
- }
- } catch (IOException ioe) {
- System.out.println(ioe);
- }
- }
- private static boolean tryInspectImage (File f) {
- return tryInspectImage(f, true);
- }
- private static boolean tryInspectImage (File f, boolean loopInternally) {
- try {
- BufferedImage i = ImageIO.read(f);
- if (i == null) return false;
- System.out.println("-> width: " + i.getWidth());
- System.out.println("-> height: " + i.getHeight());
- System.out.println();
- return true;
- } catch (IOException ioe) {
- if (loopInternally) return tryInspectImage(f, false);
- return false;
- }
- }
- private static boolean tryInspectAudio (File f) {
- return tryInspectAudio(f, true);
- }
- private static boolean tryInspectAudio (File f, boolean loopInternally) {
- try {
- AudioFormat format = AudioSystem.getAudioInputStream(f).getFormat();
- System.out.println("-> encoding: " + format.getEncoding());
- String channels;
- if (format.getChannels() == 1) channels = "mono";
- else if (format.getChannels() == 2) channels = "stereo";
- else channels = format.getChannels() + " channels";
- System.out.println("-> channels: " + channels);
- System.out.println("-> sample rate: " + format.getSampleRate());
- System.out.println("-> sample size: " + format.getSampleSizeInBits() + "-bit");
- System.out.println("-> big-endian: " + format.isBigEndian());
- System.out.println();
- return true;
- } catch (IOException ioe) {
- if (loopInternally) return tryInspectImage(f, false);
- return false;
- } catch (UnsupportedAudioFileException uafe) {
- return false;
- }
- }
- private static char toChar (byte b) {
- if (b < 20) return '.';
- return (char) b;
- }
- private static void read (FileInputStream fs, int amt) {
- if (fs == null) return;
- try {
- for (int i = 0; i < amt; i++)
- System.out.println(Integer.toHexString(fs.read()));
- } catch (IOException ioe) {
- System.out.println("io error occured");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement