RPC-GWT序列化/ java.util.Date编码

时间:2016-10-26 09:49:22

标签: java gwt encoding

我正在创建一个序列化GWT请求的脚本,我在编码日期值时遇到问题,以符合RPC-GWT标准。通过HTTP日志,我注意到日期值被转换为一些7个字符的字符串,但我无法识别这些模式使用的算法。

有谁知道使用哪种算法来加密这些日期值?

2 个答案:

答案 0 :(得分:4)

这些日期值被序列化为long values =>自纪元(1970年1月1日)以来的毫秒,然后以打包的形式序列化为base64,以成为更短的字符串。这看一下:

/**
   * Parse a string containing a base-64 encoded version of a long value.
   *
   * Keep this synchronized with the version in Base64Utils.
   */
  static long longFromBase64(String value) {
    int pos = 0;
    long longVal = base64Value(value.charAt(pos++));
    int len = value.length();
    while (pos < len) {
      longVal <<= 6;
      longVal |= base64Value(value.charAt(pos++));
    }
    return longVal;
  }
  /**
   * Return an optionally single-quoted string containing a base-64 encoded
   * version of the given long value.
   *
   * Keep this synchronized with the version in Base64Utils.
   */
  static String longToBase64(long value) {
    // Convert to ints early to avoid need for long ops
    int low = (int) (value & 0xffffffff);
    int high = (int) (value >> 32);
    StringBuilder sb = new StringBuilder();
    boolean haveNonZero = base64Append(sb, (high >> 28) & 0xf, false);
    haveNonZero = base64Append(sb, (high >> 22) & 0x3f, haveNonZero);
    haveNonZero = base64Append(sb, (high >> 16) & 0x3f, haveNonZero);
    haveNonZero = base64Append(sb, (high >> 10) & 0x3f, haveNonZero);
    haveNonZero = base64Append(sb, (high >> 4) & 0x3f, haveNonZero);
    int v = ((high & 0xf) << 2) | ((low >> 30) & 0x3);
    haveNonZero = base64Append(sb, v, haveNonZero);
    haveNonZero = base64Append(sb, (low >> 24) & 0x3f, haveNonZero);
    haveNonZero = base64Append(sb, (low >> 18) & 0x3f, haveNonZero);
    haveNonZero = base64Append(sb, (low >> 12) & 0x3f, haveNonZero);
    base64Append(sb, (low >> 6) & 0x3f, haveNonZero);
    base64Append(sb, low & 0x3f, true);
    return sb.toString();
  }
  private static boolean base64Append(StringBuilder sb, int digit, boolean haveNonZero) {
    if (digit > 0) {
      haveNonZero = true;
    }
    if (haveNonZero) {
      int c;
      if (digit < 26) {
        c = 'A' + digit;
      } else if (digit < 52) {
        c = 'a' + digit - 26;
      } else if (digit < 62) {
        c = '0' + digit - 52;
      } else if (digit == 62) {
        c = '$';
      } else {
        c = '_';
      }
      sb.append((char) c);
    }
    return haveNonZero;
  }
  // Assume digit is one of [A-Za-z0-9$_]
  private static int base64Value(char digit) {
    if (digit >= 'A' && digit <= 'Z') {
      return digit - 'A';
    }
    // No need to check digit <= 'z'
    if (digit >= 'a') {
      return digit - 'a' + 26;
    }
    if (digit >= '0' && digit <= '9') {
      return digit - '0' + 52;
    }
    if (digit == '$') {
      return 62;
    }
    // digit == '_'
    return 63;

 }

我是在Colin Alworth

的指导下自己在PHP中完成的

答案 1 :(得分:1)

有关更多信息,GWT项目发布了包含类gwt-user的库Base64Utilshttps://mvnrepository.com/artifact/com.google.gwt/gwt-user)可以帮助编码/解码到Base64,如下所述:http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/server/Base64Utils.html

我使用以下代码将日期转换为GWT Base64字符串(7个字符):

public String dateToGWTString(Date date) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    try {
        Date dateWithoutTime = simpleDateFormat.parse(simpleDateFormat.format(date));
        return Base64Utils.toBase64(dateWithoutTime.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return null;
}