How can i convert object to byte array very*100 fast?

i need to transfer a object by datagram packet in embeded system.
i make a code fallowing sequence.
1) convert object to byte array ( i append object attribute to byte[] sequencailly )
2) send the byte array by datagram packet ( by JNI )
but, it's not satisfied my requirement.
it must be finished in 1ms.
but, converting is spending 2ms.
network speed is not bottleneck. ( transfer time is 0.3ms and packet size is 4096 bytes )
Using ObjectOutputStream is very slow, so i'm using this way.
is there antoher way? or how can i improve?
Edited by: JongpilKim on May 17, 2009 10:48 PM
Edited by: JongpilKim on May 17, 2009 10:51 PM
Edited by: JongpilKim on May 17, 2009 10:53 PM

thanks a lot for your reply.
now, i use udp socket for communication, but, i must use hardware pci communication later.
so, i wrap the communication logic to use jni.
for convert a object to byte array,
i used ObjectInputStream before, but it was so slow.
so, i change the implementation to use byte array directly, like ByteBuffer.
ex)
public class ByteArrayHelper {
private byte[] buf = new byte[1024];
int idx = 0;
public void putInt(int val){
buf[idx++] = (byte)(val & 0xff);
buf[idx++] = (byte)((val>>8) & 0xff);
buf[idx++] = (byte)((val>>16) & 0xff);
buf[idx++] = (byte)((val>>24) & 0xff);
public void putDouble(double val){ .... }
public void putFloat(float val){ ... }
public byte[] toByteArray(){ return this.buf; }
public class PacketData {
priavte int a;
private int b;
public byte[] getByteArray(){
ByteArrayHelper helper = new ByteArrayHelper();
helper.putInt(a);
helper.putInt(b);
return helper.toByteArray();
but, it's not enough.
is there another way to send a object data?
in java language, i can't access memory directly.
in c language, if i use struct, i can send struct data to copy memory by socket and it's very fast.
Edited by: JongpilKim on May 18, 2009 5:26 PM

Similar Messages

  • How can I convert  AudioInputStream to  byte array ?

    Hi
    I want to convert audioinputStream to bytearry?
    i try like this
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
         int c = 0;
         while (true) {
         try {
                        c = audioInputStream.read();
                   } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
         if (c == -1)
         break;
         baos.write(c);
    but...
    java.io.IOException: cannot read a single byte if frame size != 1
    occur...
    plz help me..

    Hi,
    I haven't tried, but from the API doc I would suggest:
    Retrieve the frame size by
    int fs = audioInputStream.getFormat().getFrameSize();Allocate a byte array with at least this size and read full frames:
    byte[] buf = new byte[fs];
    int sz = audioInputStream.read(buf);Write also to your baos a full frame:
    baos.write(buf, 0, sz);

  • How can I convert stream to byte and byte to stream

    I got a IInputStream from BackgroundDownloader operation, how can I convert it to byte array? I try to convert it to buffer and after that to bytes, but it's always throw exception.
    function decryptStream(inputStream, totalBytes) {
    var reader = new Windows.Storage.Streams.DataReader(inputStream);
    var iBuffer = reader.readBuffer(totalBytes);
    var bytes = Windows.Security.Cryptography.CryptographicBuffer.copyToByteArray(iBuffer);
    Could I have examples about that?

    Hi Jeft,
    In my program, I have a BackgroundDownloader to download an image. When we have result, I will convert it to bytes array.
    if (p.hasResponseChanged && imageStream == null) {
    imageStream = operation.getResultStreamAt(0);
    if (!p.hasResponseChanged && imageStream != null) {
    decryptStream(imageStream, p['bytesReceived']);

  • How can I convert an array off byte into an Object ?

    Hi folks...
    I�m developing an application that comunicates a PDA and a computer via Wi-Fi. I�m using a DataStream ( Input and Output ) to receive / send information from / to the computer. Most off the data received from him is in the byte[] type...
    How can I convert an array off byte ( byte[] ) into an Object using MIDP 2.0 / CLDC 1.1 ?
    I found on the web 2 functions that made this... but it uses a ObjectOutputStream and ObjectInputStream classes that is not provided by the J2ME plataform...
    How can I do this ?
    Waiting answers
    Rodrigo Kerkhoff

    There are no ObjectOutputStream and ObjectInputStream classes in CLDC. You must know what you are writing to and reading from the DataStream. You should write the primitives like int, String to the DataOutputstream at one end and read those in exactly the same sequence at the outher end using readInt(), readUTF() methods.

  • How to convert a Image object to byte Array?

    Who can tell me, how to convert Image object to byte Array?
    Example:
    public byte[] convertImage(Image img) {
    byte[] b = new byte...........
    return b;

    Hello,
    any way would suit me, I just need a way to convert Image or BufferedImage to a byte[], so that I can save them. Actually I need to save both jpg and gif files without using any 3rd party classes. Please help me out.
    thanx and best wishes

  • How can i convert an object to stream of chars or bytes?

    how can i convert an object to stream of chars or bytes?

    One way is the serialization mechanism. There are examples and explanations of it in the Java tutorial: http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

  • How can I convert an Hexadecimal number in bytes

    Hello!
    I have a string wich represents an hexadecimal value ( ex: C2105215767E)
    I want to convert it into byte. I want to obtain an array of byte. I want to write it into a variable : byte[]var
    How can I do it?
    Thanks you for your answer.
    cordialy,
    lafon1

    Look into the API for BigInteger. You can use a constructor that takes a String and specify the radix as 16 (hex). Big Integer includes a method to convert to a byte array.

  • How can I convert output data (string?) from GPIB-read to an 1D array?

    Hello all,
    I am reading a displayed waveform from my Tektronix Oscilloscope (TDS3032) via the GPIB Read VI. The format of the waveform data is: positive integer data-point representation with the most significant byte transferred first (2 bytes per data point).
    The output data of GPIB-Read looks like a string(?) where the integer numbers and a sign like the euro-currency sign are seperated by spaces e.g. #5200004C3 4 4 4 4 3C3C3........ (C represents the euro-currency sign).
    How can I convert this waveform data into a 1D/2D array of real double floatingpoint numbers (DBL) so I can handle the waveform data for data-analysis?
    It would be very nice if someone know the solution for this.
    t
    hanks

    Hi,
    First of all, I'm assuming you are using LabVIEW.
    The first you need to do is parse the string returned by the instrument. In this case you need to search for the known symbols in the string (like the euro sign) and chop the string to get the numeric strings. Here are some examples on parsing from www.ni.com:
    Keyword Search: parsing
    Once you parse the numeric strings you can use the "String/number conversion VIs" in the String pallette.
    Hope this helps.
    DiegoF.Message Edited by Molly K on 02-18-2005 11:01 PM

  • How can I convert a layer into a smart object with Adobe Photoshop Elements 10?

    How can I convert a layer into a smart object with Adobe Photoshop Elements 10?

    That feature is only in PS. Elements as you buy it has only limited support for smart objects. If you use File>Place to add something to your image it will come in as a smart object (within the limits to which PSE understands smart layers). However, if you know someone who can write an action for you in PS you can install it in PSE, or you could look into something like elements+ from simplephotoshop.com.

  • How can I convert  an ArrayList to a String[]

    Hi,
    How can I convert an ArrayList (only with strings) to a String[] ?
    I've tried this :
         public static String listToString(List l) {
              StringBuffer sb = new StringBuffer();
              Iterator iter = l.iterator();
              while (iter.hasNext()) {
                   sb.append(iter.next().toString());
                   if (iter.hasNext()) {
                        sb.append(',');
              return sb.toString();
    But what I get is an array of xxxxx@435634 (for example).
    Thanks a lot !

    Strings are Objects but not all Objects are Strings and at least one of the elements in your List is not a String.

  • How can I convert string to the record store with multiple records in it?

    Hi Everyone,
    How can I convert the string to the record store, with multiple records? I mean I have a string like as below:
    "SecA: [Y,Y,Y,N][good,good,bad,bad] SecB: [Y,Y,N][good,good,cant say] SecC: [Y,N][true,false]"
    now I want to create a record store with three records in it for each i.e. SecA, SecB, SecC. So that I can retrieve them easily using enumerateRecord.
    Please guide me and give me some links or suggestions to achieve the above thing.
    Best Regards

    Hi,
    I'd not use multiple records for this case. Managing more records needs more and redundant effort.
    Just use single record. Create ByteArrayOutputStream->DataOutputStream and put multiple strings via writeUTF() (plus any other data like number of records at the beginning...), then save the byte array to the record...
    It's easier, it's faster (runtime), it's better manageable...
    Rada

  • How can I convert a pages color document to a PDF Black and White doc.

    How can I convert a pages color document to a PDF Black and White doc.  Or, covert the color to B&W in a new doc?

    > How can I convert a pages color document to a PDF Black and White doc.  Or, covert the color to B&W in a new doc?
    The general idea is that you colour correct photographs once, archive, and convert with or without colour changes. The archived photograph is unchanged - or we would be colour correcting the same photographs again and again and again.
    If you have a photograph with a corrected exposure, you can open the photograph in the Apple ColorSync Utility, apply a colour space conversion to a grayscale appearance using the preinstalled ICC profile, save the photograph under another name, and place that in your pagination.
    If you have a paginated document with corrected exposures, and any such non-scalable bitmap or scalable spline graphics as you have chosen to add, you can render the pagination as a whole to PDF through the same ICC profile, carrying out the same colour space conversion on any and all objects.
    Caveat: If you intend the pagination for certain processes, in particular offset lithography, then you are probably  expected not to render the type to grayscale, but rather to render it to single ink solid black. No software can determine what printing process you intend, you have to understand a bit about printing, and how to set up general colour space conversions in software. Ask your prepress provider, and if the answer is not prompt and proficient, pick another provider.
    /hh

  • How do I convert a 1-D array of cluster of 5 elements into a 2-D array of numbers? (history data from a chart)

    Hello,
    in my vi I have a chart with 5 Plots displaying measurement data.
    The user should be able to save all the history data from the chart at anytime. (e.g. the user watches the chart and some event happens, then he presses a "save"-button)
    I know, that I can read out the history data with a property node. That is not the problem. The problem is, how do I handle the data? The type of the history data is a 1-D array of cluster of 5 elements.
    I have to convert that data somehow into a 2 D-array of numbers or strings, so that I can easily save it in a text-file.
    How do I convert a 1-D array of cluster of 5 elements into a 2-D array of numbers?
    I use LabVIEW 7.1
    Johannes
    Greetings Johannes
    Using LabVIEW 7.1 and 2009 recently
    Solved!
    Go to Solution.

    Gerd,
    thank you for the quick response and the easy solution.
    Look what I did in the meantime. I solved the problem too, but muuuch more complicate :-)
    And I have converted the numbers to strings, so that I can easily write them into a spreasheet file.
    Johannes
    Message Edited by johanneshoer on 04-28-2009 10:39 AM
    Greetings Johannes
    Using LabVIEW 7.1 and 2009 recently
    Attachments:
    SaveChartHistory.JPG ‏57 KB
    SaveChartHistory.JPG ‏57 KB

  • How can I convert the variable expression stored as string back to variable expression

    How can I convert the variable expression stored as string back to variable expression?
    I am storing the expression enterd in the TSExpresssionEditControl as simple string and want to convert back to expression since I want to get the data type of that expression.

    pritam,
    I'm not sure what you're trying to do exactly. If you are trying to get the value of a variable and you only have the name of value in a string, then you can use Evaluate() to get its value. If you want the data type, my advise is to use the GetPropertyObject() API method and just pass in the loop up string. Then you'll have a handle to the data object and then proceed from there.
    Regards,
    Song D
    Application Engineer
    National Instrument
    Regards,
    Song Du
    Systems Software
    National Instruments R&D

  • Converting object wrapper type array into equivalent primary type array

    Hi All!
    My question is how to convert object wrapper type array into equivalent prime type array, e.g. Integer[] -> int[] or Float[] -> float[] etc.
    Is sound like a trivial task however the problem is that I do not know the type I work with. To understand what I mean, please read the following code -
    //Method signature
    Object createArray( Class clazz, String value ) throws Exception;
    //and usage should be as follows:
    Object arr = createArray( Integer.class, "2%%3%%4" );
    //"arr" will be passed as a parameter of a method again via reflection
    public void compute( Object... args ) {
        a = (int[])args[0];
    //or
    Object arr = createArray( Double.class, "2%%3%%4" );
    public void compute( Object... args ) {
        b = (double[])args[0];
    //and the method implementation -
    Object createArray( Class clazz, String value ) throws Exception {
         String[] split = value.split( "%%" );
         //create array, e.g. Integer[] or Double[] etc.
         Object[] o = (Object[])Array.newInstance( clazz, split.length );
         //fill the array with parsed values, on parse error exception will be thrown
         for (int i = 0; i < split.length; i++) {
              Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
              o[i] = meth.invoke( null, new Object[]{ split[i] });
         //here convert Object[] to Object of type int[] or double[] etc...
         /* and return that object*/
         //NB!!! I want to avoid the following code:
         if( o instanceof Integer[] ) {
              int[] ar = new int[o.length];
              for (int i = 0; i < o.length; i++) {
                   ar[i] = (Integer)o;
              return ar;
         } else if( o instanceof Double[] ) {
         //...repeat "else if" for all primary types... :(
         return null;
    Unfortunately I was unable to find any useful method in Java API (I work with 1.5).
    Did I make myself clear? :)
    Thanks in advance,
    Pavel Grigorenko

    I think I've found the answer myself ;-)
    Never thought I could use something like int.class or double.class,
    so the next statement holds int[] q = (int[])Array.newInstance( int.class, 2 );
    and the easy solution is the following -
    Object primeArray = Array.newInstance( token.getPrimeClass(), split.length );
    for (int j = 0; j < split.length; j++) {
         Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
         Object val = meth.invoke( null, new Object[]{ split[j] });
         Array.set( primeArray, j, val );
    }where "token.getPrimeClass()" return appropriate Class, i.e. int.class, float.class etc.

Maybe you are looking for

  • SAPJCA / SAPJRA timeout

    Hello, I need to extract mass data out of SAP and I use the SAPJRA & JCO to do that. But after some time I get a "timeout" exception. Is there a possibility to set the timeout for the SAPJRA? thanks a lot, Juraj

  • Portal Error 404 (The requested resource is not available)

    Dear, I have faced the below error in our Portal (6.0), I have created a new role that its linked normally with worksets, pages and i-view, all the way to custom SAP R/3 transactions, most of the users are able to see and execute these transactions,

  • Confirmation is not happening

    Hello Guru's, The problem is like this. 1) i have created the production order through co01. 2)done the goods issue through mb1a. 3)While confirmation it is showing a error that Goods movement not possible with mvmt type 261. Pls sort it out. vish

  • IWeb Starts to Publish, and never finishes

    Hey y'all- Here is the deal. I am running iWeb 09 and have had no problems... until tonight. I publish to my MobileMe regularly through the three sites that I run from my MobileMe account. I made some changes to several pages on each of the sites and

  • .FindText() infinite loop problem

    My company has a need to count how many times certain words are in a PDF document while doing automated testing.  We're using QuickTest Professional and it's programming language is VBScript.  Now I attempted to use the example code from the SDK manu