Java tutorial
//package com.java2s; /* * File: xml_utils.java.java * * Copyright (C) 2002, Ruth Mikkelson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu> * Department of Mathematics, Statistics and Computer Science * University of Wisconsin-Stout * Menomonie, WI 54751, USA * * This work was supported by the Intense Pulsed Neutron Source Division * of Argonne National Laboratory, Argonne, IL 60439-4845, USA. * * For further information, see <http://www.pns.anl.gov/ISAW/> * * Modified: * * $Log$ * Revision 1.10 2004/03/11 23:07:13 dennis * Removed dependency on DataSet Attributes * * Revision 1.9 2004/01/22 02:19:05 bouzekc * Removed unused imports and local variables. * * Revision 1.8 2003/10/20 16:31:06 rmikk * Fixed javadoc errors * * Revision 1.7 2003/06/18 20:35:35 pfpeterson * Changed calls for NxNodeUtils.Showw(Object) to * DataSetTools.util.StringUtil.toString(Object) * * Revision 1.6 2003/03/03 16:49:16 pfpeterson * Changed SharedData.status_pane.add(String) to SharedData.addmsg(String) * * Revision 1.5 2002/11/27 23:14:07 pfpeterson * standardized header * * Revision 1.4 2002/11/12 03:36:24 dennis * Since Attributes are no longer mutable, this can't call setValue() * for Double, Float and Int Attributes (line 421). This must still be * modified to adapt to immutable Attributes. * * Revision 1.3 2002/07/10 19:35:23 rmikk * Change in documentation * * Revision 1.2 2002/06/17 22:46:19 rmikk * Prettied up the file. Made minor changes * * Revision 1.1 2002/06/14 21:25:44 rmikk * Common utilities for Implementers of the IXmlIO interface * */ import java.io.*; public class Main { private static final int fillchar = '='; private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 22223333333333444444444455 // 67890123456789012345678901 + "abcdefghijklmnopqrstuvwxyz" // 555555556666 // 234567890123 + "0123456789+/"; public static byte[] convertToB64(float[] a) { ByteArrayOutputStream os = new ByteArrayOutputStream(a.length * 4 + 2); DataOutputStream dou = new DataOutputStream(os); try { for (int i = 0; i < a.length; i++) dou.writeFloat(a[i]); return encode(os.toByteArray()); } catch (Exception s) { return null; } } public static byte[] convertToB64(int[] a) { ByteArrayOutputStream os = new ByteArrayOutputStream(a.length * 4 + 2); try { DataOutputStream dou = new DataOutputStream(os); for (int i = 0; i < a.length; i++) dou.writeInt(a[i]); return encode(os.toByteArray()); } catch (Exception s) { return null; } } public static byte[] encode(byte[] data) { int c; int len = data.length; StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4); for (int i = 0; i < len; ++i) { c = (data[i] >> 2) & 0x3f; ret.append(cvt.charAt(c)); c = (data[i] << 4) & 0x3f; if (++i < len) c |= (data[i] >> 4) & 0x0f; ret.append(cvt.charAt(c)); if (i < len) { c = (data[i] << 2) & 0x3f; if (++i < len) c |= (data[i] >> 6) & 0x03; ret.append(cvt.charAt(c)); } else { ++i; ret.append((char) fillchar); } if (i < len) { c = data[i] & 0x3f; ret.append(cvt.charAt(c)); } else { ret.append((char) fillchar); } } return (getBinaryBytes(ret.toString())); } private static byte[] getBinaryBytes(String str) { byte[] b = new byte[str.length()]; for (int i = 0; i < b.length; ++i) b[i] = (byte) str.charAt(i); return (b); } }