• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to split byte[] to smaller chunks

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i have a big file.i have loaded this file to byte[].

now i want to split this byte[] to smaller byte[] chunks of byte[500].

how to do this ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no way except for the direct way -- in a "for" loop, allocate 500-byte arrays, and use System.arraycopy() to copy data from the big array into them.
 
Daniel Bauer
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void splitBytes(byte[] fileBytes) throws Exception {


int offset = 0;
int fileCounter = 0;

while (offset < fileBytes.length) {
byte[] outputBytes;

if(fileBytes.length - offset < 500 ) {
outputBytes = new byte[fileBytes.length - offset];
System.arraycopy(fileBytes, offset, outputBytes, 0, fileBytes.length - offset);
saveFile(outputBytes , fileCounter++);
break;
}

outputBytes = new byte[500];
System.arraycopy(fileBytes, offset, outputBytes, 0, 500);
offset +=500 ;
saveFile(outputBytes , fileCounter++);
Thread.sleep(3000);

}

}
 
Daniel Bauer
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or with a recursion

public static void splitBytes(byte[] fileBytes, int offset , int fileCounter) throws Exception {

byte[] outputBytes;

if(fileBytes.length - offset < 500 ) {
outputBytes = new byte[fileBytes.length - offset];
System.arraycopy(fileBytes, offset, outputBytes, 0, fileBytes.length - offset);
saveFile(outputBytes , fileCounter++);
return;
}

outputBytes = new byte[500];
System.arraycopy(fileBytes, offset, outputBytes, 0, 500);
saveFile(outputBytes , fileCounter++);
Thread.sleep(3000);
splitBytes(fileBytes,offset+500, fileCounter++);


}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic