1
/****************************************************************
2
* Licensed to the Apache Software Foundation (ASF) under one *
3
* or more contributor license agreements. See the NOTICE file *
4
* distributed with this work for additional information *
5
* regarding copyright ownership. The ASF licenses this file *
6
* to you under the Apache License, Version 2.0 (the *
7
* "License"); you may not use this file except in compliance *
8
* with the License. You may obtain a copy of the License at *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
12
* Unless required by applicable law or agreed to in writing, *
13
* software distributed under the License is distributed on an *
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15
* KIND, either express or implied. See the License for the *
16
* specific language governing permissions and limitations *
17
* under the License. *
18
****************************************************************/
20
package org.apache.james.mime4j;
22
import java.io.ByteArrayInputStream;
23
import java.io.ByteArrayOutputStream;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import java.util.Random;
29
import org.apache.commons.io.output.NullOutputStream;
30
import org.apache.james.mime4j.codec.Base64InputStream;
31
import org.apache.james.mime4j.codec.CodecUtil;
33
public class Base64InputStreamBench {
35
public static void main(String[] args) throws Exception {
36
byte[] data = initData(2 * 1024 * 1024);
37
byte[] encoded = encode(data);
39
// decoder test to make sure everything is okay
41
testDecode(data, encoded);
45
OutputStream nullOut = new NullOutputStream();
47
for (int i = 0; i < 5; i++) {
48
ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
49
InputStream in = new Base64InputStream(ed);
50
CodecUtil.copy(in, nullOut);
56
long t0 = System.currentTimeMillis();
58
final int repetitions = 50;
59
for (int i = 0; i < repetitions; i++) {
60
ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
61
InputStream in = new Base64InputStream(ed);
62
CodecUtil.copy(in, nullOut);
65
long dt = System.currentTimeMillis() - t0;
66
long totalBytes = data.length * (long) repetitions;
68
double mbPerSec = (totalBytes / 1024.0 / 1024) / (dt / 1000.0);
70
System.out.println(dt + " ms");
71
System.out.println(totalBytes + " bytes");
72
System.out.println(mbPerSec + " mb/sec");
75
private static byte[] initData(int size) {
76
Random random = new Random(size);
77
byte[] data = new byte[size];
78
random.nextBytes(data);
82
private static byte[] encode(byte[] data) throws IOException {
83
InputStream in = new ByteArrayInputStream(data);
84
ByteArrayOutputStream out = new ByteArrayOutputStream();
85
CodecUtil.encodeBase64(in, out);
86
return out.toByteArray();
89
private static void testDecode(byte[] data, final byte[] encoded)
91
ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
92
InputStream in = new Base64InputStream(ed);
93
ByteArrayOutputStream out = new ByteArrayOutputStream();
94
CodecUtil.copy(in, out);
96
compare(data, out.toByteArray());
99
private static void compare(byte[] expected, byte[] actual) {
100
if (expected.length != actual.length)
101
throw new AssertionError("length: " + expected.length + ", "
104
for (int i = 0; i < expected.length; i++)
105
if (expected[i] != actual[i])
106
throw new AssertionError("value @ " + i);
b'\\ No newline at end of file'