Problem rendering certain bytes when reading binary file

I have a two part problem. I am trying to read files of any type from a client and transfer them over a pipe to a UNIX host running a C API. I have this process working for text data just fine however when I try and submit binary data there appears to be some data loss. Most of the file appears to end up on the host but it just isn't as long as the source, nor will it render correctly.
In investigating this problem I tried to simply output a temporary copy of the transferred file on the client as I was reading the file just to see if my "thinking/process" was correct. The copy of the file ends up the same length but some bytes seem to have been misread. Upon doing a windiff on the source and copy it appears that several characters that are rendered as blocks in the original show up as '?' in the destination file.
I believe this is an entirely different problem than why I am losing data on the host side but I want to first figure out why this problem is occuring. The below code is how I am reading and writing the binary file. I realize it has some problems with it, it is more of a POC at this point.
           final int BUF_SIZE = 1000;  // 1K
           char[] cBuffer = new char[BUF_SIZE];
           byte[] bBuffer = new byte[BUF_SIZE];
           int read = BUF_SIZE;
           long length = fLocalFile.length();
           FileInputStream fis = new FileInputStream(fLocalFile);
           DataInputStream dis = new DataInputStream(fis);
           FileOutputStream fos = new FileOutputStream("C:\\temp.file", false);
           DataOutputStream dos = new DataOutputStream(fos);
           for (int start = 0; start < length && reply.getSuccess(); start += read)
               System.out.println("length: " + length + " start: " + start);
               read = dis.read(bBuffer, 0, BUF_SIZE);
               // Send the file data
               String sTemp = sDestName + ":" + new String(bBuffer,0,read);
               dos.write(bBuffer,0,read);
               reply = axBridge.execute (Commands.CMD_FILE_TRANSFER_SEND, sTemp);
            dos.close();
        }It seems as if when reading or writing on the data streams some of the characters aren't getting converted correctly. Can anyone help? I've been testing with a PDF if that sheds any light.

Yes but you ARE converting to a String first which you then send to the axBridge (sTemp!). Try just sending the bytes. You can easily pre-pend the "<filename>:" by sending those first.
I know that some conversions occur when converting to a String, what they are exactly and what the exact effects are escapes me. Past experience though has taught me to ALWAYS send bytes, with no conversions, what you read is what you send.
You may need to modify the send/receive protocol so that you send the command first with the filename then the bytes are sent after...
As for why the file is not being written correctly to: c:\\temp.file, don't know... try the following code, it tends to be one of the "standard" ways of "streaming" data...
     byte buf[] = new byte[bufSize];
     int bRead = -1;
     while ((bRead = in.read (buf)) != -1)
         out.write (buf,
                 0,
                 bRead);
     }     And try just using a FileOutputStream or wrapping in a BufferedOutputStream.

