Read array of bytes

using what do i read array of bytes that is on a stream...
i send an array of bytes to the invoking stream using void write(byte buffer[])....
how do i read it from the other side....
uzair

Attempts to read up to buffer.length bytes into
buffer, returning the number of bytes that were
sucessfully read. -1 is returned when the end of the
file is encountered.I wasn't asking you to post what the API says it does. We know what it does. Now, do you understand how it reads the bytes into the buffer that you pass to it?

Similar Messages

  • Reading an array of byte with special delimiter....help me!!

    Hi guys,
    i've developed a java application that has in input a txt file with a standard format
    string string string
    string double double
    string double double
    and stores it into an array of byte with a fixed format,
    that is byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)
    that mean introducing byte whitespace value and byte ; value to separate different columns and different rows.
    Now i have to develop inverse routine,that has to read the array of byte and rebuild its original format.
    Can you help me with some idea ord code?
    I'm inexepert...please help me with clear suggest...
    I post you my routine that converts txt file into an array of byte
    P.S.In my txt file number of columns and rows change for each file,what is standard is a firtst headline that is an array of string and others row with the same format string array of doubles....
    public byte[] getBytes(){
              byte middlerow=' ';
              byte endrow=';';
              Vector temp=new Vector(10000000);
              int i=0;     
              String g=null;
              Riga r;
              Double val[];
              while(i<intest.length){
                   //copio tutto nell'array di byte
                   byte []bytes = intest.getBytes();
    //               memorizza in byte un elemento del vettore alla volta
                   for( Byte aByte : bytes ) {
                   temp.addElement( aByte );
                   temp.addElement( Byte.valueOf( middlerow ) );
                   i++;
              temp.addElement(Byte.valueOf(endrow));
              System.out.println("Intestazione convertita in byte");
              for(int l=0;l<rows.size()-1;l++){
                   r=(Riga)rows.get(l);
                   g=r.getgeneid();
              temp.addElement(g.getBytes());
              temp.addElement(Byte.valueOf(middlerow));
              val=r.getvalues();
              for(int e=0;e<=val.length-1;e++){
              temp.addElement(Byte.valueOf(val[e].byteValue()));
              //val[e].byteValue() fa il casting double in byte
              temp.addElement(Byte.valueOf(middlerow));
         temp.addElement(Byte.valueOf(endrow));
              byte [] b=new byte[temp.size()];
              for (int k=0;i<temp.size();i++){
                   b[k]=(((Byte)temp.elementAt(k)).byteValue());
              return b;
    Thanks for your help...

    Ahh, how is the "array of bytes" being populated, Java code?
    If so which encoding are you using? When you create the bytes (from a string at a guess) the other end which encoding do you use?
    If you are using a db, why not use a db? Why this blob of data?
    If I really was banned from using the DB as a DB, and HAD (on pain of death) to use this wonky blob thing, I would do:
    Writing:
    ByteArrayOutputStream daos = new ByteArrayOutputStream() ;
    DataOutputStream dos = new DataOutputStream( daos );
    dos.writeUTF( "me String" );
    dos.writeDouble( 4.2 );
    dos.close;
    byte[] stickInUglyBlob = daos.toByteArray() ;
    Reading:
    DataInputStream dis = new DataInputStream( new ByteInputStream( blobOfUckyness ) );
    String meString = dis.readUTF();
    double meDouble = dis.readDouble();

  • 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.

  • Array of bytes..

    hi..
    how can i convert array of bytes to string and vise versa
    thanks

    You can convert the string to a char array with String.toCharArray(), but if you want them as a byte array you have to first decide what character encoding should be used with the conversion. You can e.g. use "8859_1" if you only work with characters in the range 0 to 255. Then it would be String.getBytes("8859_1"). You have to add a try-catch block. It will throw an exception if you use an unsupported encoding. ISO 8859-1 is one of the basic supported encodings, so you will not get any exceptions when you use this.
    To go the other way, you use one of the String constructors.

  • Array of bytes containg punctuatio​n

    Hi, I have an array of bytes that I would like to export using the spreadsheet VI. The spreadsheet creates a file with the extension .res, which logs the array as hex values. The problem is, all my data is separated with a punctuation mark, which I guess LabView inserts into the array to separate the numbers.
    So my question is, is there any way to convert an array of U8's into a string of pure hex or just a stream of binary data?
    Thanks

    q-bertsuit wrote:
    Thank you for your answers, I'll try your link. The first suggested solution didn't work.
    You are not giving enough information. Did the second suggestion work?
    q-bertsuit wrote:
    I want to use the spreadsheet VI to make a .res file, saving as a plain text file would cause alot of extra work in MatLab.
    A "spreadsheet" in LabVIEW is nothing more than a plain text file with certain delimiters for columns and newlines delimiting rows. "Write to spreadsheeet file" is a simple tool for typical use. All the formatting can be done explicit and then written to a plain text file, the outcome is exactly the same. There is no extra work involved in Matlab.
    q-bertsuit wrote:
    I might not have explained the problem sufficiently in the first post. If my buffer containing U8s is passed in to the spreadsheet VI, the .res file saves for example the number 255 as ASCII character 2 then character 5 then 5. So 255 would be saved as 32 35 35 instead of FE.
    Again, write to spreadsheet file is not the right tool then, so don't use it.
    It is still not clear. So you have an array of U8. How should the file look like? Should there be any delimiters?
    Example: If you only write 255 to the file, should it contain
    one binary, nonpritable character (xFF)
    two charcters (two instances of the letter F)
    Three characters (letter 2 followed by two letters 5)
    If the array has two elements, how should the second character be stored? Should there be a delimiter (space, comma, tab, etc) or nothing separating it from the first value?
    Once we have the specifications, the rest will be trivial.
    LabVIEW Champion . Do more with less code and in less time .

  • Managing bit flags in an array of bytes

    In my program, I have a large array of bytes. Some arbitrarily long groups of bytes in this array act as groups of bit flags. I need to be able to retrieve and manipulate these bit flags. I read that the best way to do this is with bitwise operations; something I have never learned before. I wrote methods that seem to work, but because I have never done anything like this before, can someone check my work?
    Here is an example program, where the only contents of the byte array is a single two-byte grouping of 16 bit flags:
    public class test
        static byte[] bytes = new byte[2];
        static byte[] pow2 = {1, 2, 4, 8, 16, 32, 64, -128};
        static byte[] pow2i = {-2, -3, -5, -9, -17, -33, -65, 127};
        public static void main(String[] args) throws Exception
            writeBitFlag(0, 6, true);
            for (int i = 0; i < 16; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(0, i));
            System.out.println();
            writeBitFlag(0, 12, true);
            invertBitFlag(0, 0);
            invertBitFlag(0, 0); 
            invertBitFlag(0, 1);
            for (int i = 0; i < 16; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(0, i));  
        }//end main
        public static boolean getBitFlag(int startAddress, int flag)
            return (bytes[startAddress + flag / 8] & pow2[flag %= 8]) == pow2[flag];
        }//end getBitFlag
        public static void invertBitFlag(int startAddress, int flag)
            bytes[startAddress + flag / 8] ^= pow2[flag % 8];
        }//end invertBitFlag
        public static void writeBitFlag(int startAddress, int flag, boolean flagVal)
            if (flagVal)
                bytes[startAddress + flag / 8] |= pow2[flag % 8];
            else
                bytes[startAddress + flag / 8] &= pow2i[flag % 8];
        }//end writeBitFlag      
    }//end class testDoes this look like the right way to do what I am trying to do?

    You could try BitSet which provides these function for you.
    public class test { 
        public static void main(String[] args) throws Exception {   
            byte[] bytes = new byte[2];
            writeBitFlag(bytes, 6, true);
            for (int i = 0; i < bytes.length*8; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(bytes, i));
            System.out.println();
            writeBitFlag(bytes, 12, true);
            invertBitFlag(bytes, 0);
            invertBitFlag(bytes, 0); 
            invertBitFlag(bytes, 1);
            for (int i = 0; i < bytes.length*8; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(bytes, i));  
        }//end main
        public static boolean getBitFlag(byte[] bytes, int flag) {
            return ((bytes[flag >> 3] >> flag) & 1) != 0;
        public static void invertBitFlag(byte[] bytes, int flag) {
            bytes[flag >> 3] ^= (1 << (flag & 7));
        public static void writeBitFlag(byte[] bytes, int flag, boolean flagVal) {
            if (flagVal)
                bytes[flag >> 3] |= (1 << (flag & 7));
            else
                bytes[flag >> 3] &= ~(1 << (flag & 7));
    }//end class test

  • How to Convert array of int into array of byte - please help

    I have a 2-dim array of type int and I want to convert it to an array
    of byte. How can I do that? Also, if my 2-dim int array is one dimention
    can I use typecast as in the example below?
    private byte[] getData()
    byte []buff;
    int []data = new int[5];
    //populate data[]
    buff = (byte[]) data; <---- CAN I DO THIS??????
    return buff;

    hi, I suggest you make an array of byte arrays
    --> Byte[][] and use one row for each number
    you can do 2 things,
    take each cipher of one number and put one by one in each column of the row correspondent to that number. of course it may take too much room if the int[] is too big, but that is the easiest way I think
    the other way is dividing your number into bitsets(class BitSet) with sizes of 8 bits and then you can save each bit into each column of your array. and you still have one number in each row. To put your numbers back use the same class.
    Maybe someone has an easier way, I couldnt think of any.

  • Converting an array of bytes to an image

    I think this is the right forum for my question.
    This code comes from the J2ME side of my application. It reads in an int from my server, and if its over a certain length, I can tell its an image so i can decode it from an array of bytes back into an image, but it throws an IllegalArgument Exception on image = Image.createImage(b,0,length);
    I've decoded the byte array before it gets sent and convereted it back into an image and that works, and ive run the debugger and the array is full of numbers on the J2ME side of the app, but the call to createImage, just keeps throwing this exception and i can't find an answer. If you need anymore code, I'll post it.
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
    byte[] b = null;
    int length = in.readInt();
    System.out.println("length = " + length);
    if(length < 10) {
    imageArray[i] = null;
    System.out.println("null image");
    } else {
    b = new byte[length];
    in.readFully(b,0,length);
    System.out.println("image not null");
    Image image = Image.createImage(b,0,length);
    System.out.println("hit");
    ImageItem imageItem = new ImageItem("null", image, 0, "null");
    imageArray[i] = imageItem;
    If anyone can tell me how to indent the code, i would appreciate it, it looks indented when i post it my side.
    Message was edited by:
    trisonetwo
    Message was edited by:
    trisonetwo

    If it works before sending then check your code for sending the image.
    Also you can compare the byte array before sending with the array which was received at the other end.
    For indent and syntex highliting use [ c o d e ] and [ / c o d e ] tags (without spaces)
    Ex:-
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
       byte[] b = null;
       int length = in.readInt();
       System.out.println("length = " + length);
       if(length < 10) {
          imageArray = null;
          System.out.println("null image");
       } else {
          b = new byte[length];
          in.readFully(b,0,length);
          System.out.println("image not null");
          Image image = Image.createImage(b,0,length);
          System.out.println("hit");
          ImageItem imageItem = new ImageItem("null", image, 0, "null");
         imageArray = imageItem;
    }

  • Converting a PNG image to an array of bytes and vice versa..

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

  • Getting file into an array of byte..

    Hi guys,
    i first thank you for all the help you've given in my precedent posts.
    Now i've to ask you a question.
    I need to convert some lines of a txt file(obtained from an excel table) into an array of byte in the best possible manner.
    My file can have a various number of rows and columns.
    With this code
    BufferedReader br = new BufferedReader(new InputStreamReader(myFile.getInputStream()));
    String line = null;
            while ((line = br.readLine()) != null) {
                    line = line.replace (',', '.');
                    StringTokenizer st = new StringTokenizer(line);
                    numberOfNumericColumns = (st.countTokens()-1);
                    col=(numberOfNumericColumns+1);I read the file, i change , with . and line to line i calculate the number of value(that have to be the same,i do the control for showing exception if necessary).
    Now i know from my file what are the number of values in a row(columns of my old excel table).
    I have to convert this file into an array of byte.
    How can i do it?
    Suppose we have only 3 lines, the first line is ever only string,the others lines have a string and other double values.....
    nome giuseppe carmine paolo
    valerio 23.3 34.3 43.3
    paolo 21.2 34.5 12.2
    each value is separated from another one by one or more whitespaces..
    The most important thing is that in a second moment i have to read this array of byte and showing its original content,so i have to put in the array some different value and different line symbol delimiter.
    Can you help me giving me some idea or code?(please be clear,i'm inexpert of java...)
    Thanks very much,i need your help
    Message was edited by:
    giubat

    thanks for your reply...
    my file has about 50000 rows..........and i have to develop an application that allows the upload of about 6000/7000 files....
    so i have to optimize everythings......i want to use whitespace to separate different value and ;different line
    so in my array of bytes i want
    nome(converted in byte) whitespace(in byte) giuseppe (in byte) whitespace(in byte)carmine(in byte) whitespace(in byte) paolo (in byte) ;(in byte);
    valerio(in byte) whitespace(in byte) 23.3 (in byte) whitespace(in byte) 34.3 (in byte) whitespace(in byte)43.3 (in byte) ;(in byte) etc.....So i should have an array of byte lower than the array of byte obtained converting each line as a string into bytes.......
    do you understand what's my problem?
    How can i create an array of byte without fixing its initial dimension that is able to grows up for each line read?
    Excuse for my poor english..
    In the past i've used a vector to temporary store the data while reading the file and at the end i copied the vector in the array of byte but when i do upload in my application i had java heap size error.
    So someone has suggested me to improve my storing method and i've decided to eliminate vector....
    can you help me......????
    please....i need your help..

  • Reading file in bytes using FileReader...

    Hi,
    I am trying to read out bytes value from a .bmp file then i try to change the byte values that i read out to hexadecimal value.
    I open the .bmp in Win Hex (a software) but the hexadecimal value does not tally....
    public void readByteFile (String path)
    Vector in = new Vector ();
    try
    FileReader reader = new FileReader (path);
    int c;
    while ((c = reader.read ()) != -1)
    String hex = Integer.toHexString (c);
    reader.close ();
    } catch (FileNotFoundException e) {System.err.println(e);}
    catch (IOException e) {System.err.println(e);}
              return in;
         }

    If you want to read the actual bytes from the file then do not use a Reader. That will convert the bytes to characters as if they were text. Use an InputStream instead.

  • How to send array of bytes to a servlet and store it in a file

    Hi,
    How to send array of bytes to a servlet and store it in a file.
    I am new to Servlets. if possible for any one, please provide the code.
    Thanks,
    cmbl

    Through HTTP this is only possible with a POST request of which the encoding type is set to multipart/form-data. You can use Apache Commons FileUpload to parse such a multipart form data request into useable elements. From the other side (the client) you can use a HTML <input type="file"> element or a Java class with Apache Commons PostMethod API.
    You may find this article useful: http://balusc.blogspot.com/2007/11/multipartfilter.html

  • Wonderware and OPC; reading arrays ?

    I have a contact on a project that says Wonderware is not able to read
    OPC data (Shared Variables) from LabVIEW in the form of arrays of double, all data must be single values.
    Can anybody confirm this statement, or tell me otherwise ? I find it strange that
    this big software platform in the year of 2015 cannot read arrays of OPC data.
    Martin

    Hi Martin,
    and in which way can LabVIEW help when "Wonderware is not able to read OPC data (Shared Variables) from LabVIEW in the form of arrays of double" as you wrote?
    That sounds like a problem of Wonderware - and you should ask the support for Wonderware…
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • How to convert an array of bits into an array of bytes?

    If I have an array of 1s and 0s, how can I pack them
    into an array of bytes (8 bits)?
    Thanks.

    NI recommends to get rid of 4.x mode (Quote from the help file: "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution."). For this reason, I have attached an alternative example to give you some ideas.
    (The previous example (using 4.x type cast) is actually slightly flawed, because for some reason it only produces even sized output arrays (why???) so you would need to trim the resulting array as a function of the input array size.)
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    OnesAndZeroesU8v2.vi ‏43 KB

  • IOException reading reader invalid first byte 11111000

    Hi,
    I have just switched from Vista to Ubuntu 8.04, and when I try to emulate a j2me application I get an exception.
    The first time I start the application (with no ~/j2mewtk/ folder) the following exception is casted:
    Running with storage root ~/j2mewtk/2.5.2/appdb/DefaultColorPhone
    Running with locale: LC_CTYPE=nb_NO.UTF-8;LC_NUMERIC=nb_NO.UTF-8;LC_TIME=nb_NO.UTF-8;LC_COLLATE=nb_NO.UTF-8;LC_MONETARY=nb_NO.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=nb_NO.UTF-8;LC_NAME=nb_NO.UTF-8;LC_ADDRESS=nb_NO.UTF-8;LC_TELEPHONE=nb_NO.UTF-8;LC_MEASUREMENT=nb_NO.UTF-
    8;LC_IDENTIFICATION=nb_NO.UTF-8
    Running in the manufacturer security domain
    Uncaught exception java/lang/RuntimeException: IOException reading reader invalid first byte 11111000.
    Execution completed.
    22885068 bytecodes executed
    3358 thread switches
    1791 classes in the system (including system classes)
    37695 dynamic objects allocated (2415020 bytes)
    144 garbage collections (2133624 bytes collected)If I try to run the application again (with a ~/j2mewtk/ folder), this exception is cast:
    Running with storage root ~/j2mewtk/2.5.2/appdb/DefaultColorPhone
    Running with locale: LC_CTYPE=nb_NO.UTF-8;LC_NUMERIC=nb_NO.UTF-8;LC_TIME=nb_NO.UTF-8;LC_COLLATE=nb_NO.UTF-8;LC_MONETARY=nb_NO.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=nb_NO.UTF-8;LC_NAME=nb_NO.UTF-8;LC_ADDRESS=nb_NO.UTF-8;LC_TELEPHONE=nb_NO.UTF-8;LC_MEASUREMENT=nb_NO.UTF-
    8;LC_IDENTIFICATION=nb_NO.UTF-8
    Running in the manufacturer security domain
    java.lang.RuntimeException: IOException reading reader invalid byte 11100110
         at com.sun.cldc.i18n.Helper.byteToCharArray(+228)
         at com.sun.cldc.i18n.Helper.byteToCharArray(+9)
         at java.lang.String.<init>(+7)
         at java.lang.Class.runCustomCode(+0)
         at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
         at com.sun.midp.midlet.Scheduler.schedule(+52)
         at com.sun.midp.main.Main.runLocalClass(+28)
         at com.sun.midp.main.Main.main(+80)
    Execution completed.
    3739336 bytecodes executed
    51 thread switches
    1774 classes in the system (including system classes)
    27329 dynamic objects allocated (1267964 bytes)
    91 garbage collections (1149916 bytes collected)I use Sun Java Wireless Toolkit 2.5.2, MyEclipse 6.0.1 and jdk1.6.0_06. The source files are originally created in XP and Vista.
    Any tips?

    I found the error. Some of the source files or configuration files were Cp1252 encoded. When setting the jvm to use Cp1252 encoding both on server and client side, everything worked just fine.

Maybe you are looking for