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

[863 byte] By [LoveOpensourcea] at [2007-11-27 9:00:19]
# 1
With the information given, all I can offer is "it doesn't work"
georgemca at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 2
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 methodi can pass two values hard codedthanks ranu
LoveOpensourcea at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 3

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;

}

cotton.ma at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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

georgemca at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 5
the two values i want to pass to my method offset and length are the 2nd and last parameters in the original method
LoveOpensourcea at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 6

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 > top of Java-index,Java Essentials,Java Programming...
# 7
> the two values i want to pass to my method offset and> length are the 2nd and last parameters in the> original methodRequest for relevant information ignored again. Good luck
georgemca at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 8
is that a wrong way to create anonymous inner classplease point me to right direction to overload the method of toByteArray() method od ByteArrayOutputStream classthanks ranu
LoveOpensourcea at 2007-7-12 21:28:49 > top of Java-index,Java Essentials,Java Programming...
# 9

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 > top of Java-index,Java Essentials,Java Programming...