Forums > Java

embed data in patcher

August 28, 2006 | 5:58 pm

hello all,
is there a way to embed a complex data structure (ex: Atom[][][] or int[][]…) in the patcher using the save() method? or any alternatives before I start digging in java file IO services…
thank you for any suggestions.

pfg


August 28, 2006 | 7:38 pm

By default the embedMessage/save paradigm expects to be working with
atom lists.
The only hack i can think of off the top of my head is to:

* serialize your complex object to a string stream and store that
string in a single atom.
*add a message ‘deserializedata’ to your object
*in your embedMessage call use deserializedata as the message to be
sent to your object
to decode the data.

Topher

On Aug 28, 2006, at 10:58 AM, pedro guerreiro wrote:

>
> hello all,
> is there a way to embed a complex data structure (ex: Atom[][][] or
> int[][]…) in the patcher using the save() method? or any
> alternatives before I start digging in java file IO services…
> thank you for any suggestions.
>
> pfg
>


August 28, 2006 | 8:34 pm

thank you Topher.
I’ll look into that.

pfg

> * serialize your complex object to a string stream and store that
> string in a single atom.
> *add a message ‘deserializedata’ to your object
> *in your embedMessage call use deserializedata as the message to be
> sent to your object
> to decode the data.
>
> Topher
>


August 29, 2006 | 10:45 am

tried it.
it works if the Atom[] resulting from serialization is shorter than 256 in length (the max limit).
with a bigger Atom[] max crashes when attempting to save the patch.
thanks anyway.


August 29, 2006 | 5:29 pm

you could break them into multiple 256 size chunks. that might be a
limitation of the current file format!
topher

On Aug 29, 2006, at 03:45 AM, pedro guerreiro wrote:

>
> tried it.
> it works if the Atom[] resulting from serialization is shorter than
> 256 in length (the max limit).
> with a bigger Atom[] max crashes when attempting to save the patch.
> thanks anyway.


May 1, 2009 | 6:21 pm

was there ever a solution to this? Example code perhaps? I to am trying to wedge a decent sized object into the embed message function


May 3, 2009 | 3:47 pm

Since 5.0.6, you can save list of 4094 atoms. You still have to serialized multidimensional arrays though.

HTH,


May 4, 2009 | 12:57 am

I got it happening. Bleedin’ awesome! Honestly it solves a TON of my storage issues in max. THANK YOU to who ever figured it out! It’s absolutely amazing. wish it would work for abstractions but I can understand the problem with that. This rocks.


May 4, 2009 | 1:11 am

I hacked this together,

static public Atom[] objectToAtoms(Object saveMe)
serializes java object into atoms for MaxObject.save()

static public Object atomsToObject(Atom[] atoms)
deserializes an atom list into a java object. remember to cast!

static public void setDivisionLength(int size)
sets the length of each atom

Here’s the source

package mat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import markov.doc.v001.DocMarkovAlias;
import markov.doc.v001.MarkovDocument;
import markov.newgen.MarkovHouse;

import com.cycling74.max.Atom;
import com.cycling74.max.MaxObject;

public class ObjAtomConverter extends MaxObject {

	static int divisionLength = 200;

	static public Object atomsToObject(Atom[] atoms)
	{
		String stringFromPatcher ="";
		for(int i = 0 ; i < atoms.length;i++)
		{
			stringFromPatcher= stringFromPatcher+atoms[i].getString();
		}
		return SToO(stringFromPatcher);

	}

	static public Atom[] objectToAtoms(Object saveMe)
	{

		try
		{
			String a = OToS(saveMe);
			return   stringToAtomArray(a, divisionLength);

		}
		catch (Exception a)
		{
			post("exception trying to save " + a.getMessage() + " " + a.toString());
		}

		return null;
	}

	static public Atom[] stringToAtomArray(String b,int divisionLength)
	{
		StringBuffer a = new StringBuffer(b);
		float numOfSubStrings =  (float)a.length()/(float)divisionLength ;
		post(numOfSubStrings+" <-numOfSubStrings");

		Atom[] atomsToPatcher = new Atom[(int)Math.ceil(numOfSubStrings)];
		post(atomsToPatcher.length+" <-atoms in output");

		post(a.length()+" <-a.length");

		int i = 0;
		int end;
		for(  ; i < numOfSubStrings;i++)
		{
			end = Math.min((i+1)*divisionLength,a.length());
			post(end+" <-substring end");
			atomsToPatcher[i] = Atom.newAtom(a.substring(i*divisionLength, end));
			post(i+"   "+atomsToPatcher[i].getString()+"     "+i*divisionLength);
		}

		return atomsToPatcher;
	}
	static private BASE64Encoder	encode	= new BASE64Encoder();
	static private BASE64Decoder	decode	= new BASE64Decoder();

	static public String OToS(Object obj) {
		long start = System.currentTimeMillis();
		String out = null;
		if (obj != null)
		{
			try
			{
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(obj);
				out = encode.encode(baos.toByteArray());
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("Encode:" + (end - start));
		return out;
	}

	static public Object SToO(String str) {
		long start = System.currentTimeMillis();
		Object out = null;
		if (str != null)
		{
			try
			{

				ByteArrayInputStream bios = new ByteArrayInputStream(decode.decodeBuffer(str));
				ObjectInputStream ois = new ObjectInputStream(bios);
				out = ois.readObject();
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
			catch (ClassNotFoundException e)
			{
				e.printStackTrace();
				return null;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("Decode:" + (end - start));
		return out;
	}

	static public void setDivisionLength(int size)
	{
		divisionLength = Math.min(256, size);

	}
}

Viewing 9 posts - 1 through 9 (of 9 total)