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 ByteArrayOutputStream)
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{
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
String compressedString = null;
byte[] theBytes = theString.getBytes("8859_1");
CRC32 CRC = new CRC32();
CRC.update(theBytes);
try {
out = new ByteArrayOutputStream(theString.length());
zout = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry("string");
entry.setCrc(CRC.getValue());
zout.putNextEntry(entry);
zout.write(theBytes,0,theBytes.length);
zout.closeEntry();
zout.finish();
zout.close();
out.close();
compressedString = new String(out.toByteArray(),"Cp1252");
} catch (IOException e){
throw new Exception("Unable to compress the data.",e);
} finally {
if (zout!=null){
try {zout.close();}catch(Exception e){};
}
}
return compressedString;
}
*********
public class Encoder {
public static final String CARRIAGE_RETURN = System.getProperty
("line.separator");
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[] theEncodedBuffer = new byte[linelength+1];
int encodedIndex = 0;
StringBuffer yencodedStream = new StringBuffer();
String yencodedLine;
yencodedStream.append("=ybegin line="+linelength+"
size="+sourcelength+" name="+postname+CARRIAGE_RETURN);
int bytesRead = source.read(theBuffer);
while (bytesRead>0) {
for (int srcIndex = 0; srcIndex <= bytesRead; srcIndex++)
{
theReadByte = theBuffer[srcIndex]; // Get a
source byte
theEncodedByte = (byte)((theReadByte+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
theEncodedBuffer[encodedIndex]='=';
encodedIndex++;
theEncodedByte= (byte)(theEncodedByte+64);
}
theEncodedBuffer[encodedIndex]=theEncodedByte;
encodedIndex++;
if ((encodedIndex>=linelength)) // Block full
{
yencodedLine = new String
(theEncodedBuffer,0,encodedIndex,CHARSET);
yencodedStream.append(yencodedLine);
yencodedStream.append(CARRIAGE_RETURN);
theEncodedBuffer = new byte[linelength+1];
encodedIndex=0;
}
} // end of for all bytes read
bytesRead = source.read(theBuffer);
} // end of while not EOF
// End of File, dump the rest of the encoded buffer
yencodedStream.append(new String
(theEncodedBuffer,0,encodedIndex-1,CHARSET)); // dunno why but the last
char is always * and should not be there
yencodedStream.append(CARRIAGE_RETURN);
yencodedStream.append("=yend
size="+sourcelength+CARRIAGE_RETURN);
return yencodedStream.toString();
}
public static String yEncode(String name, FileInputStream fin, int
i, long l, Charset theCharSet) throws IOException {
CHARSET = theCharSet.displayName();
return yEncode(name,fin,i,l);
}
}
--
Christophe Elek
Complex and difficult problem resolution specialist
IBM Software Groupe - Support
Eclipse Project - Update Core