Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36501

Re: BufferedImage and int[] array

Newsgroups comp.lang.java.programmer
Date 2017-04-21 10:46 -0700
References <5a1d0bab-5ae7-45a1-9c8f-344183ed48ba@googlegroups.com> <4f6d8eef-73e3-45f6-94fc-4ccb530ddd8c@googlegroups.com> <0f806bdc-aa93-4897-bb9f-8373ffd16175@googlegroups.com> <903d6ec5-ab8e-4e87-9581-13ff803bf461@googlegroups.com>
Message-ID <bd0382cd-8a6b-4cde-8b5d-a213645bb2c4@googlegroups.com> (permalink)
Subject Re: BufferedImage and int[] array
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Friday, April 21, 2017 at 1:41:28 PM UTC-4, Eric Douglas wrote:
>           final byte[] contents;
>           InputStream myFileInputStream;
>           File myImgFile = new File("c:/image.png");
>           myFileInputStream = new FileInputStream(myImgFile);
>           contents = new byte[myFileInputStream.available()];
>           myFileInputStream.read(contents);
>           myFileInputStream.close();

While using .available() to read the entire stream often just works on local files, or files embedded in the jar, there is a more reliable stream method.

     public static byte[] getStreamBytes(final InputStream is) throws IOException {
          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
          int bytesRead = 0;
          while (bytesRead > -1) {
               bytesRead = Math.max(is.available(), 1);
               final byte[] buf = new byte[bytesRead];
               bytesRead = is.read(buf);
               if (bytesRead > 0) {
                    baos.write(buf, 0, bytesRead);
               }
          }
          baos.flush();
          baos.close();
          return baos.toByteArray();
     }

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 08:28 -0700
  Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 08:54 -0700
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 09:03 -0700
      Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 10:41 -0700
        Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 10:46 -0700
        Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 11:13 -0700
          Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 12:12 -0700
          Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:24 -0700
            Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:37 -0700
              Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:40 -0700
              Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:54 -0700
                Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:59 -0700
              Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:58 -0700
                Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 14:06 -0700
  Re: BufferedImage and int[] array Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-22 11:28 -0500
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-24 07:33 -0700
      Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-24 08:04 -0700
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-24 08:17 -0700
      Re: BufferedImage and int[] array Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-24 11:25 -0500

csiph-web