Article View: borland.public.jbuilder.java.language
Article #16098Re: ByteArray problem
From: Patrick Carroll
Date: Tue, 06 Apr 1999 00:00
Date: Tue, 06 Apr 1999 00:00
60 lines
2186 bytes
2186 bytes
getBytes() will convert a string into bytes according to the platform's default
character encoding, storing the result into a new byte array.
Hence, you're overwriting bMessage (bMessage = message.getBytes();) with a new
array of bytes which may not be exactly 300 bytes long.
I think you might want to rethink your code to remove the hard-coded lengths.
Pat
--
Zellig wrote:
> I'm trying to put somes ByteArray into a bigger one. The big one will be the
> param of a new DatagramPacket.
>
> When it comes into the bMessage[300] it give me an OutOfBoundArray
> Exception.
>
> it's never crashing to same "i" value might be 75 or 84, etc...
>
> i'm a bit lost with this basic problem
>
> S.O.S
>
> Z
>
> public void publish(String name,String phone,String email,String message){
> byte buf[] = new byte[373];
> byte bName[] = new byte[25];
> byte bPhone[] = new byte[14];
> byte bEmail[] = new byte[30];
> byte bMessage[] = new byte[300];
> byte REQUEST[] = {'a','d','p','u','b'};
> int i;
>
> bName = name.getBytes();
> bPhone = phone.getBytes();
> bEmail = email.getBytes();
> bMessage = message.getBytes();
>
> for(i=0;i<5;i++){
> buf[i] = REQUEST[i];
> }
> for(i=0;i<25;i++){
> buf[i+5] = bName[i];
> }
> for(i=0;i<30;i++){
> buf[i+30] = bEmail[i];
> }
> for(i=0;i<300;i++){
> System.out.println(i); // the problem is here
> buf[i+60] = bMessage[i];
> }
> for(i=0;i<14;i++){
> buf[i+360] = bPhone[i];
> }
> }
Technical Details
Message-ID:
<370A50E1.DB019D20@witsys.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7e2ifl$gsr5@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,769768ed403c57dd X-Google-Attributes: gidf74b3,public From: Patrick Carroll <PCarroll@witsys.com> Subject: Re: ByteArray problem Date: 1999/04/06 Message-ID: <370A50E1.DB019D20@witsys.com> X-Deja-AN: 463328915 Content-Transfer-Encoding: 7bit References: <7e2ifl$gsr5@forums.borland.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language