I have been doing a lot with Java lately. But you can not know everything or do everything alone. Sometimes you need help. That's why I created this post to ask for help.
Here's what I've done so far:
Class DecryptedHashlink in C#:
Code
class DecryptedHashlink
{
public String Name { get; set; }
public IPAddress IP { get; set; }
public ushort Port { get; set; }
}
Translation to Java:
Code
class DecryptedHashlink
{
private String name;
private InetAddress ip;
private Byte port;
public String getName(){
return name;
}
public void setName(String newName){
this.name = newName;
}
public InetAddress getIP(){
return ip;
}
public void setIP(InetAddress newIP){
this.ip = newIP;
}
public Byte getPort(){
return port;
}
public void setPort(Byte newPort){
this.port = newPort;
}
}
Method DecodeHashlink in C#:
Code
public static DecryptedHashlink DecodeHashlink(String hashlink)
{
DecryptedHashlink room = new DecryptedHashlink();
try
{
if (hashlink.ToUpper().StartsWith("CHATROOM:")) // not encrypted
{
hashlink = hashlink.Substring(9);
int split = hashlink.IndexOf(":");
room.IP = IPAddress.Parse(hashlink.Substring(0, split));
hashlink = hashlink.Substring(split + 1);
split = hashlink.IndexOf("|");
room.Port = ushort.Parse(hashlink.Substring(0, split));
room.Name = hashlink.Substring(split + 1);
return room;
}
else // encrypted
{
byte[] buf = Convert.FromBase64String(hashlink);
buf = d67(buf, 28435); //I could translate up to here
buf = Zip.Decompress(buf);
HashlinkReader reader = new HashlinkReader(buf);
reader.SkipBytes(32);
room.IP = reader;
room.Port = reader;
reader.SkipBytes(4);
room.Name = reader;
return room;
}
}
catch // badly formed hashlink
{
return null;
}
}
I translate it to Java so:
Code
public static DecryptedHashlink DecodeHashlink(String hashlink){
DecryptedHashlink room = new DecryptedHashlink();
try
{
if (hashlink.toUpperCase().startsWith("CHATROOM:")) // not encrypted
{
hashlink = hashlink.substring(9);
int split = hashlink.indexOf(":");
try {
room.setIP(InetAddress.getByName((hashlink.substring(0, split))));
}
catch (Exception ex) {
}
hashlink = hashlink.substring(split + 1);
split = hashlink.indexOf("|");
room.setPort(convertStringToByte(hashlink.substring(0, split)));
room.setName(hashlink.substring(split + 1));
return room;
}
else // encrypted
{
byte[] buf = DatatypeConverter.parseBase64Binary(hashlink);
buf = d67(buf, 28435); //I could translate up to here
buf = Zip.Decompress(buf);
HashlinkReader reader = new HashlinkReader(buf);
reader.SkipBytes(32);
room.IP = reader;
room.Port = reader;
reader.SkipBytes(4);
room.Name = reader;
return room;
}
}
}
public static byte convertStringToByte(String str)
{
// Convert string to byte
// using parseByte() method
return Byte.parseByte(str);
}
Method d67 in C#:
Code
private static byte[] d67(byte[] data, int b)
{
byte[] buffer = new byte[data.Length];
Array.Copy(data, buffer, data.Length);
for (int i = 0; i < data.Length; i++)
{
buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
b = (b + data[i]) * 23219 + 36126 & 65535;
}
return buffer;
}
Translation to Java:
Code
private static byte[] d67(byte[] data, int b)
{
byte[] buffer = new byte[data.length];
System.arraycopy(data, 0, buffer, 0, data.length);
for (int i = 0; i < data.length; i++)
{
buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
b = (b + data[i]) * 23219 + 36126 & 65535;
}
return buffer;
}
And now I have problems with the method Decompress from Class Zip:
Code
public static byte[] Decompress(byte[] data)
{
try
{
byte[] r = null;
using (MemoryStream ms = new MemoryStream(data))
using (Stream s = new InflaterInputStream(ms))
{
List<byte> list = new List<byte>();
int count = 0;
byte[] b = new byte[8192];
while ((count = s.Read(b, 0, 8192)) > 0)
list.AddRange(b.Take(count));
r = list.ToArray();
list.Clear();
list = null;
}
return r;
}
catch { }
return new byte[] { };
}
Specially with this part from code:
Code
using (MemoryStream ms = new MemoryStream(data))
using (Stream s = new InflaterInputStream(ms))
Having only JDK 7, how can I translate it to Java?
Does a try have to be written within another try?
And what is the equivalent of MemoryStream, Stream and InflaterInputStream?
Regards and thanks