How to remove CRLF(13,10) from an Byte array
i have a ByteArrayOutputStream with the following content
[13, 10, 45, 45, 95, 78, 101, 120, 116, 80, 97, 114, 116, 0]
i have check whether my stream contains a Empty Line(CRLF) which is represented by 13,10 at the begining.
if it is there i have remove that 13,10.
any body please help me out in doing this
regards
ranadheer
i have a ByteArrayOutputStream with the following content
[13, 10, 45, 45, 95, 78, 101, 120, 116, 80, 97, 114, 116, 0]
i have to check whether my stream contains a Empty Line(CRLF) which is represented by 13,10 at the begining.
if it is there i have remove that 13,10.
any body please help me out in doing this
regards
ranadheer
> i have a ByteArrayOutputStream with the following content
> [13, 10, 45, 45, 95, 78, 101, 120, 116, 80, 97, 114, 116, 0]
> i have check whether my stream contains a Empty
> Line(CRLF) which is represented by 13,10 at the begining.
> if it is there i have remove that 13,10.
> any body please help me out in doing this
> regards
> ranadheer
Create an new array of length: old array.length-2 and copy the contents from the old array (starting from index 2) to the new array
Look at the arraycopy(...) method from the System class:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
thanks for u r reply.
can u give me a clue on the last parameter(lenght parameter) of arraycopy method actually i am getting exception over there. what can be the length paramater
actually my code follows
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
MutableBodyMimePart mime = MimeHelper.createMimePart(transportMessage, false);
mime.setOutputStream(baos);
mime.writePart();
byte[] wholeMessage = baos.toByteArray();
//here my job starts (removing the CRLF from the baos
ByteArrayOutputStream baos1=new ByteArrayOutputStream(baos.size()-2);
System.arraycopy(baos, 2,baos1,0,baos1.size());
baos1.toByteArray();
baos.close();
baos1.close();
iam getting exception of kind
java.lang.ArrayStoreException
regards'
ranu
byte[] oldArray = {13, 10, 45, 45, 95, 78, 101, 120, 116, 80, 97, 114, 116, 0};
final int SKIP = 2;
byte[] newArray = new byte[oldArray.length-SKIP];
System.arraycopy(oldArray, SKIP, newArray, 0, newArray.length);
System.out.println(java.util.Arrays.toString(oldArray));
System.out.println(java.util.Arrays.toString(newArray));
done. really wonderfulthanks and regardsranadheer
> done. really wonderful> thanks and regards> ranadheerYou're welcome. Of course SKIP should never exceed oldArray.length
and by the way is there any way to check the initial bytes whether its 13,10 or notjust like using an If condition
and by the way is there any way to check the initial bytes whether its 13,10 or notjust like using an If conditionregardsRanadheer
> and by the way is there any way to check the initial> bytes whether its 13,10 or not> just like using an If condition> regards> RanadheerYes, by using the == operator.
An additional question to that topic. What can I do if i want to remove multiple occurrences that are widespread over the array?
> An additional question to that topic. What can I do
> if i want to remove multiple occurrences that are
> widespread over the array?
I'd say use a dynamic list (like an ArrayList) to store the Bytes you're interested in. When you're done, convert it to an array of primitive bytes (if you want to). If you have more questions on the topic, I suggest creating a thread of your own about it.
> Thank youYou're welcome.