• 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

SequenceInputStream performance

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using a SequenceInputStream to read multiple files and merge them together into one file. This is way slow. I'm guessing due to buffering. How do I properly buffer with a SequenceInputStream. My code is below. Note that I'm buffering output, but not input. I tried using s.read(byte[] b, int off, int len) but my output was wacked, I apparantly did not use it properly.

FileOutputStream out = new FileOutputStream(strMergedFilePathAndName);
BufferedOutputStream dest = new BufferedOutputStream(out, m_intBufferSize);

while ((c = s.read()) != -1)
{
out.write(c);
}
s.close();
out.close();
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This snippet is working well.

Try no to use any buffering at all for speed. I think the OS is is already doing buffering for us.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the best way to get this DataOutputStream into a File object?

Thanks.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much for pointing out the importance of using a Buffer. The code I used below was so very slow.
String wavFile1 = "C:\\1.mp3";
String wavFile2 = "C:\\2.mp3";
FileInputStream fistream1 = new FileInputStream(wavFile1);  // first source file
FileInputStream fistream2 = new FileInputStream(wavFile2);//second source file
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream("D://merge1.mp3");//destinationfile

int temp;

while( ( temp = sistream.read() ) != -1)
{
   // System.out.print( (char) temp ); // to print at DOS prompt
   fostream.write(temp);   // to write to file
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();


so tried the buffer and it improved 100%
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope you have also learnt how difficult it is to use methods like read() returning int.
If try with resources was introduced 12½ years ago, why do people still call close() methods?
 
reply
    Bookmark Topic Watch Topic
  • New Topic