Arthas

Arthas的基础命令

2021-06-09  本文已影响0人  晴天哥_王志

系列

开篇

arthas本身提供一些基础的linux命令,熟悉linux命令行的应该都比较熟悉,因为这趴内容也不是特别难,所以就一次性多讲解几个命令。

命令解析

public class CatCommand extends AnnotatedCommand {

    @Override
    public void process(CommandProcess process) {

        for (String file : files) {
            File f = new File(file);
            try {
                String fileToString = FileUtils.readFileToString(f,
                        encoding == null ? Charset.defaultCharset() : Charset.forName(encoding));
                process.appendResult(new CatModel(file, fileToString));
            } catch (IOException e) {
                logger.error("cat read file error. name: " + file, e);
                process.end(1, "cat read file error: " + e.getMessage());
                return;
            }
        }

        process.end();
    }

    public static String readFileToString(File file, Charset encoding) throws IOException {
        FileInputStream stream = new FileInputStream(file);
        try {
            Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[8192];
            int read;
            while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                builder.append(buffer, 0, read);
            }
            return builder.toString();
        } finally {
            stream.close();
        }
    }
}
public class Base64Command extends AnnotatedCommand {

    @Override
    public void process(CommandProcess process) {
       
        InputStream input = null;

        try {
            input = new FileInputStream(f);
            byte[] bytes = IOUtils.getBytes(input);

            ByteBuf convertResult = null;
            // 使用Netty的Unpooled转换成ByteBuf对象
            // 使用Netty的Base64进行加密和解密
            if (this.decode) {
                convertResult = Base64.decode(Unpooled.wrappedBuffer(bytes));
            } else {
                convertResult = Base64.encode(Unpooled.wrappedBuffer(bytes));
            }

            if (this.output != null) {
                // 通过Netty的ByteBuf对象进行操作并进行输出
                int readableBytes = convertResult.readableBytes();
                OutputStream out = new FileOutputStream(this.output);
                convertResult.readBytes(out, readableBytes);
                process.appendResult(new Base64Model(null));
            } else {
                String base64Str = convertResult.toString(CharsetUtil.UTF_8);
                process.appendResult(new Base64Model(base64Str));
            }
        } catch (IOException e) {

        } finally {
            IOUtils.close(input);
        }

        process.end();
    }

    public static byte[] getBytes(InputStream input) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        copy(input, result);
        result.close();
        return result.toByteArray();
    }
}
public class PwdCommand extends AnnotatedCommand {
    @Override
    public void process(CommandProcess process) {
        String path = new File("").getAbsolutePath();
        process.appendResult(new PwdModel(path));
        process.end();
    }
}

唠嗑

上一篇 下一篇

猜你喜欢

热点阅读