views:

79

answers:

2

Hi I am trying to implement the interface SourceStream in my application and overrides the method read(byte[],off,len) and I read the bytes from a server.But I want to convert those byte stream into String for that I used a string object by new String(byte[]) but it asks the initial byte in off and length of the bytes ie len as parameters..Why it is asking like that, as we contain only Strring(bye[])only. can any one help me...Thanks

A: 

Just provide 0 as the initial offset and yourArray.Length as the length and you're done. Quite why a method that takes just a byte array isn't provided is anyones guess - probably just to avoid 101 variations of the method.

Will A
A: 

If you just have a byte[] then you can create a new String via the String(byte[],int,int) constructor provided by the API.

In your case you would do

byte[] myBytes = ("Hello, World!").getBytes();
String myString = new String(myBytes, 0, myBytes.length);
System.out.println(myString);

EDIT: Try something like this:

int readLength = (len > bufSize ? bufSize : len);
for (int i = 0; i < readLength; i++) {
    b[off + i] = buffers[PBuf][PByte];
}
String metaSt = new String(b, 0, readLength);
KLee1
Hi Klee thanks for suggestion, but when I am using myBytes.length parameter it is complaining "primitive type byte of myBytes does not have a field length" error...
Koushik
Interesting... I think you are just using a `byte` where I am using a `byte[]`. You cannot use fields for primitive types (such as `byte`s).
KLee1
When I take directly means as it is coming from the stream(int bytes) even though it is said length is not resolved.I am using String myString=new String(b[off+i],0,b[off+i].length);cant we convert int Bytestream into string form?
Koushik
Can you post an example of your code? I don't know which Bytestream you are talking about.
KLee1
This is the code I am using...here bufSize is some length of bytespublic int read(byte[] b, int off, int len) throws IOException{ int readLength = (len > bufSize ? bufSize : len); for ( int i = 0; i < readLength; i++) { b[off+i] = buffers[PBuf][PByte]; metaSt=new String(b[off+i],0,b[off+i].length); } }
Koushik
Right now in your code, you are trying to make a `String` out of a single byte `b[off+i]` is a single byte.
KLee1
Thanks for clarification..But I want to convert the whole byteStream(mp3 blocks) into string so that I can recognize the metaData blocks and parse the song name..?
Koushik
This way you can get the `String` of what you have just pulled from the stream. Then you can concatenate the `String`s together to be the whole file and parse whatever information you need.
KLee1
It might be better for you to start a new question for this. You will likely get better response than just me. It is important to be very clear about what exactly your problem is and post example code or code that you have when asking a question. Good luck!
KLee1
Thanks Kenneth Lee....
Koushik