Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: byte stream vs char stream buffer Date: Sun, 11 May 2014 12:10:54 -0700 Organization: A noiseless patient Spider Lines: 64 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 11 May 2014 19:11:00 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="15212"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KwGxnKazxO3yBvV4pal7X1WFVaFndAIA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: Cancel-Lock: sha1:gLmcIU+ZDxjPe38d9VkOJQxzNi8= Xref: csiph.com comp.lang.java.programmer:28437 I think this might be the original code that I used to speed up my project. It's been a while so I don't know exactly how well this is working right now. You can wrap this in whatever you'd like -- BufferedRead for example -- since I just make a plain InputStream here. I also notice that there's not even a close method on this class, so obviously I didn't complete it. import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; /** * Copyright 2013 Brenden Towey DONATED TO THE PUBLIC DOMAIN. * * PROVIDED AS-IS WITH NO WARRANTY. FURTHER TESTING IS * REQUIRED TO COMPLETE THIS CODE. CAVEAT EMPTOR. * * @author Brenden Towey */ public class FileByteBufferInputStream extends InputStream { private ByteBuffer buffer = ByteBuffer.allocateDirect( 64 * 1024 ); private FileInputStream file; int read; boolean eof = false; public FileByteBufferInputStream( FileInputStream file ) throws IOException { this.file = file; read = file.getChannel().read( buffer ); if( read == -1 ) eof = true; buffer.rewind(); } @Override public int read() throws IOException { if( eof ) return -1; if( read-- == 0 ) { buffer.clear(); read = file.getChannel().read( buffer ); if( read == -1 ) eof = true; buffer.rewind(); if( eof ) { return -1; } else { int retval = buffer.get(); assert( read > 0 ); read--; return retval; } } else{ int retval = buffer.get(); return retval; } } }