Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking the first 3 characters of a string 1

Status
Not open for further replies.

developerinlondon

Programmer
Jan 11, 2003
196
GB
I need a way to check if the first 3 characters of a string equals something:
eg
Code:
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = -1;
            while (true) {
                i = rd.read(buffer, 0, buffer.length);
                if (i == -1) break;
                baos.write(buffer, 0, i);
            }
            /*
             if baos.toByteArray() begins with '\xFF\xFE\x3C' then 
             */
            xml_in = new String(baos.toByteArray(), "utf-16");
             /*
              else
               */
             xml_in = new String(baos.toByteArray());
            baos.close();
basically I need to manually check if the data i am reading is UTF-16, if it is then convert it from utf-16 format, otherwise use standard method.

any thoughts on how I could do this?

thanks in advance.
 
I would probably do something like :

Code:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while (true) {
	i = rd.read(buffer, 0, buffer.length);
	if (i == -1) break;
	baos.write(buffer, 0, i);
}

byte[] byteData = baos.toByteArray();

if (checkData(byteData)) {
	xml_in = new String(byteData, "utf-16");
} else {
	xml_in = new String(baos.toByteArray());
}

baos.close();

where "checkData" is something along the lines of :

Code:
boolean checkData(byte[] data) throws IOException {
   if (data.length < 3) {
      throw new IOException("Checking job must have an array longer than 3 elements!");
   }
   
   if ((data[0] == '\xFF') && (data[1] == '\xFE') && (data[1] == '\x3C')) {
   	return true;
   } else {
   	return false;
   }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
xml_in = new String(baos.toByteArray());


should be :

xml_in = new String(byteData);


--------------------------------------------------
Free Database Connection Pooling Software
 
Humm, maybe I'm wrong but ...

if you want to check what are you reading, wouldn't you need to check your input (rd) rather that the output?

Cheers,

Dian
 
Whether the incoming data is UTF-16 or not, you want to do something with the input - which is read into the ByteArrayOutputStream - so it seems OK to me ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Ouch, there's an else after that.

My mistake, sorry.

Just as a matter of curiosity, String class has a startsWith method.

Cheers,

Dian
 
Yes, but according to the encoding of the data, the OP wants to do different things with it - so it makes much more sense to analyse the byte array (seeing as you have to convert the BAOS to a byte[] to create the String anyhow ...

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks for that sedj. just one small problem with it. I am getting the following -
Code:
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                           ^
    [javac] xxx.java:232: unclosed character literal
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                         ^
    [javac] xxx.java:232: unclosed character literal
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                              ^
    [javac] xxx.java:232: illegal escape character
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                                                  ^
    [javac] xxx.java:232: unclosed character literal
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                                                ^
    [javac] xxx.java:232: unclosed character literal
    [javac]  if ((data[0] == '\xFF') && (data[1] == '\xFE')) {
    [javac]                                                     ^
    [javac] xxx.java:237: ')' expected
    [javac]     }
    [javac]     ^

I tried changing it to 'and' but didnt help.

 
sorry, my bad :

if ((data[0] == 0xFF) && (data[1] == 0xFE) && (data[1] == 0x3C)) {

(if these are the true byte values you wish)

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks for that.
actually I had to change my condition a bit since the data coming back in java is different then I had in other languages.
so it should be :
if ((data[0] == -1) && (data[1] == -2))
to check if a string is utf16.

 
Perhaps you should do :

if ((data[0] == ((byte)-1)) && (data[1] == ((byte)-2)))

because "-1" and "-2" are actually ints - bearing in mind that an int is 32 bits, and a byte is 8 bits - so a byte's int range is only from -127 to +127 ...

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks.
I have a slightly different problem now. I need to check the String and see if its first 2 characters match something.

eg.
Code:
if (xml_in.StartsWith(0xFF + 0xFE)) {
   xml_in = new String(xml_in, "utf-16");
} else {
   xml_in = new String(xml_in);
}

any ideas how I could do this? (I may also need a way to check if they match -1 and -2 bytevalues depending on whether FF and FE works or not)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top