Console grab binary output

I'm playing around with a small app that will save mysqldump output to file.
I managed to get it working, except when there's binary data on blob fields.
So, if I run from a console:
mysqldump -h server -u uni testdb > /tmp/test.1sql
and then the program listed below, I get excessive garbage in the binary fields and file sizes grow up:
-rw-r--r-- 1 nobody nobody 4535097 Aug 20 14:36 /tmp/test1.sql
-rw-r--r-- 1 nobody nobody 6493448 Aug 20 14:51 /tmp/test.sql
So, I tried executing with the --hex-blob flag in the mysqldump command, but in this case I've to pay the extra size cost:
-rw-r--r-- 1 nobody nobody 7131966 Aug 20 14:47 /tmp/test1.sql
As you will see, I'm grabbing the contents through an InputStreamReader
and then I write they with OutputStreamWriter. I didn't choose execute the command and save the file by the console 'cause I would prefer know what's going on, count bytes, showing a progress or whatever...
I would like to know how could I grab the binary output of the command ?...
It seems like the sh command is not binary...
Thank you!
This is the code:
class StreamGobbler extends Thread
InputStream is;
String type;
String Filename;
StreamGobbler(InputStream is, String type,String Filename)
this.is = is;
this.type = type;
this.Filename=Filename;
public void run()
try
PrintWriter out;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
if (type=="ERROR") {
FileWriter outFile = new FileWriter("/tmp/error.log");
out = new PrintWriter(outFile);
String line=null;
while ( (line = br.readLine()) != null)
if (type=="ERROR") {
System.out.println(type + ">" + line);
out.println(line);
} else {
out.println(line);
out.close();
} else {
// FileWriter outFile = new FileWriter(Filename);
// out = new PrintWriter(outFile);
FileOutputStream fos = new FileOutputStream(new File(Filename));
OutputStreamWriter outs = new OutputStreamWriter(fos,"UTF8");
int len = 512;
char buf[] = new char[len];
int numRead;
while ((numRead = isr.read(buf, 0, len)) != -1)
outs.write(buf, 0, numRead);
outs.close();
} catch (IOException ioe)
ioe.printStackTrace();
public class mxdump {
public mxdump(String Cmd) {
try {
Cmd="/bin/bash /usr/bin/mysqldump -h server -u uni testdb";
/* String cmd[]={"sh", "-c",Cmd,""} ; //"mysqldump";
ProcessBuilder pb=new ProcessBuilder(cmd);
System.out.println("Start");
pb.start();
System.out.println("Termino"); */
String cmd[]={"sh", "-c",Cmd,""} ;
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR","");
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT","/tmp/test.sql" );
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
catch(Throwable t)
//System.out.println(e);
t.printStackTrace();
}

Don't use Readers and Writers on binary data. Use Streams.

Similar Messages

  • Passing binary output in servlets

    Hi all,
    I am new to struts.In Jsp i need to give three text field values
    1.category
    2.two date fields.
    On click of submit button,it should export the csv file from db into excel sheets.
    If i didnt give the input values properly and clicking the submit button will display the error msg.
    But next time if i give the proper input values,then it should disappear.
    but my problem is the err msg is not removing from jsp but export is working and in my console :
    SEVERE: Servlet.service() for servlet action threw exception+
    java.lang.IllegalStateException: Cannot forward after response has been committed*
    **and in my action class:**
    The following condition is to check existence of err:
    if (category.length() == 0 || startdate.length() == 0
                        || enddate.length() == 0) {
                   ActionMessages errors = this.getErrors(request);
                   errors.add("fatal", new ActionMessage(DATE_FIELD_MISSING, ""));
                   saveErrors(request, errors);
                   return mapping.findForward("failure");
    The following code is for export  the data:+
    if (category.equals("book")) {
                   String fileName = "part.csv";
                   response.setContentType("application/octet-stream");
                   response.setHeader("Content-Disposition", "attachment; filename=\""
                             + fileName + "\"");
                   try {
                        OutputStream oStream = response.getOutputStream();
                        oStream
                                  .write(" Number, Description, Revision, Dummy, Manual, Classification, Owner, Global Effective Date, Global Expiration Date, New Part\n"
                                            .getBytes());
                        for (int i = 0; i < result.size(); i++) {
                             PartDTO part = (PartDTO) result.get(i);
                             StringBuffer sbpart = new StringBuffer(part.getPartNumber());
                             sbpart.append(',');
                             sbpart.append(part.getPartDescription());
                             sbpart.append(',');
                             sbpart.append(part.getRevision());
                             sbpart.append(',');
                             sbpart.append(part.getIsDummy());
                             sbpart.append(',');
                             sbpart.append(part.getIsManual());
                             sbpart.append(',');
                             sbpart.append(part.getClassification());
                             sbpart.append(',');
                             sbpart.append(part.getOwner());
                             sbpart.append(',');
                             sbpart.append(part.getEffectiveDate());
                             sbpart.append(',');
                             sbpart.append(part.getExpirationDate());
                             sbpart.append(',');
                             sbpart.append(part.getIsNewPart());
                             sbpart.append("\n");
                             oStream.write(sbpart.toString().getBytes());
                        oStream.close();               } catch (IOException ioe) {
    return mapping.findForward("success");
    I think oStream.close() will return to jsp .. so the last return statement is not working..
    I dont know how to do it in someother way......Plz help me.its urgent yaar......Thanks in advance....

    It's been awhile since I've used Struts, so don't put too much stock in this answer. That said...
    Generally, you should not close the response output stream, ever. The application server takes care of this, when and if the time is right.
    Also, it appears that you're writing output within an action, and then forwarding to a JSP. If you want to write binary output or some other type of output for which a text-template based JSP is not appropriate, forward to a plain old servlet, and write your output there. Define the servlet as you would any other in web.xml, and dispatch to it from Struts instead of going to a JSP.
    Good luck!
    - Jerry Oberle

  • How to display binary output in numeric indicator

    Hai
        How to display binary output in a Numeric display.
    in my program i use numeric display for displaying numeric values in one case
    in the other case i want to display binary output in the same numeric display window as binary,
     i don't want to make seperate o/p display for binary o/p i want change the property of the Numeric display to binary display .
    is it possible to display Binary data in numeric indicator, please give me an idea to do this.
    thanks
    sk
    Attachments:
    DBL2BIN.vi ‏39 KB

    Alternatively, you can use the FormatString property node, and use %08b as format string : 08 means : pad the output with 0 to get a 8 digits wide result.
    See attachment.
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        
    Attachments:
    DBL2BIN[1].vi ‏21 KB

  • How can I run external console program, printing output to JTextPane?

    How can I run external console program, printing output to JTextPane?
    I have a console app. written in C++ and I would like to run in it from java swing app. and I would like to see its output in a JTextPane.

    I have used this article
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    to successfully pipe output from jboss batch files to a JTextPane.

  • Formatting Binary Output

    I want to format binary output in text fields to confrom to the following scheme:
    head data1 data2 return offSet id
    6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
    where the first row are field names and the second is the filed width.
    What that means is that, using the value 1 (base 10) as an example, a 6 bit filed will read 000001 and so a 5 bit field will read 00001.
    My program converts to binary no worries, but how do I enforce the width requirements? I am using Integer.toBinaryString(int) to convert, if that helps.
    Thanks

    public String intToJustifiedBinaryString(int number, int length)
      StringBuffer buffer = new StringBuffer(Integer.toBinaryString(number));
      while (buffer.length() < length)
          buffer.insert(0, "0");
      return buffer.toString();It works great! Now for the learning. I looked up StringBuffer and found this explanation:
    StringBuffer(String str)
    Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.
    What I am trying to figure out is how it is not overwriting what already exists in the buffer. Lets pretend with int = 7 and a 6 bit field.
    So initially, buffer.length() = 3 and my buffer looks like 1 1 1.
    From the API: The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.
    If I am specifying my insert point as 0 (insert(int offset, char c) Inserts the string representation of the char argument into this string buffer.) How am I not overwriting one of my bits comprising my binary 7? My only guess is that insert operation first shifts what is already there over the necessary amount, based on the length of what is being inserted, before inserting at the specified point. I have read nothing to support this theory. Please let me know if it is correct.
    Thanks for the help!

  • How to send a binary output from ni daq 6009

    I am trying to get a binary output from ni daq 6009 to make the selections of a multiplexer.
    I am trying to make the selection directly from the labview program.
    Please help me in getting this binary output from ni daq 6009 to do the selection

    Try something like this. 
    I'm not a fan of daq-assistant express vi's... use the primitives.  Create the task outside the main structure, pass that task inside the loop and do a write where needed.  Close the task after the main loop.  This improves speed and labview performance.
    Attachments:
    ocelot.png ‏43 KB
    ocelot.vi ‏21 KB

  • Display PDF from BAPI's binary output

    Dear All,
    I am struck badly with this scenerio here.
    we have a Bapi which is generating an adobe form in the backend and providing that as an attribute of type binary.
    i used the following code to display that binary output, to be opened in Acrobet reader.
    byte[] pdfContent= wdContext.nodeZhra_Get_Lettertype_Desc_Input().nodeOutput().currentOutputElement().getE_Bin_File();
         IWDCachedWebResource pdfResource = WDWebResource.getWebResource(pdfContent,WDWebResourceType.PDF);
         IWDWindow win = wdComponentAPI.getWindowManager().createExternalWindow(pdfResource.getURL(),"PDF in Arabic",true);
         win.setTitle("PDF in Arabic");
         win.removeWindowFeature(WDWindowFeature.ADDRESS_BAR);
         win.removeWindowFeature(WDWindowFeature.TOOL_BAR);
         win.removeWindowFeature(WDWindowFeature.MENU_BAR);
         win.removeWindowFeature(WDWindowFeature.STATUS_BAR);
         win.show();
    the problem is that the acrobet reader opens and says it could not display the file and it may be not supported or might be damaged.BUT
    1. the file is being generated and correctly displayed on another system
    2. both systems have the same version and updates of acrober reader 9.
    Kindly let me know the solution or some work-around to solve this problem. Is there a way,where i should use interactive form to do this...
    thanks in advance

    Hello!
    If it works on one client but not on the other i think it is not a Server problem.
    Check th following:
    Open the document in the Browser where it is displayed and save it on a thumbdrive and open it on the other client.
    -> If it works: it must be Browser settings
    -> If not: the generated PDF is not valid for the Adobe Reader of the other client.
    If it works on the Server but not on the client, try to dump it to a file on the Webserver to see if the BAPI call (RFC/WS) is the problem. When you use XString for exampe I think there is a problem with the data beeing split into several lines. If you have the file you should be able to open it. Compare the size of the File on the WebAS and the backend.
    Kind regards
    Matthias
    Edited by: Matthias Schneider on Nov 4, 2008 6:19 PM

  • Binary Output On Serialport

    Hi,
    I am trying to send numbers from 0 to 255 over the serial port. Locally I can convert an integer to a byte by a cast. However, if I write this byte in the Outputstream with the OutputstreamWriter, all values from 128 to 160 get lost. Instead I recive the value 63.
    Using a workaround with ISO-8859-1 makes it possible to send all values except 129,141,143,144,157. Again, these are replaced by the value 63.
    OutputStreamWriter aus = new OutputStreamWriter(main.out);
    int value;
    String s;
    s = new Character((char) i).toString();
    s = new String(s.getBytes("ISO-8859-1"));
    aus.write(s);For the communication I am using the RxTx library under Windows XP pro.
    How can I write binary code to the serial port?

    If you want write byte per byte (not very effective) :
    DataOutputStream aus = new DataOutputStream(main.out);
    int value;
    aus.writeByte(value);If you want write more efficiently, gather data into byte arrays, and write into a buffered output.
    DataOutputStream aus = new DataOutputStream( new BufferedOutputStream(main.out) );
    byte [] values;
    //fill values, assuming numberOfValuesInArray is the number of bytes
    // in values that must be written, with numberOfValuesInArray<=values.length
    aus.write(value, 0, numberOfValuesInArray);
    //Don't forget to flush after writing the last array of values, or at any time you want to force data output on underlaying stream.
    aus.flush()

  • Binary Output

    Hey there, im pretty new to java, and i've stumbled across a problem with one assignment. I need to "Save a ?snapshot? of the situation say every 10 generations in a binary file".
    I've tried to use what i know, but if anyone could give me some pointers, that would be great.
    this is what i have so far.
    thanx
    try {
               ObjectOutputStream outputstream =
               new ObjectOutputStream(new FileOutputStream("binaryoutput.dat"));
               outputStream.writeObject(" " + temp + "             " + df.format(score) + " " +gen);
               System.out.println("Numbers written to the file binaryoutput.dat");
               outputStream.close();
            catch(IOException e) {
                System.out.println("Problem with file output.");

    the rest is from this part of my code.
       import java.text.*;
       import java.io.ObjectOutputStream;
       import java.io.FileOutputStream;
       import java.io.IOException;
       public class MonkeyGen
          public static void main(String [] args)
             DecimalFormat df = new DecimalFormat("#0.0000"); //Format of the percentag output.
             Genetic m = new Genetic();
             String t = "To be or not to be, that is the question."; //The line in which we need to have as our output.
             System.out.println(t);
             String temp = m.randString(t.length());
             System.out.println(temp);
             String [] slist = new String[10000]; //declaring a new array of 10,000 strings
             double score = 0, tempScore;
             int gen =0;
             while (score < 1.0)
                gen++;
                for (int n=0;n<10000;n++)
                   slist[n] = m.permuteString(temp.length(), temp);
                   tempScore = m.getScore(slist[n],t);
                   if (tempScore > score)
                      score = tempScore;
                      temp = slist[n];
                System.out.println(" " + temp + "             " + df.format(score) + " " +gen); // Prints out the final solution.
             System.out.println("--------------------------------------- "+ gen);im only really following lecture notes and my textbook, both which seem to point to using an ObjectOutputStream.
    the problem im really having is the fact that it's not compiling

  • Binary output from two tags

    Hi
    I'm having problems outputting a binary image from a tag handler more than once. This is apparently a known problem with JSP but I was wondering if anyone had found a workaround (other that writing a servlet).
    The basic problem is that if you call ServletResponse.getOutputStream() more than once in a page or if you call it after ServletResponse.getWriter() has been called then you get:
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    The result is that Tag Libraries are a technology that doesn't allow you to output two binary things directly onto a page or to output any binary data if you've also output text or vice versa. That sounds extraordinarily bad to me.
    I know there must be good reasons for the difficulty but surely there's a fix or a well known workaround.
    Has anyone out there got over this problem or know whether any fixes are planned? Thanks.
    Murray

    You can call either getOutputStream or getWriter, and you can it only once (it assumes you'll assign that to a variable and use the variable from then on). It's not a bug or problem, it's a reasonable restriction to prevent you from doing what I think you are trying to do....
    So you can use the writer to write text-based output, or the output stream to write binary data.
    But this is somewhat irrelevent. You can't output HTML and include binary data, like an image, in the HTML.
    <div>here is some text, followed by an image</div>
    <div>0a68df9834ce7932d....</div> <-- binary data...
    The browser has no idea what to do with that. That's why you have image tags and object/embed tags in HTML. And the browser has to make a separate request to get the content for the image tag. So the servlet can output image data or other stuff, if the image tag is used to call a servlet to serve an image. But they are not doing with the same call the a servlet (and usually you have separate servlets for that stuff).
    Ultimately, this is not a JSP/servlet issue. This is strictly and HTML, HTTP, browser issue: That's how it works and has worked since long before JSP and servlets came about. JSP/servlets do not change how HTML and browsers work in any way/shape/form. They only allow for dynamically generating content the browser gets.

  • Cfdocument and binary output

    I'm using the Fusebox3 framework.
    file: dsp_Invoice.cfm
    <cfdocument format="pdf" orientation="portrait"
    name="myPDF" overwrite="true">
    <cfcontent type="application/pdf" variable="#myPDF#">
    I get the binary results opened in Wordpad with the filename
    default.cfm
    Any ideas?
    Tried using Foxit PDF reader on IE7 and Firefox2. Both
    usually open PDF correctly from anywhere on the web.

    <cfheader name="Content-Disposition" value="inline;
    filename=invoice.pdf">
    <cfcontent type="application/pdf" variable="#myPDF#">
    in place of just the cfcontent tag alone.
    In any case, I would build the invoice document in
    dsp_Invoice.cfm. Then I would open it in a separate page,
    dsp_showInvoice.cfm, say, that contains just a cfheader and
    cfcontent tag.

  • Running programs and grabbing their output

    I'm kinda confused. I want to run command.com from java and be able to capture its output and print it somewhere else, as well as insert my own input through another medium (ie. JTextField). I know i can use runtime to execute it, and then use process to get the output stream and input stream, but when i get the output stream, it returns it as an output stream, which would mean i couldnt actually read what the program was outputting, i could only write to it, which doesnt make sense at all. Will somebody please explain to me what i'm doing wrong, or if i dont have it wrong, please explain to me how to do what i want to do.
    Thank you

    The above posts seem to be right:
    // Get the runtime. Replace 'this' with what you want from
    Runtime rt = this.getRuntime();
    // Intialize a variable for recieving the output
    byte b[] = new byte[10000];
    // Get a stream to recieve the output
    InputStream outputOfRT = rt.getRuntime();
    // Read the output. Use a better read() logic to all the stuff
    read(b);
    // Manipulate the b for what you want
    rajesh

  • Binary output at the digital output pin as 1111 1111 1111

    Hi 
     I am using DAQ 6009 and i need a output as 1111 1111 1111 at the digital output(12pin )of the DAQ.Please give an idea or a vi to do so
    Thanks in advance...
    Solved!
    Go to Solution.

    DKLabView wrote:
    I am using DAQ 6009 and i need a output as 1111 1111 1111 at the digital output(12pin )of the DAQ.
    It seems that you want to generate static value(s) on 12 pins of USB-6009. Find attached example and let me know if it helps.
    I am not allergic to Kudos, in fact I love Kudos.
     Make your LabVIEW experience more CONVENIENT.
    Attachments:
    Example - Write Digital [LV 2009].vi ‏10 KB

  • Remotely view output on a separate Linux console

    I am wondering if anybody knows of a way to view output on one Linux console from another Linux console.  Here is an example.  I have an Arch Linux desktop machine and a server machine.  I usually use my server remotely through ssh to compile software.  Some software takes forever to compile, and I will go somewhere to work on something.  I then remotely login from another machine, but I want to see the output from the compiling that is being output to my desktop at home.  Does anybody know of a way to grab that output so I can view it from a new console session?
    -Chris

    GNU screen is fantastic for stuff like this. There are plenty of forum threads out there that talk about setting it up and configuring it (this is a good place to start).[/url]

  • I want to show console output in my cmd prompt in C# winform application

    Hi,
    I'm launching the some process in C# .net Winform appliaction. But i couldn't able to see console output on the screen. The process is getting launched but inside cmd prompt window, it is showing nothing. I would like to show something on cmd prompt. Please
    help on this.
     using (Process comxdcProcess = new System.Diagnostics.Process())
                            comxdcProcess.StartInfo.FileName = fileName;
                            comxdcProcess.StartInfo.Arguments = args;
                            comxdcProcess.StartInfo.RedirectStandardError = true;
                            comxdcProcess.StartInfo.RedirectStandardOutput = true;
                            comxdcProcess.StartInfo.UseShellExecute = false;
                            comxdcProcess.Start();
                            this.errorComment = comxdcProcess.StandardError.ReadToEnd();
                            StreamReader myStreamReader = comxdcProcess.StandardOutput;
                            //// Read the standard output of the spawned process. 
                            this.errorComment = myStreamReader.ReadToEnd();
                            comxdcProcess.WaitForExit();
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari

    @Hariprasadbrk
    Do you mean you have use process class to start "cmd prompt" And want to display some output in it?
    If so, there is no need to use RedirectStandardOutput property. This property means the output of an application is written to the
    Process.StandardOutput stream.
    // Setup the process with the ProcessStartInfo class.
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\7za.exe"; // Specify exe name not cmd exe.
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    // Start the process.
    using (Process process = Process.Start(start))
    // Read in all the text from the process with the StreamReader.
    using (StreamReader reader = process.StandardOutput)
    string result = reader.ReadToEnd();
    Console.Write(result);
    Output
    This section shows the output of the process.
    7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19
    Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
    [<@listfiles...>]
    So you can start a cmd exe.
    Please also take a look at the article from codeproject
    How to redirect Standard Input/Output of an application
    In summary, the shutdown proces is invoked from my application, and it displays the output from the process in RichTextBox control.
    Have a nice day!
    Kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

Maybe you are looking for

  • Ipod not being recognized by comp or itunes.

    its not being recognize and the prob is not the usb port itself(flashdrives, cameras,ect will work) the comp is a dell and when i do connect it just charges and stuff no actually activity with the comp

  • Set default value to selection screen for report

    Hi All,    I've a report selection screen which has 3 fields from the report category: PNPPERNR, PNPSTAT1, PNPBTRTL. My logical database is PNPCE.    How can I set the default value to the field PNPSTAT1? Means when I first display the selection scre

  • How can I equalize exposures from a shoot?

    I did a studio shoot recently and all 400+ images were properly exposed. But at one point during the shoot I must have hit a setting on my camera that under-exposed by 1 stop or so about nine frames. Is there a way in Lightroom 4 to bring those nine

  • Why is my ipod not playing music through a docking station?

    Why is my ipod touch not playing music through a docking station?

  • Connection has already been created in this tx context

    Hi All, I am experiencing this problem and I am not able to find the solution for this. Please help. I have stateless EJB ('REQUIRED') and within that I have many EJB's using 'SUPPORTS' as transaction and in the flow I am also calling DAO object whic