
文章目录
一、base64 编码解码
1.1 基本的编码和解码
- base64 编码: 
  - 使用 base64.getencoder().encodetostring(originalinput.getbytes())对原始字符串进行编码。
- base64.getencoder()返回一个- base64.encoder实例,调用- encodetostring()方法将原始字符串的字节数组编码为base64字符串。
 
- 使用 
- base64 解码: 
  - 使用 base64.getdecoder().decode(encodedstring)对base64编码后的字符串进行解码。
- base64.getdecoder()返回一个- base64.decoder实例,调用- decode()方法将base64编码后的字符串解码为原始的字节数组。
- 使用 new string(decodedbytes)将解码后的字节数组转换为字符串。
 
- 使用 
- 注意事项: 
  - 在实际应用中,确保使用相同的编码和解码方法,以避免数据损坏或不正确的解码结果。
- java 8 中的 java.util.base64类提供了方便且高效的base64编码和解码功能,适用于处理字符串数据的安全传输和存储。
 
public static void main(string[] args) {
        // 原始字符串
        string originalinput = "hello world!";
        // 编码为base64
        string encodedstring = base64.getencoder().encodetostring(originalinput.getbytes());
        system.out.println("encoded string: " + encodedstring);
        // 解码base64
        byte[] decodedbytes = base64.getdecoder().decode(encodedstring);
        string decodedstring = new string(decodedbytes);
        system.out.println("decoded string: " + decodedstring);
    }
// 输出
encoded string: sgvsbg8gv29ybgqh
decoded string: hello world!
1.2 url 和文件名安全的编码解码器
- url 和文件名安全的 base64 编码: 
  - 使用 base64.geturlencoder().encodetostring(originalinput.getbytes("utf-8"))对原始字符串进行url和文件名安全的base64编码。
- base64.geturlencoder()返回一个- base64.encoder实例,调用- encodetostring()方法将原始字符串的字节数组编码为url和文件名安全的base64字符串。
 
- 使用 
- url 和文件名安全的 base64 解码: 
  - 使用 base64.geturldecoder().decode(encodedstring)对url和文件名安全的base64编码后的字符串进行解码。
- base64.geturldecoder()返回一个- base64.decoder实例,调用- decode()方法将base64编码后的字符串解码为原始的字节数组。
- 使用 new string(decodedbytes, "utf-8")将解码后的字节数组转换为字符串。
 
- 使用 
- 注意事项: 
  - url 和文件名安全的base64编码会使用 -替换+,并且使用_替换/,以确保编码结果可以安全地在url和文件名中使用。
- 如果使用的是不同的字符集编码(例如 utf-8),请确保在编码和解码过程中使用相同的字符集,以避免数据损坏或不正确的解码结果。
 
- url 和文件名安全的base64编码会使用 
public static void main(string[] args) throws unsupportedencodingexception {
        // 原始字符串
        string originalinput = "hello world!";
        // url 和文件名安全的 base64 编码
        string encodedstring = base64.geturlencoder().encodetostring(originalinput.getbytes("utf-8"));
        system.out.println("encoded string (url safe): " + encodedstring);
        // 解码 url 和文件名安全的 base64
        byte[] decodedbytes = base64.geturldecoder().decode(encodedstring);
        string decodedstring = new string(decodedbytes, "utf-8");
        system.out.println("decoded string: " + decodedstring);
    }
// 输出
encoded string (url safe): sgvsbg8gv29ybgqh
decoded string: hello world!
1.3 mime base64编码和解码
- mime base64 编码: 
  - 使用 base64.getmimeencoder().encodetostring(originalinput.getbytes("utf-8"))对原始字符串进行mime base64编码。
- base64.getmimeencoder()返回一个- base64.encoder实例,调用- encodetostring()方法将原始字符串的字节数组编码为mime base64字符串。
 
- 使用 
- mime base64 解码: 
  - 使用 base64.getmimedecoder().decode(encodedstring)对mime base64编码后的字符串进行解码。
- base64.getmimedecoder()返回一个- base64.decoder实例,调用- decode()方法将mime base64编码后的字符串解码为原始的字节数组。
- 使用 new string(decodedbytes, "utf-8")将解码后的字节数组转换为字符串。
 
- 使用 
- 注意事项: 
  - mime base64编码会在每行末尾添加换行符 \r\n,以便适应电子邮件等格式要求。
- 如果使用的是不同的字符集编码(例如 utf-8),请确保在编码和解码过程中使用相同的字符集,以避免数据损坏或不正确的解码结果。
 
- mime base64编码会在每行末尾添加换行符 
public static void main(string[] args) throws unsupportedencodingexception {
        // 原始字符串
        string originalinput = "hello world!";
        // mime base64 编码
        string encodedstring = base64.getmimeencoder().encodetostring(originalinput.getbytes("utf-8"));
        system.out.println("encoded string (mime):" + encodedstring);
        // 解码 mime base64
        byte[] decodedbytes = base64.getmimedecoder().decode(encodedstring);
        string decodedstring = new string(decodedbytes, "utf-8");
        system.out.println("decoded string: " + decodedstring);
    }
