1

I am a novice user trying to figure out how to use uuencode method. We have a form that allows only a single text file to be uploaded. Now it looks like only zip files will be uploaded. I am trying to include uuencode method to convert bytes to String so that we dont have to modify the rest of the code to accommodate the binary file.

Original code:

public void SettingUpload(File inputfile) { 
    this.inputfile = inputfile;
}

I changed it to

public void SettingUpload(File inputfile){
UUEncoder uuec = new UUEncoder();
    try{
        InputStream is = new FileInputStream(inputfile);
        OutputStream os = new FileOutputStream(inputfile);
        uuec.encodeBuffer(is, os);
        this.inputfile = inputfile;
    }catch (Throwable error) {
        reportError(error, "Error converting zipfile");
    }

}

When I tested it out, I got a java.io.EOFException. I grabbed the uuencoded file and manually uudecoded it. When I tried to unzip it,

bash1:~>unzip s6b0c9e663c74f72941bd8271a5fac3b.bin 
 Archive:  s6b0c9e663c74f72941bd8271a5fac3b.bin

 End-of-central-directory signature not found.  Either this file is not
 a zipfile, or it constitutes one disk of a multi-part archive.  In the

Edit:

I changed it to:

 public void SettingUpload(File inputfile){
    UUEncoder uuec = new UUEncoder();
        try{
            InputStream is = new FileInputStream(inputfile);
                   File OutputFile=new File("Output");
                        OutputFile.createNewFile();
            OutputStream os = new FileOutputStream(OutputFile);
            uuec.encodeBuffer(is, os);
            this.OutputFile = OutputFile;
        }catch (Throwable error) {
            reportError(error, "Error converting zipfile");
        }

 }

and I get the following error:

cannot find symbol
symbol  : variable OutputFile
devrys
  • 1,571
  • 3
  • 27
  • 43
user1719051
  • 109
  • 2
  • 14

1 Answers1

5

As Haozhun has commented, you shouldn't do this:

InputStream is = new FileInputStream(inputfile); // Good
OutputStream os = new FileOutputStream(inputfile); // Bad

You should be outputting to a separate file, otherwise the first time you write, you will trash the original file.

UPDATED with example

This works fine for me...

public static void main(String[] args) {

    File inputfile = new File("file/to/be/encoded");
    File outFile = new File("out.uue");

    UUEncoder uuec = new UUEncoder();
    InputStream is = null;
    OutputStream os = null;
    try {

        is = new FileInputStream(inputfile);
        os = new FileOutputStream(outFile);
        uuec.encodeBuffer(is, os);

    } catch (Exception error) {
        error.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
        try {
            os.close();
        } catch (Exception e) {
        }
    }

    File newFile = new File("decoded.jpg");
    UUDecoder decoder = new UUDecoder();
    try {

        is = new FileInputStream(outFile);
        os = new FileOutputStream(newFile);
        decoder.decodeBuffer(is, os);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
        try {
            os.close();
        } catch (Exception e) {
        }
    }

}

Also, I would return the output file from you encode methods

public void SettingUpload(File inputfile) throws IOException {
    UUEncoder uuec = new UUEncoder();
    File outFile = File.createTempFile("encoded", "uue");
    InputStream is = null;
    OutputStream os = null;
    try{
        is = new FileInputStream(inputfile);
        os = new FileOutputStream(outFile );
        uuec.encodeBuffer(is, os);
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
            try {
        os.close();
        } catch (Exception e) {
        }
    }
    return outFile;
}

You should never suppress exceptions. How would the caller know if something has gone wrong?

Also, if you open a stream, you're responsible for closing it, so make sure you're closing your streams.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for the quick response. I added File OutputFile=new File("Output");
    OutputFile.createNewFile();
    I got the following errors [293,7] cannot find symbol symbol : variable OutputFile.
    – user1719051 Oct 04 '12 at 06:52
  • Thanks! I cannot return the outFile. Setting this.outFile=outFile gives symbol error. Is there an alternative – user1719051 Oct 04 '12 at 07:21
  • No, not really. Unless you have a member variable at the class level that you can assign it to, returning really is the most viable way. – MadProgrammer Oct 04 '12 at 07:27