summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/Base64.java
blob: 9ea9a02403fa971116bd38f3cc2c97216a6bf1c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package de.lmu.ifi.dbs.elki.utilities;
/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2012
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Class to wrap various Base64 encoders that could be available.
 * 
 * This is a rather ugly hack; it would maybe have been sensible to just import
 * one of the publicly available (and fast) Base64 encoders. The expectation was
 * that at some point, Oracle will acutally include a public and fast Base64
 * encoder in Java.
 * 
 * @author Erich Schubert
 */
public final class Base64 {
  /**
   * Instance of sun.misc.BASE64Encoder
   */
  private static Object sunj6i;

  /**
   * Encode method
   */
  private static Method sunj6m;

  /**
   * Instance of java.util.prefs.Base64
   */
  private static Object jup6i;

  /**
   * Encode method
   */
  private static Method jup6m;

  // Initialize
  static {
    // Try Java 6
    {
      try {
        Class<?> c = ClassLoader.getSystemClassLoader().loadClass("sun.misc.BASE64Encoder");
        sunj6i = c.newInstance();
        sunj6m = c.getMethod("encode", byte[].class);
      }
      catch(Throwable e) {
        // de.lmu.ifi.dbs.elki.logging.LoggingUtil.exception(e);
        // Ignore.
        sunj6i = null;
        sunj6m = null;
      }
    }
    // Try private class in Java6 preferences
    {
      try {
        Class<?> c = ClassLoader.getSystemClassLoader().loadClass("java.util.prefs.Base64");
        Constructor<?> cons = c.getDeclaredConstructor();
        cons.setAccessible(true);
        jup6i = cons.newInstance();
        jup6m = c.getDeclaredMethod("byteArrayToBase64", byte[].class);
        jup6m.setAccessible(true);
      }
      catch(Throwable e) {
        // de.lmu.ifi.dbs.elki.logging.LoggingUtil.exception(e);
        // Ignore.
        jup6i = null;
        jup6m = null;
      }
    }
    if(sunj6i == null && jup6i == null) {
      de.lmu.ifi.dbs.elki.logging.LoggingUtil.warning("No usable Base64 encoders detected.");
    }
  }

  /**
   * Encode a string as Base64.
   * 
   * @param s Bytes to encode
   * @return Result string
   */
  public static final String encodeBase64(byte[] s) {
    if(jup6i != null && jup6m != null) {
      try {
        return (String) jup6m.invoke(jup6i, s);
      }
      catch(Exception e) {
        throw new RuntimeException("java.util.prefs.Base64 is not working.");
      }
    }
    if(sunj6i != null && sunj6m != null) {
      try {
        return (String) sunj6m.invoke(sunj6i, s);
      }
      catch(Exception e) {
        throw new RuntimeException("sun.misc.BASE64Encoder is not working.");
      }
    }
    throw new RuntimeException("No usable Base64 encoder detected.");
  }
}