converting Strings to base64Binary
in
Contributed Library Questions
•
8 months ago
Hello all, I'm afraid I'm back with another dose of naivety and confusion. I am writing some software to handle large data sets generated by my mass spectrometer. In order to view the results in the open source viewer called TOPPView I need to be able to convert my results to
xs:base64Binary to be incorporated into an XML file with the
.mzML file extension. It seems that there are ways to do this in Java, using the
Apache commons codec library. I have imported this into Processing but I am still struggling to make sense of it. There is example Java code at the end of
this document describing a closely related open MS data format. I deleted a few things that Processing objected to and it looks like it runs now but I can't figure out how to feed it strings to convert.
- import java.util.List;
- import java.util.Vector;
- import org.apache.commons.codec.DecoderException;
- import org.apache.commons.codec.EncoderException;
- import org.apache.commons.codec.binary.Base64;
- /**
- *
- * @author Kai Runte
- */
- public class Base64Util {
- public String floatListToBase64String(List floatList, boolean bigEndian) throws EncoderException {
- byte[] raw = floatListToByteArray(floatList, bigEndian);
- Base64 base64 = new Base64();
- byte[] encoded = base64.encode(raw);
- return new String(encoded);
- }
- /**
- * Returns a byte array representing the float values in the IEEE
- * 754 floating-point "single format" bit layout.
- * @param floatList a list of float values
- * @param bigEndian
- * @return a byte array representing the float values in the
- * IEEE 754 floating-point "single format" bit layout.
- */
- public byte[] floatListToByteArray(List floatList,
- boolean bigEndian) {
- int floatListSize = floatList.size();
- byte[] raw = new byte[floatListSize * 4];
- int jjj = 0;
- if (bigEndian) {
- for (int iii = 0; iii < floatListSize; iii++) {
- Float aFloat = (Float) floatList.get(iii);
- int ieee754 = Float.floatToIntBits(aFloat.floatValue());
- raw[jjj] = (byte) ((ieee754 >> 24) & 0xff);
- raw[jjj + 1] = (byte) ((ieee754 >> 16) & 0xff);
- raw[jjj + 2] = (byte) ((ieee754 >> 8) & 0xff);
- raw[jjj + 3] = (byte) ((ieee754) & 0xff);
- jjj += 4;
- }
- } else {
- for (int iii = 0; iii < floatListSize; iii++) {
- Float aFloat = (Float) floatList.get(iii);
- int ieee754 = Float.floatToIntBits(aFloat.floatValue());
- raw[jjj] = (byte) ((ieee754) & 0xff);
- raw[jjj + 1] = (byte) ((ieee754 >> 8) & 0xff);
- raw[jjj + 2] = (byte) ((ieee754 >> 16) & 0xff);
- raw[jjj + 3] = (byte) ((ieee754 >> 24) & 0xff);
- jjj += 4;
- }
- }
- return raw;
- }
- public List base64StringToFloatList(String base64String, boolean bigEndian) throws DecoderException {
- Base64 base64 = new Base64();
- byte[] encoded = base64String.getBytes();
- byte[] raw = base64.decode(encoded);
- List floatList = byteArrayToFloatList(raw, bigEndian);
- return floatList;
- }
- public List byteArrayToFloatList(byte[] raw,
- boolean bigEndian) {
- Vector floatList = new Vector();
- if (bigEndian) {
- for (int iii = 0; iii < raw.length; iii += 4) {
- int ieee754 = 0;
- ieee754 |= (((int) raw[iii]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii + 1]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii + 2]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii + 3]) & 0xff);
- float aFloat = Float.intBitsToFloat(ieee754);
- floatList.add(new Float(aFloat));
- }
- } else {
- for (int iii = 0; iii < raw.length; iii += 4) {
- int ieee754 = 0;
- ieee754 |= (((int) raw[iii + 3]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii + 2]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii + 1]) & 0xff);
- ieee754 <<= 8;
- ieee754 |= (((int) raw[iii]) & 0xff);
- float aFloat = Float.intBitsToFloat(ieee754);
- floatList.add(new Float(aFloat));
- }
- }
- return floatList;
- }
- }
Help plz?
NC
1