yenc and zip in Java - or bytes[] to String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christophe Elek

    #1

    yenc and zip in Java - or bytes[] to String

    Ok, I am completly at lost :)

    in both cases (my Yenc and zip utilities) i try to do the following

    1) get a String
    2) transform in byte[]
    3) so some computation (either adding value to the byte or passing it to
    the ZipOutputStream on a ByteArrayOutput Stream)
    4) then transform back into String

    In both cases the write(byte[]) does not generate propert stream
    I checked Roedy's conversion: http://mindprod.com/jgloss/conversion.html
    Still not working :(

    Is there anything I am missing ?

    *************

    public static String compress(String theString) throws Exception{
    ByteArrayOutput Stream out = null;
    ZipOutputStream zout = null;
    String compressedStrin g = null;
    byte[] theBytes = theString.getBy tes("8859_1");
    CRC32 CRC = new CRC32();
    CRC.update(theB ytes);

    try {
    out = new ByteArrayOutput Stream(theStrin g.length());
    zout = new ZipOutputStream (out);
    ZipEntry entry = new ZipEntry("strin g");
    entry.setCrc(CR C.getValue());
    zout.putNextEnt ry(entry);
    zout.write(theB ytes,0,theBytes .length);
    zout.closeEntry ();
    zout.finish();
    zout.close();
    out.close();
    compressedStrin g = new String(out.toBy teArray(),"Cp12 52");
    } catch (IOException e){
    throw new Exception("Unab le to compress the data.",e);
    } finally {
    if (zout!=null){
    try {zout.close();} catch(Exception e){};
    }
    }
    return compressedStrin g;
    }

    *********

    public class Encoder {

    public static final String CARRIAGE_RETURN = System.getPrope rty
    ("line.separato r");
    private static String CHARSET = "Cp1252";

    public static String yEncode(String postname, InputStream source,
    int linelength, long sourcelength) throws IOException {

    byte theReadByte;
    byte theEncodedByte;
    byte[] theBuffer = new byte[4100];
    byte[] theEncodedBuffe r = new byte[linelength+1];
    int encodedIndex = 0;

    StringBuffer yencodedStream = new StringBuffer();
    String yencodedLine;
    yencodedStream. append("=ybegin line="+lineleng th+"
    size="+sourcele ngth+" name="+postname +CARRIAGE_RETUR N);

    int bytesRead = source.read(the Buffer);
    while (bytesRead>0) {
    for (int srcIndex = 0; srcIndex <= bytesRead; srcIndex++)
    {
    theReadByte = theBuffer[srcIndex]; // Get a
    source byte
    theEncodedByte = (byte)((theRead Byte+42)%256); //
    and add the secret number
    switch (theEncodedByte ) { // Solve special in NNTP
    'forbidden' characters
    case 0:
    case 9:
    case 10:
    case 13:
    case '=': // Including the escape character itself
    case '.': // Some usual servers have problems with
    a dot in the first column
    theEncodedBuffe r[encodedIndex]='=';
    encodedIndex++;
    theEncodedByte= (byte)(theEncod edByte+64);
    }
    theEncodedBuffe r[encodedIndex]=theEncodedByte ;
    encodedIndex++;
    if ((encodedIndex> =linelength)) // Block full
    {
    yencodedLine = new String
    (theEncodedBuff er,0,encodedInd ex,CHARSET);
    yencodedStream. append(yencoded Line);
    yencodedStream. append(CARRIAGE _RETURN);
    theEncodedBuffe r = new byte[linelength+1];
    encodedIndex=0;
    }
    } // end of for all bytes read

    bytesRead = source.read(the Buffer);
    } // end of while not EOF
    // End of File, dump the rest of the encoded buffer
    yencodedStream. append(new String
    (theEncodedBuff er,0,encodedInd ex-1,CHARSET)); // dunno why but the last
    char is always * and should not be there
    yencodedStream. append(CARRIAGE _RETURN);

    yencodedStream. append("=yend
    size="+sourcele ngth+CARRIAGE_R ETURN);
    return yencodedStream. toString();
    }

    public static String yEncode(String name, FileInputStream fin, int
    i, long l, Charset theCharSet) throws IOException {
    CHARSET = theCharSet.disp layName();
    return yEncode(name,fi n,i,l);
    }
    }

    --
    Christophe Elek
    Complex and difficult problem resolution specialist
    IBM Software Groupe - Support
    Eclipse Project - Update Core
Working...