Similar Messages

  • Problems when reading pdf files

    I use windows 7, and also have available Adobe Reader 5 and Adobe Reader X, but i receive some problems when reading pdf files. What can I do???

    Reader 5 is not Windows Compatible. Two different versions of the same computer can cause all sorts of problems. Remove all Acrobat and/or Reader software then re-install Reader X.

  • Read binary file 10 bytes at a time

    I want to read binary file in a specific format. There is a number at the starting of the file. That tells us where the data is starting. Suppose it says 3975, then it mean data starts at 3975. Then once I read that location I want to read data at 10bytes at a time and then convert it to numeric and display it on graph. I am attaching the file format and the current vi which I am using. please help.
    Solved!
    Go to Solution.
    Attachments:
    topo.zip ‏89 KB

    Ah, the first 10 characters are a string!
    After opening the file, use the Read From Text File with count set to 10.  Then use the Decimal String to Number to convert this string into your "record size".
    NOTE:  The text indicator was just for debug purposes.  You don't really need it.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    Read record size.png ‏12 KB

  • Read binary file in LabVIEW 2012

    Hi,
    Have anyoune experienced problems reading binary files in 2012 previously saved in 2010.
    As shown on attached image, the same VI is opened in 2010 and 2012 reading the same binary file containing the data structure "ProcessList IN".
    I suspect it has something to do with the variants ?
    The error message does help a lot (-;
    Any ideas ?
    Kind regards
    Kahr
    Regards Kahr
    Certified LabVIEW Architect
    CIM A/S
    Attachments:
    Read_Binary_File_Error_LV2012.png ‏426 KB

    Hi Kahr,
    "error 1" tells one of the input parameters is wrong: it's either the path or the cluster datatype! The error message only mentions a file path AS AN EXAMPLE!
    You're using BinaryRead similar to those old Datalog file functions to read a cluster of several data. It has been know that datalog files get tricky when one is using a different LabVIEW version to read them due to changes of internal data representation. Maybe that's the point that hits you now?
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • How to open and read binary files?

    How do I open and read Binary files?

    Did you  look on The Unarchiver's web site where it has a link to older versions? http://theunarchiver.googlecode.com/files/TheUnarchiver3.2_legacy.zip
    The best thing to do is ask your friends what programs they used to produce these files, or at least what format files they are producing.  Otherwise it's like being shown a car and given a bundle of 200 keys with no idea to which one to use, or even if any of them work with that car.
    Using The Unarchiver will likely not do anything because it too will not know what format files are involved, and they may not even been in an archived format.  If they sent you a Word file without telling you (a favorite of Windows users to do  -- it drives me crazy when they could have just sent them in plain text), The Unarchiver won't open them.  If it's a picture file then using Hexedit will just show you a bunch of unintelligible stuff as shown in an earlier post, though you may see a line of text providing a hint.
    As I said earlier, often .bin may be an executable program which needs another program to actually interpret it.  That's what Java is trying to do.  Still, it may think it can execute the file, but it is highly unlikely somebody would send you an executable program (and if they did I would not trust it).  For all you know it may be a Windows virus.

  • Before, when reading PDF files, I was able to copy and paste part of the book. Now it is not possible. I can copy but when pasting gets nothing.  Antes, quando lia arquivos PDF, eu conseguia copiar e colar parte do livro. Agora isso não é possível.

    Before, when reading PDF files, I was able to copy and paste part of the book. Now it is not possible. I can copy but when pasting gets nothing.
    Antes, quando lia arquivos PDF, eu conseguia copiar e colar parte do livro. Agora isso não é possível. Eu consigo copiar, mas quando vou colar o texto, não cola nada.

    Caro Fabiano,
    Obrigado por ter contactado as Comunidades de Suporte Apple.
    Na origem do seu problema podem estar várias causas. Nomeadamente:
    O ficheiro (arquivo) que está a ler pode não ser texto, ou seja, pode ser texto transformado em imagem e exportado como PDF.
    A aplicação de destino (onde está a tentar colar o texto copiado) pode não aceitar a colagem de texto formatado.
    A aplicação que está a utilizar para abrir os ficheiros PDF pode não ser indicada para certo tipo de documentos. Experimente o iBooks, o Dropbox ou até mesmo o seu e-mail.
    No caso de nenhuma destas causas estar na origem do seu problema, forneça, por favor, mais informações sobre a origem do ficheiro (descarga pelo Safari, Mail ou outro) bem como o destino (documento Pages, Keynote ou Numbers ou outra aplicação que não o iWork).
    Atentamente,
    Gonçalo Matos
    Estou aqui para ajudar. Clique no botão "Reply" no caso de ter outra questão ou necessitar de outro esclarecimento para que eu possa ajudar da melhor forma que conseguir.

  • Reading binary files

    Hi
    I need some help in reading binary files. This is the code I have so far.
    import java.io.InputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    public class patternmatch {
         public static void main (String[] args) {   
              final byte[] bytes = new byte[1];
              try {     
                   final InputStream  instream = new FileInputStream  (args[0]);
                   while (true) {
                        final int count = instream.read (bytes);
                        if (count==-1) break;
                        System.out.println(bytes);
                   instream.close();       
              } catch (IOException e) {
                   System.err.println(e);         
    }Instead of a list of hexadecimal numbers that I want I get a long line of
    [B@eee36c
    [B@eee36c
    [B@eee36c
    [B@eee36c
    Which represents some address I think.
    Does anyone know how to extract the hexidecimal numbers?
    Thanks
    Charles

    double post http://forum.java.sun.com/thread.jsp?forum=31&thread=558802

  • Labview 8.2 Read Binary File, no pos offset option anymore?!

    I have a VI that worked perfectly well in 7.1 and now I've opened it in 8.2 it doesn't work and I can't revert back to 7.1 because I accidently pressed save.
    In 7.1 I used the combination of open, read and close file functions to read parts of a large binary data file - I cannot read the whole file into Labview 8.2 as it fills the memory. There isn't a terminal in the 'read binary file' function in 8.2 for 'pos offset' which is what I used to use in 7.1. How am I supposed to open the file now? Surely they have not removed such a useful facility entirely - is there another way to do it that I am not aware of?
    Many thanks in advance for help,
    Sarah

    smercurio_fc wrote:
    André, at first I could not understand your response, ...
    Must be my Dutch way of writing English
    Regards,
    André
    Using whatever version of LV the customer requires. (LV5.1-LV2012) (www.carya.nl)

  • How to read binary files wrt specific BYTE size and length??

    Hello Everyone,
                                I have a project I want to accomplish. I have a binary file, and I would like to read the data and print on wfm in a specific order and size.
    The data is 16 bit binary type , and needs to be read in chunks of 2 bytes.
    i have 30 bytes of sample 1.
    followed by 2 bytes of sample 2.
    followed by another 2 bytes of sample 3.
    steps 2-4 should be repeated 10 times and then i should read sample 4 which is of 2 bytes.....
    How should I do it?? I don't have any VI build... all i have is the example VI...
    can anyone pleasehelp me???
    Now on LabVIEW 10.0 on Win7

    smercurio_fc, sorry for the confusion, i will try my best to explain...
    1. No, i don;t have to read the file again, once it has read, I used
    while loop just to see the data updating (i press run, and before i can
    visualize i have the waveforms; i can get rid of the while loop)
    2. I have 30 different values of 1 sample. actually, the data is cmg
    from tri-axial accelerometer; each axis is of 10 bytes(hence 3*10 =
    30bytes)
    3. I am repeating the steps 2-4 10 times because the data was written
    into the binary file after 10 times sampling the sensors(if first 3
    samples are read @ 1000hz, sample 4 was read at 1000/10 = 100hz)
    4. I am using the graphs to interpret the values, that's it. The
    values are already scaled when they were wrote to the binary file, I
    have to simply interpret it.
    I have made some changes in the VI, now i am reading only the first
    30bytes, that too, in chunks of 10-10-10 bytes, and plotting the 3
    samples simultaneously on a waveform chart. (will approach 1
    sensor/sample at a time) and running the loop for 10 times. I have changed I8 to I16 now.
    Please let me know if it makes sense to you now.
    P.S. each sample is a sensor data.
    Now on LabVIEW 10.0 on Win7
    Attachments:
    data_read.vi ‏24 KB

  • Read binary files that are wraped in the downloaded executable signed jar

    Hello, there:
    I have created a Swing application and created a signed jar file and uploaded it to my site. The signed jar includes class packages, and a folder of binary files which are the datasource for my application.
    jws downloads this signed executable jar, it'll automatically run it, but it has problems reading the binary folders wrapped in itself (the app is supposed to read the folder's structure and use the info to create a JTree object, and read the file's content as well). Is it the file path conversion problem? Do we need to use URL instead? I tried it after reading some threads on this forum but didn't make it.
    As an alternatives, I want JWS to unjar the jar file and expand it to exploded files. I manually unjar it and run the app from command line, it works fine.
    Plus, the app is supposed to manipulate the binary files when it's in process, like saving new content back to the files, zip the files and upload them to the remote sql server. therefore, I think it's easy to have it run when it's expanded.
    So here is the question: JWS by default is running the executable jar, is there a way to tell JWS to unzip the jar and find the main class in the exploded files and run it?
    Thanks a lot for your suggestions,
    Sway

    You can get to any resource in a jar file in your classpath. The code below will return InputStream for resource.bin nested two packages down.
    InputStream in = YourClass.class.getResourceAsStream("/com/mypackage/resouce.bin");  //use '/' instead of '.'You can open a FileOutputStream to write that file.
    OutputStream out = new FileOutputStream("myTempResource.bin");
    IOUtil.streamAndClose(in,out);If the resouce is a nested zip or nested jar then you can use the Java Zip utilities to unwrap the stream.

  • How to read binary file ?

    Hi All,
    I want to read a binary file which has double data. The first byte is an unsigned byte. I have tried using DataInputStream readUnsignedByte method. But then how to convert this value into double.
    int hdop = _data.readUnsignedByte();

    Hi,
    I have managed to read the first byte i.e hdop. I am getting the values of first 2 bytes properly. But the values where I have to read more than 1 byte is creating problem. Can anyone let me know whats wrong in the code?
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.util.Vector;
    public class SBPParser {
         //RandomAccessFile _file = null;
         FileInputStream _file = null;
         DataInputStream _data = null;
         int eofmark = 0;
         public SBPParser(FileInputStream f, DataInputStream d) {
              _file = f;
              _data = d;
         public void parseSBPFile() {
              try {
                        int hd = _data.readUnsignedByte();
                        double douhd = (hd & 0xff) * 0.2;
                        int svid = (_data.readUnsignedByte()) & 0xff;
                        int utcsec = _data.readUnsignedShort();
                        double utcs = (utcsec & 0xff) * 0.001;
                        int utc = Integer.parseInt(utcdatetime);
                        int sec = utc & 0xFC000000;
                        int min = utc & 0x3F00000;
                        int hour = utc & 0xF8000;
                        int day = utc & 0x7C00;
                        int months = utc & 0x3FF;*/
                        int utc = _data.readInt();
                        int sec = utc & 0xFC000000; //For fetching first 6 bits
                        int min = utc & 0x3F00000;  //For fetching next 6 bits
                        int hour = utc & 0xF8000;    //For fetching next 5 bits
                        int day = utc & 0x7C00;   //For fetching next 5 bits
                        int months = utc & 0x3FF; //For fetching next 10 bits
                        int svidlist = (_data.readInt()) & 0xff;
                        int lat = (_data.readInt()) & 0xff;
                        double latitude = lat * 0.0000001;
                        int lon =(_data.readInt()) & 0xff;
                        double longitude = lon * 0.0000001;
                        int alt = (_data.readInt()) & 0xff;
                        System.out.println("Value of Hdop ==> " + douhd);
                        System.out.println("Value of SVIDCnt ==> " + svid);
                        System.out.println("Value of UtcSec ==> " + utcs);
                        System.out.println("Value of seconds ==> " + sec);
                        System.out.println("Value of minutes ==> " + min);
                        System.out.println("Value of hours ==> " + hour);
                        System.out.println("Value of day ==> " + day);
                        System.out.println("Value of months ==> " + months);
                        System.out.println("Value of SVIDList ==> " + svidlist);
                        System.out.println("Value of Latiutude ==> " + latitude);
                        System.out.println("Value of Longitude ==> " + longitude);
                        System.out.println("Value of Altitude ==> " + alt);
                        //_data.skipBytes(31);
              } catch(Exception e) {
                   e.printStackTrace();
         public void print() {
              for(int i = 0; i < hdopVct.size(); i++) {
                   System.out.println("HDOP => " + hdopVct.elementAt(i));
              /*System.out.println("SVIDCNT => " + svidlistVct.elementAt(0));
              System.out.println("UTCSEC1 => " + utcsecVct.elementAt(0));
              System.out.println("UTCSEC2 => " + utcsecVct.elementAt(1));*/
         public static void main(String [] args) {
              try {
                   FileInputStream file_input = new FileInputStream("000000000_GPSLOG_20090605_122548.sbp");
                  DataInputStream data_in    = new DataInputStream (file_input);
                   SBPParser sbp = new SBPParser(file_input, data_in);
                   sbp.parseSBPFile();
                   //sbp.print();
              } catch(Exception e) {
                   e.printStackTrace();
    }Thanks & Regards
    Sunil

  • Read Binary File, T=1

    Hello,
    I'm trying to read a binary file from a card.
    This is something I can easily do, at least until now :)
    I'm testing a new card (with protocol T=1 instead of the T=0 I used so far) and I don't have the card technical document (with the APDUs format) but I guess the APDUs are the same (for the read binary command and other more general commands like the select and others alike).
    I can select the pretended files and navigate in the card EFs/DFs, but for some reason I cant read a complete binary file (it stops before the last block reading).
    This is the code I have used so far without problems:
    private static byte[] readBinary() throws CardException, IOException {
      //CardChannel channel = getCardChannel();
      ByteArrayOutputStream os = new ByteArrayOutputStream(256);
      boolean finished = false;
      byte offset = (byte) 0x00;
      int i=1;
      while (!finished) {
         //Read binary command
         ResponseAPDU resp = channel.transmit(new CommandAPDU(0x00, 0xB0, offset,
                   0x00, READ_BUFFER_LENGTH));
         i++;
         byte[] data = resp.getData();
         offset = (byte) (offset + (byte) 0x01);
         os.write(data);
         if (data.length < READ_BUFFER_LENGTH) {
              finished = true;
      return os.toByteArray();
    }Where READ_BUFFER_LENGTH = 256
    If with the other cards I tested this code works fine why doesn't it work with this card? The only difference is at the transport protocol, this card protocol is T=1 and the other cards was T=0, but I thought that PC/SC would hide the transport protocol layer
    Thanks for any help
    Brian
    Edited by: Brian.Rep on Dec 26, 2008 3:16 AM

    Brian.Rep wrote:
    Hello,
    I'm trying to read a binary file from a card.If it is a Java Card, it would help to see source code of the applet.
    This is something I can easily do, at least until now :)
    I'm testing a new card (with protocol T=1 instead of the T=0 I used so far) and I don't have the card technical document (with the APDUs format) but I guess the APDUs are the same (for the read binary command and other more general commands like the select and others alike).
    I can select the pretended files and navigate in the card EFs/DFs, but for some reason I cant read a complete binary file (it stops before the last block reading).
    This is the code I have used so far without problems:
    Where READ_BUFFER_LENGTH = 256
    If with the other cards I tested this code works fine why doesn't it work with this card? The only difference is at the transport protocol, this card protocol is T=1 and the other cards was T=0, but I thought that PC/SC would hide the transport protocol layerThis is not quite true. Java Card tries to hide the underlying protocol used, but it is not quite possible with T=0. You need to know what APDU case you're expecting for CLA/INS in order to pick up the data field or send outgoing data.
    What error do you get?

  • Please Help on Reading Binary file

    I'm trying to read a binary file and find a string like "DE F0 CA 01"
    i'm new for java and looked in the forum for solution without success
    please help
    best regards
    gebi

    now i can read the binary file, my problem is now to find the position of byte sequence in the file
    here is my code, may be it helps
    import java.io.*;
    public class ReadBinary
         public static void main(String f[])
              FileInputStream fis;
              DataInputStream dis;
              String value;
              char charIn;
              try
                   fis = new FileInputStream(f[0]);
                   dis = new DataInputStream(fis);
                   System.out.println("file " + f[0]);
                   while (dis.available() > 0)
                        value = Integer.toHexString(dis.read());
              catch (EOFException eof)
                   System.out.println("EOF reached");
              catch (IOException ioe)
                   System.out.println("IO Error " + ioe);
    }best regards
    gebi

  • Ipod too slow when reading video files via DVD player with USB port.

    Hi all, this is my first message in this forum.
    I've got a DVD player that has a USB port and reads all types of disks. When I use my IPOD 80 GB the files I read are somehow frozen and don't run properly (lack of sync between audio and video, etc). I've got version 1.2.1 and use it in disk mode otherwise the player doesn’t see it. Meanwhile a friend of mine uses it's 60 Gb IPO on the same player, version 1.0 and it doesn't need to go to disk mode, and the player reads perfect from it. Is this a problem of the ipod software version?
    Both ipods are in Fat32 format, but in his ipod the partition is set to active. I can't change mine to that, because windows is seeing my IPOD as a removable device, and my friend’s IPOD as a disk.
    Hope I haven't been too confusing, and I welcome any help anyone can give.
    Thanks.

    Sorry, but I don't even know
    what it is you are trying to do. Are you attaching
    the iPod to a stand alone DVD player? Are you trying
    to export stuff out of the iPod into the DVD player?
    Patrick
    Hi, I'm attaching the iPod to a stand alone DVD player, that has a usb input port. And then read video files from it.

  • Display PDF in different window by reading Binary file in MIME folder.

    Hi all,
    I've kept binary file in MIME folder and I'm trying to read the contents of it. and I'm trying to display contents in pdf format in separate window.
    For this I was able to read the file by using FileInputStream
    byte[] bytes = new byte[1000];
         try
                  //File file = new File(WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(),"data.bin"));
                   File file = new File(WDURLGenerator.getResourcePath("test.bin"));
                   FileInputStream is = new FileInputStream(file);
                  long length = file.length();
                   //Create the byte array to hold the data
                   byte[] bytesA = new byte[(int)length];
                   //Read in the bytes
                    int offset = 0;
                    int numRead = 0;
                    while (offset < bytesA.length&& (numRead=is.read(bytesA, offset, bytesA.length-offset)) >= 0)
                         offset += numRead;
                        is.close();
                   bytes = bytesA;
                   wdContext.currentContextElement().setPdfSource(bytes);
                 catch(Exception e)
                   wdComponentAPI.getMessageManager().reportException("Error Reading File:"+e.getCause(),true);
                   wdComponentAPI.getMessageManager().reportException("Error Reading File To String:"+e.toString(),true);
    I've to show this in external window. I created attribute "pdfResource" of type binary
    and declared in the source
    private ISimpleType myPdfDoc;
    After that I added this bit of code in Init Method
    myPdfDoc = wdContext.getNodeInfo().getAttribute("pdfResource").getModifiableSimpleType();
    ((IWDModifiableBinaryType)myPdfDoc).setMimeType(WDWebResourceType.PDF);
    ((IWDModifiableBinaryType)myPdfDoc).setFileName("PDF");
    After this I made an action handler which has following code.
    final byte[] documentContent = bytes
    final String docUrl = myPdfDoc.format( bytes );
    String docUrl = myPdfDoc.format(bytes);
    IWDWindow window = wdComponentAPI.getWindowManager().createExternalWindow(docURL,"Resource", true);
    window.open();
    But it is throwing Null Pointer exception for Init Method and also while displaying.
    Where is that I'm faltering. Any Pointers will be help ful
    Thanks
    Srikant

    First of all why do you need those three lines of code in your wdDoInit() ?
    Simply, you may use this:
    IWDCachedWebResource cachedResource = null;
    String url = null;
    byte[] pdf = wdContext.currentContextElement().getPdfSource();
    if (pdf != null) {
       cachedResource = WDWebResource.getWebResource(pdf, WDWebResourceType.PDF);
       url = cachedResource.getURL();
    Refer <a href="http://help.sap.com/saphelp_nw04s/helpdata/en/4a/fb8342a7d30d53e10000000a155106/content.htm">Utilizing the IWDCachedWebResource-API</a> for further assistance.
    /* Changed the code */
    Message was edited by: Bala Krishnan

Maybe you are looking for