473,508 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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
Nov 7 '05 #1
0 6450

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2351
by: Freddie | last post by:
Hi, I posted a while ago for some help with my word finder program, which is now quite a lot faster than I could manage. Thanks to all who helped :) This time, I've written a basic batch...
5
369
by: Marvin Grill | last post by:
Hi, I'm working on a type of NewsHound program and am stuck on how to decode the yEnc encoded articles (body). I tried using Joe Feser's yEnc class but can't seem to get it to work properly Not...
2
2045
by: Phillip Hamlyn | last post by:
I'm trying to get a Yenc Decoding algorithm working but keep getting the same problem. I'm using data from the yenc test newsgroup and have tried the same thing with most of the example source code...
3
11029
by: AHanso | last post by:
Hey I am new to C# (My background is in Java), I am writing a C# application (that uses the Compact Framework) that communicates to a Java server. To login the server is expecting the password...
6
6825
by: Scirious | last post by:
People, many of you may not know what yEnc is. To simplify, it is a methode to encode binary files very used in Usenet because it creates an overhead of only 2% when compare to 33%-40% of overhead...
0
3117
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
6
2232
by: Extremest | last post by:
Does anyone know how to decode a yenc encoded file? I have created a decoder that seems to work great for text. Now i am trying to create another one for images and I can't get it to work. I...
2
2167
by: John Savage | last post by:
I save posts from a midi music newsgroup, some are encoded with yenc encoding. This gave me an opportunity to try out the decoders in Python. The UU decoder works okay, but my YENC effort gives...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.