By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Converting String<->byte[] -- No Data Loss?

P: n/a
If I have a byte[] and I convert it to string (String sData = new
String(byte[] bData), then convert it back (byte bData = sData.getBytes()),
will all data be intact, or do Strings have problems with bytes that are
not printable characters? I've tested this and it seems to work fine, but
I want to make sure there isn't some condition or situation I'm not aware
of that could cause problems.

I'm doing this because it's easier to do some of my work with strings, so
I'm reading a Zip file, saving each entry as a String, modifying some, then
saving it as a new Zip. I've found with Zip files, you can't always be
sure of how much data you'll be able to read in, so I'm reading in data in
blocks upto 2k, then taking each block and converting it to a String, so I
can concatenate the amount read in onto the end of another string. (With
Strings, I can use sString.substring(), I can't find a way to concatenate
byte[] arrays without continually creating a new array and using
System.arrayCopy().)

Thanks for any info.

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hal Vaughan wrote:
If I have a byte[] and I convert it to string (String sData = new
String(byte[] bData), then convert it back (byte bData =
sData.getBytes()), will all data be intact, or do Strings have
problems with bytes that are
not printable characters? I've tested this and it seems to work
fine, but I want to make sure there isn't some condition or
situation I'm not aware of that could cause problems.

I'm doing this because it's easier to do some of my work with
strings, so I'm reading a Zip file, saving each entry as a String,
modifying some, then
saving it as a new Zip. I've found with Zip files, you can't always
be sure of how much data you'll be able to read in, so I'm reading
in data in blocks upto 2k, then taking each block and converting it
to a String, so I
can concatenate the amount read in onto the end of another string.
(With Strings, I can use sString.substring(), I can't find a way to
concatenate byte[] arrays without continually creating a new array
and using System.arrayCopy().)

Thanks for any info.

Hal


Hello,
Definitely don't do this. It is possible to lose data. Instead, what
you want to do, probably, is use a ByteArrayOutputStream. Basically,
what you do, is something like this:

<Given that 'is' is an input stream reading from the ZIP entry>

ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int i;
while ((i = is.read(buffer)) != -1) {
bout.write(buffer, 0, i);
}

byte[] entireEntry = bout.toByteArray();

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/ssYTwxczzJRavJYRArPIAKCzew0OfM78PC9ZSkBeFDHQdsJ2Rg CeMLWy
1Lt6/L3PzZc/i+BMQnlAAuw=
=48iU
-----END PGP SIGNATURE-----
Jul 17 '05 #2

P: n/a
Chris wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hal Vaughan wrote:
If I have a byte[] and I convert it to string (String sData = new
String(byte[] bData), then convert it back (byte bData =
sData.getBytes()), will all data be intact, or do Strings have
problems with bytes that are
not printable characters? I've tested this and it seems to work
fine, but I want to make sure there isn't some condition or
situation I'm not aware of that could cause problems.

I'm doing this because it's easier to do some of my work with
strings, so I'm reading a Zip file, saving each entry as a String,
modifying some, then
saving it as a new Zip. I've found with Zip files, you can't always
be sure of how much data you'll be able to read in, so I'm reading
in data in blocks upto 2k, then taking each block and converting it
to a String, so I
can concatenate the amount read in onto the end of another string.
(With Strings, I can use sString.substring(), I can't find a way to
concatenate byte[] arrays without continually creating a new array
and using System.arrayCopy().)

Thanks for any info.

Hal
Hello,
Definitely don't do this. It is possible to lose data. Instead, what
you want to do, probably, is use a ByteArrayOutputStream. Basically,
what you do, is something like this:

<Given that 'is' is an input stream reading from the ZIP entry>

ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int i;
while ((i = is.read(buffer)) != -1) {
bout.write(buffer, 0, i);
}

byte[] entireEntry = bout.toByteArray();


I've never used (or heard of) ByteArrayOutputStream. I looked it up. Am I
correct to say that what you're doing is writing to bout and that you can
keep writing to bout and it basically just keeps filling up until you're
done, then you can move the entire amount into a new byte array?

Thanks!

Hal
- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/ssYTwxczzJRavJYRArPIAKCzew0OfM78PC9ZSkBeFDHQdsJ2Rg CeMLWy
1Lt6/L3PzZc/i+BMQnlAAuw=
=48iU
-----END PGP SIGNATURE-----


Jul 17 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hal Vaughan wrote:
I've never used (or heard of) ByteArrayOutputStream. I looked it
up. Am I correct to say that what you're doing is writing to bout
and that you can keep writing to bout and it basically just keeps
filling up until you're done, then you can move the entire amount
into a new byte array?

Thanks!

Hal


Bingo. It gets as big as you need. I'm pretty sure you can even call
toByteArray(), use the results, and then (or even at the same time)
add MORE stuff to it and call toByteArray() again, returning
everything. It's also very useful if you need to generate some kind
of binary format and turn it into a byte array: you can just wrap it
with a DataOutputStream and use the methods provided there (if you
need to do this but you know the output size, I think that using
java.nio.ByteBuffer and wrap()ing an array is probably faster but, as
I said, it requires that you know the size beforehand).

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/tV/ywxczzJRavJYRAlliAJ0WeuwD2guxLYK9jranjRpX5dMexACgh unN
fQNn8AjhWxsUWo7e7PfkhfI=
=0oqd
-----END PGP SIGNATURE-----
Jul 17 '05 #4

P: n/a
Chris wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hal Vaughan wrote:
I've never used (or heard of) ByteArrayOutputStream. I looked it
up. Am I correct to say that what you're doing is writing to bout
and that you can keep writing to bout and it basically just keeps
filling up until you're done, then you can move the entire amount
into a new byte array?

Thanks!

Hal
Bingo. It gets as big as you need. I'm pretty sure you can even call
toByteArray(), use the results, and then (or even at the same time)
add MORE stuff to it and call toByteArray() again, returning
everything. It's also very useful if you need to generate some kind
of binary format and turn it into a byte array: you can just wrap it
with a DataOutputStream and use the methods provided there (if you
need to do this but you know the output size, I think that using
java.nio.ByteBuffer and wrap()ing an array is probably faster but, as
I said, it requires that you know the size beforehand).


When I first started learning Java (I'm entirely self taught), I found IO
streams frustrating, but now that I see all the things you can do -- and
how easy it is to wrap one stream around another to do some complex stuff
fairly easily, they make a lot more sense. It just takes time to get used
to them

Thanks!

Hal
- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/tV/ywxczzJRavJYRAlliAJ0WeuwD2guxLYK9jranjRpX5dMexACgh unN
fQNn8AjhWxsUWo7e7PfkhfI=
=0oqd
-----END PGP SIGNATURE-----


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.