1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java Wed Apr 30 15:04:10 2014 +0200
1.3 @@ -0,0 +1,87 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.io.InputStream;
1.25 +import java.net.URL;
1.26 +import java.util.Enumeration;
1.27 +
1.28 +/**
1.29 + *
1.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 + */
1.32 +public class Resources {
1.33 + public static String loadKO() throws IOException {
1.34 + InputStream is = Resources.class.getResourceAsStream("ko.js");
1.35 + return readIS(is, false);
1.36 + }
1.37 +
1.38 + static String loadClazz() throws IOException {
1.39 + InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
1.40 + return readIS(is, false);
1.41 + }
1.42 +
1.43 + private static String readIS(InputStream is, boolean asString) throws IOException {
1.44 + if (is == null) {
1.45 + return "No resource found!";
1.46 + }
1.47 + byte[] arr = new byte[4092];
1.48 + int len = is.read(arr);
1.49 + if (len < 5) {
1.50 + return "No data read! Len: " + len;
1.51 + }
1.52 +
1.53 + if (asString) {
1.54 + return new String(arr, 0, len, "UTF-8").toString().toString();
1.55 + }
1.56 +
1.57 + StringBuilder sb = new StringBuilder();
1.58 + sb.append("[");
1.59 + for (int i = 0; i < len; i++) {
1.60 + sb.append(arr[i]).append(", ");
1.61 + }
1.62 +
1.63 + return sb.toString().toString();
1.64 + }
1.65 + static long bytesToLong(byte b1, byte b2, int shift) {
1.66 + return (((long)b1 << 56) +
1.67 + ((long)b2 & 255) << 48) >> shift;
1.68 + }
1.69 +
1.70 + static String loadHello() throws IOException {
1.71 + Enumeration<URL> en;
1.72 + try {
1.73 + en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
1.74 + } catch (SecurityException ex) {
1.75 + return "SecurityException";
1.76 + }
1.77 + StringBuilder sb = new StringBuilder();
1.78 + while (en.hasMoreElements()) {
1.79 + URL url = en.nextElement();
1.80 + sb.append(readIS(url.openStream(), true));
1.81 + }
1.82 + return sb.toString().toString();
1.83 + }
1.84 + static String loadJustHello() throws IOException {
1.85 + URL url = Resources.class.getResource("/META-INF/ahoj");
1.86 + StringBuilder sb = new StringBuilder();
1.87 + sb.append(readIS(url.openStream(), true));
1.88 + return sb.toString().toString();
1.89 + }
1.90 +}