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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

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

    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.substri ng(), I can't find a way to concatenate
    byte[] arrays without continually creating a new array and using
    System.arrayCop y().)

    Thanks for any info.

    Hal
  • Chris

    #2
    Re: Converting String&lt;-&gt;byte[] -- No Data Loss?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hal Vaughan wrote:
    [color=blue]
    > 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.substri ng(), I can't find a way to
    > concatenate byte[] arrays without continually creating a new array
    > and using System.arrayCop y().)
    >
    > Thanks for any info.
    >
    > Hal[/color]

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

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

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

    byte[] entireEntry = bout.toByteArra y();

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

    iD8DBQE/ssYTwxczzJRavJY RArPIAKCzew0OfM 78PC9ZSkBeFDHQd sJ2RgCeMLWy
    1Lt6/L3PzZc/i+BMQnlAAuw=
    =48iU
    -----END PGP SIGNATURE-----

    Comment

    • Hal Vaughan

      #3
      Re: Converting String&lt;-&gt;byte[] -- No Data Loss?

      Chris wrote:
      [color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Hal Vaughan wrote:
      >[color=green]
      >> 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.substri ng(), I can't find a way to
      >> concatenate byte[] arrays without continually creating a new array
      >> and using System.arrayCop y().)
      >>
      >> Thanks for any info.
      >>
      >> Hal[/color]
      >
      > Hello,
      > Definitely don't do this. It is possible to lose data. Instead, what
      > you want to do, probably, is use a ByteArrayOutput Stream. Basically,
      > what you do, is something like this:
      >
      > <Given that 'is' is an input stream reading from the ZIP entry>
      >
      > ByteArrayOutput Stream bout = new ByteArrayOutput Stream();
      > byte[] buffer = new byte[2048];
      > int i;
      > while ((i = is.read(buffer) ) != -1) {
      > bout.write(buff er, 0, i);
      > }
      >
      > byte[] entireEntry = bout.toByteArra y();[/color]

      I've never used (or heard of) ByteArrayOutput Stream. 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
      [color=blue]
      > - --
      > Chris
      > -----BEGIN PGP SIGNATURE-----
      > Version: GnuPG v1.2.2 (GNU/Linux)
      >
      > iD8DBQE/ssYTwxczzJRavJY RArPIAKCzew0OfM 78PC9ZSkBeFDHQd sJ2RgCeMLWy
      > 1Lt6/L3PzZc/i+BMQnlAAuw=
      > =48iU
      > -----END PGP SIGNATURE-----[/color]

      Comment

      • Chris

        #4
        Re: Converting String&lt;-&gt;byte[] -- No Data Loss?

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Hal Vaughan wrote:
        [color=blue]
        > I've never used (or heard of) ByteArrayOutput Stream. 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[/color]

        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 DataOutputStrea m and use the methods provided there (if you
        need to do this but you know the output size, I think that using
        java.nio.ByteBu ffer 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/ywxczzJRavJYRAl liAJ0WeuwD2guxL YK9jranjRpX5dMe xACghunN
        fQNn8AjhWxsUWo7 e7PfkhfI=
        =0oqd
        -----END PGP SIGNATURE-----

        Comment

        • Hal Vaughan

          #5
          Re: Converting String&lt;-&gt;byte[] -- No Data Loss?

          Chris wrote:
          [color=blue]
          > -----BEGIN PGP SIGNED MESSAGE-----
          > Hash: SHA1
          >
          > Hal Vaughan wrote:
          >[color=green]
          >> I've never used (or heard of) ByteArrayOutput Stream. 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[/color]
          >
          > 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 DataOutputStrea m and use the methods provided there (if you
          > need to do this but you know the output size, I think that using
          > java.nio.ByteBu ffer and wrap()ing an array is probably faster but, as
          > I said, it requires that you know the size beforehand).[/color]

          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
          [color=blue]
          > - --
          > Chris
          > -----BEGIN PGP SIGNATURE-----
          > Version: GnuPG v1.2.2 (GNU/Linux)
          >
          > iD8DBQE/tV/ywxczzJRavJYRAl liAJ0WeuwD2guxL YK9jranjRpX5dMe xACghunN
          > fQNn8AjhWxsUWo7 e7PfkhfI=
          > =0oqd
          > -----END PGP SIGNATURE-----[/color]

          Comment

          Working...