BASE64Decoder/ BASE64Encoder非线程安全非线程安全

 

package com.hoo.client;

import sun.misc.BASE64Encoder;

public class Test {

  public static void main(String[] args) {
    BASE64Encoder encoder = new sun.misc.BASE64Encoder();

    String str = "中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国";

    String right = encoder.encode(str.getBytes());
    System.out.println("right=" + right);
    Thread thread1 = new Thread(new Thread1(encoder, str, right));
    thread1.start();
    Thread thread2 = new Thread(new Thread2(encoder, str, right));
    thread2.start();
  }

}

class Thread1 implements Runnable {
  private BASE64Encoder encoder;
  private String str;
  private String right;

  public Thread1(BASE64Encoder encoder, String str, String right) {
    this.encoder = encoder;
    this.str = str;
    this.right = right;
  }

  public void run() {
    for (;;) {// 一直循环到出问题为止吧。
      String str1 = encoder.encode(str.getBytes());
      if (!right.equals(str1)) {
        System.err.println("wrong=" + str1);
        System.exit(0);
      }
    }
  }
}

class Thread2 implements Runnable {
  private BASE64Encoder encoder;
  private String str;
  private String right;

  public Thread2(BASE64Encoder encoder, String str, String right) {
    this.encoder = encoder;
    this.str = str;
    this.right = right;
  }

  public void run() {
    for (;;) {
      String str1 = encoder.encode(str.getBytes());
      if (!right.equals(str1)) {
        System.err.println("wrong=" + str1);
        System.exit(0);
      }
    }
  }
}

 

modify 2014-04-29

调整输出:

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Test {

  public static void main(String[] args) {
    BASE64Encoder encoder = new sun.misc.BASE64Encoder();

    String str = "中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国";

    String right = encoder.encode(str.getBytes());
    System.out.println("right=" + right);
    Thread thread1 = new Thread(new Thread1(encoder, str));
    thread1.start();
    Thread thread2 = new Thread(new Thread2(encoder, str));
    thread2.start();
  }
}

class Thread1 implements Runnable {
  private BASE64Encoder encoder;
  private String str;

  public Thread1(BASE64Encoder encoder, String str_old) {
    this.encoder = encoder;
    this.str = str_old;
  }

  public void run() {
    for (;;) {// 一直循环到出问题为止吧。
      String str1 = encoder.encodeBuffer(str.getBytes());
      
      String s_new = "";
      BASE64Decoder decode = new BASE64Decoder();
      byte[] b;
      try {
        b = decode.decodeBuffer(str1);
        s_new = new String(b);
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      if (!str.equals(s_new)) {
        System.err.println("wrong=" + s_new);
        System.exit(0);
      }
    }
  }
}

class Thread2 implements Runnable {
  private BASE64Encoder encoder;
  private String str;

  public Thread2(BASE64Encoder encoder, String str) {
    this.encoder = encoder;
    this.str = str;
  }

  public void run() {
    for (;;) {
      String str1 = encoder.encodeBuffer(str.getBytes());
      
      String s_new = "";
      BASE64Decoder decode = new BASE64Decoder();
      byte[] b;
      try {
        b = decode.decodeBuffer(str1);
        s_new = new String(b);
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      if (!str.equals(s_new)) {
        System.err.println("wrong=" + s_new);
        System.exit(0);
      }
    }
  }
}

 

 

你可能感兴趣的:(BASE64Encoder)