what's wrong with my anonymous inner class
actually i am writing anonymous inner class to over load toByteArray() method of ByteArrayOutputStream class.
my code follows
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//assiging some value to baos
//overloading the method
byte[] wholeMessage= baos.toByteArray(new ByteArrayOutputStream()
{
public byte[] toByteArray(int offset,int length1)
{
byte abyte0[] = new byte[length1];
System.arraycopy(buf, offset, abyte0, 0, length1);
return abyte0;
}
});
can any body point me whats wrong with my method
the original code for toByteArray method as follows
public synchronized byte[] toByteArray()
{
byte abyte0[] = new byte[count];
System.arraycopy(buf, 0, abyte0, 0, count);
return abyte0;
}
regards
ranu
What in the world is the point of this method?
public synchronized byte[] toByteArray()
{
byte abyte0[] = new byte[count];
System.arraycopy(buf, 0, abyte0, 0, count);
return abyte0;
}
> sorry for uncompleted description. in that ananymous
> inner class that method should take two integers
> first one is the offset in the original method
> second
> one is length in the original method
> i can pass two values hard coded
> thanks
> ranu
You still haven't actually said what goes wrong
You haven't said what the error is.
> ByteArrayOutputStream baos = new
> ByteArrayOutputStream();
Why are you creating the above stream?
> byte[] wholeMessage= baos.toByteArray(new
> ByteArrayOutputStream()
There is no ByteArrayOutputStream.toByteArray method that takes a ByteArrayOutputStream.
jbisha at 2007-7-12 21:28:49 >

Now that I think of it, how would you call an overloaded method in an anonymous inner class?
import java.io.*;
public class Test117 {
public static void main(String[] args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream() {
public byte[] toByteArray(int offset, int length) {
byte[] theBytes = {1,2,3,4,5,6,7,8};
return theBytes;
}
};
//System.out.println(baos.toByteArray(3,4));
// Wait, a ByteArrayOutputStream does not have this method.
// I'll have to cast to my anonymous class - what's that class' name again?
// Well, I guess you can call the overloaded method this way but it
// is pretty limited.
byte[] b = (new ByteArrayOutputStream() {
public byte[] toByteArray(int offset, int length) {
byte[] theBytes = {1,2,3,4,5,6,7,8};
return theBytes;
}
}).toByteArray(3,4);
System.out.println(b);
}
}
Message was edited by:
jbish
jbisha at 2007-7-12 21:28:49 >
