• Java基础005 --- 安全管理器、可变参数等


    Java大小端

    大端:数据高字节保存在内存低地址中,数据低字节保存在内存高地址中

    小端:数据高字节保存在内存高地址中,数据低字节保存在内存低地址中

    java虚拟机屏蔽了机器的大小端模式,默认都是大端模式;

    可以通过ByteOrder.nativeOrder获取机器模式,通过ByteBuffer的order方法获取及设置jvm模式

    示例:

    System.out.println(ByteOrder.nativeOrder());
    ByteBuffer buf = ByteBuffer.allocate(4);
    System.out.println(buf.order());
    System.out.println(buf.order(ByteOrder.LITTLE_ENDIAN).order());

    运行结果:

    LITTLE_ENDIAN
    BIG_ENDIAN
    LITTLE_ENDIAN

    32位和64位机器存储区别

    long型存储时,32位机器下是4字节。64位机器下是8字节

    StringBuilder和StringBuffer

    StringBuilder线程不安全,效率高;StringBuffer线程安全,效率低

    switch语句支持的类型

    String、enum、char、short、int、Character、Short、Integer

    可变参数

    示例:

    public static void fun(String...str) {
        System.out.println(Arrays.toString(str));
    }

    对于可变参方法和非可变参构成的重载方法,只要非可变参方法能满足要求,那么就优先选择非可变参方法

    示例:

    public class TestArgs {
        public static void main(String[] args) {
            fun("AA", "BB");
            fun("AA");
        }
    
        public static void fun(String...str) {
            System.out.println(Arrays.toString(str));
        }
    
        public static void fun(String str) {
            System.out.println("test" + str);
        }
    }

    运行结果:

    [AA, BB]
    testAA

    安全管理器及配置

    对于未知的Java应用程序,运行时可能存在未知风险,那么就需要对其运行的权限做控制,那么就需要启用安全管理器SecurityManager

    启用方法1:通过启动参数 -Djava.security.manager -Djava.security.policy "E:policy.txt"

    示例:设置test.txt为只读模式,然后通过安全管理器校验是否有写权限

    public class TestSecurityManager {
    
        public static void main(String[] args) {
            SecurityManager securityManager = System.getSecurityManager();
            if (securityManager != null) {
                securityManager.checkPermission(new FilePermission("	est.txt", "write"));
            }
            System.out.println("AAAAAAAAAAAA");
        }
    }

    编译:javac TestSecurityManager.java

    运行:java -Djava.security.manager TestSecurityManager

    Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "      est.txt" "write")

            at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)

            at java.security.AccessController.checkPermission(AccessController.java:884)

            at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)

            at TestSecurityManager.main(TestSecurityManager.java:8)

    启用方法2:通过System.setSecurityManager(new SecurityManager())

    示例:设置test.txt为只读模式,然后通过安全管理器校验是否有写权限

    public class TestSecurityManager {
    
        public static void main(String[] args) {
            System.setSecurityManager(new SecurityManager());
            SecurityManager securityManager = System.getSecurityManager();
            if (securityManager != null) {
                securityManager.checkPermission(new FilePermission("	est.txt", "write"));
            }
            System.out.println("AAAAAAAAAAAA");
        }
    }

    编译:javac TestSecurityManager.java

    运行:java TestSecurityManager

    Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "      est.txt" "write")

            at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)

            at java.security.AccessController.checkPermission(AccessController.java:884)

            at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)

            at TestSecurityManager.main(TestSecurityManager.java:9)

    test.txt
  • 相关阅读:
    Elasticsearch Mantanence Lessons Learned Today
    RabbitMQ Exchange & Queue Design Trade-off
    Understanding RabbitMQ Exchange & Queue
    Behind RabbitMQ Exchange Types
    七步,搭建基于Windows平台完美Jekyll博客环境
    How to Change RabbitMQ Queue Parameters in Production?
    Android Weekly Notes Issue #237
    Android Weekly Notes Issue #236
    Android Weekly Notes Issue #235
    Android Weekly Notes Issue #234
  • 原文地址:https://www.cnblogs.com/sniffs/p/12746024.html
Copyright © 2020-2023  润新知