// 输出
encoded string (mime):sgvsbg8gv29ybgqh
decoded string: hello world!
二、optional类
-  创建 optional 对象 - optional.of(value):如果- value不为 null,则创建一个包含指定值的 optional 对象;如果- value为 null,则会抛出- nullpointerexception。
- optional.ofnullable(value):无论- value是否为 null,都会创建一个对应的 optional 对象。如果- value是 null,则创建一个空的 optional 对象。
 
-  检查是否有值 - ispresent():检查 optional 对象中是否包含值。
 
-  获取值 - get():如果 optional 对象中有值,则返回该值;否则抛出- nosuchelementexception。
 
-  处理空值 - orelse(defaultvalue):如果 optional 对象中有值,则返回该值;否则返回- defaultvalue。
- orelseget(supplier):如果 optional 对象中有值,则返回该值;否则调用- supplier提供的方法来获取默认值。
 
-  条件操作 - ifpresent():在 optional 对象非空时执行特定操作。
 
-  过滤值 - filter()方法过滤 optional 对象中的值。
 
-  映射值 - map()或- flatmap():对 optional 对象中的值进行映射操作。
 
public static void main(string[] args) {
        // 创建一个包含非空值的 optional 对象
        optional<string> optional1 = optional.of("hello");
        system.out.println("optional 1 value: " + optional1.get());
        // 创建一个可能为 null 的 optional 对象
        string nullablevalue = null;
        optional<string> optional2 = optional.ofnullable(nullablevalue);
        system.out.println("optional 2 is present? " + optional2.ispresent());
        // 获取 optional 对象中的值
        string value = optional1.get();
        system.out.println("value: " + value);
        // 检查 optional 对象中是否有值
        if (optional1.ispresent()) {
            system.out.println("optional contains value.");
        } else {
            system.out.println("optional is empty.");
        }
        // 使用 orelse 提供默认值
        string result1 = optional2.orelse("default value");
        system.out.println("result 1: " + result1);
        // 使用 orelseget 提供默认值
        string result2 = optional2.orelseget(() -> {
            // 处理逻辑,返回默认值
            return "default value from supplier";
        });
        system.out.println("result 2: " + result2);
        // 如果 optional 对象中有值,则执行操作
        optional1.ifpresent(val -> system.out.println("value is present: " + val));
        // 过滤值
        optional<string> filteredoptional = optional1.filter(val -> val.startswith("h"));
        system.out.println("filtered optional value: " + filteredoptional.orelse("not found"));
        // 映射值
        optional<string> transformedoptional = optional1.map(val -> val.touppercase());
        system.out.println("transformed optional value: " + transformedoptional.orelse("no value"));
        // 抛出异常
        try {
            value = optional2.orelsethrow(() -> new illegalargumentexception("value is required."));
            system.out.println("value: " + value);
        } catch (illegalargumentexception e) {
            system.out.println("exception: " + e.getmessage());
        }
    }
// 输出
optional 1 value: hello
optional 2 is present? false
value: hello
optional contains value.
result 1: default value
result 2: default value from supplier
value is present: hello
filtered optional value: hello
transformed optional value: hello
exception: value is required.
三、nashorn javascript
- 执行内联脚本:
public static void main(string[] args) throws exception {
    // 创建 nashorn 脚本引擎
    scriptengine engine = new scriptenginemanager().getenginebyname("nashorn");
   // 执行 javascript 代码
    engine.eval("print('hello nashorn!')");
}
- 传递变量:
public static void main(string[] args) throws exception {
   scriptengine engine = new scriptenginemanager().getenginebyname("nashorn");
   // 将 java 变量传递给 javascript
   engine.put("name", "alice");
   engine.eval("print('hello, ' + name + '!')");
}
- 高级特性: 
  - java 互操作性: javascript 代码可以直接与 java 类和对象交互。
- 函数式编程: nashorn 支持 lambda 表达式和流式 api,使其更符合现代 javascript 标准。
- 性能: 由于其现代化设计和优化技术,nashorn 通常比 rhino 更快。
 
// java 类
public class person {
    public string getname() {
        return "alice";
    }
}
// javascript 代码
engine.eval("var person = new person(); print(person.getname());");
- 命令行工具: 
  - nashorn 还配备了一个命令行工具 (jjs),用于直接从终端执行 javascript 文件,这对于测试和调试非常有用。
 
- nashorn 还配备了一个命令行工具 (
$ jjs script.js
- 与 java 应用程序集成: 
  - 可以将 nashorn 集成到 java 应用程序中,用于脚本支持、规则引擎和动态行为等多种用途。
- 提供了在 java 的稳健性和 javascript 的脚本能力之间进行灵活结合的能力。
 
懒惰也是天生的,勤奋需自己努力,一放松就懒了
 
             
                                             
                                             
                                             
                                            
发表评论