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: Fri, 16 May 2014 17:36:31 -0700 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 17 May 2014 00:36:37 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="11251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nPmPMDGbbLGp9+RRhSnJ6bq/MgJ7Ifuw=" 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:9gRNxftGlnMT0DTxv81rv1nBAso= Xref: csiph.com comp.lang.java.programmer:28502 On 5/16/2014 4:55 PM, Roedy Green wrote: > // ? > ByteBuffer b2 = bb.slice(); Yes, this bit of code was an amalgam of other bits that I had saved to remind myself how to perform some common operations on ByteBuffers. The longer sequence went something like: readToCapacity( fc, bb ); bb.flip(); ByteBuffer b2 = bb.slice(); b2.limit( 100 ); b2.asReadOnlyBuffer(); readHeader( b2 ); readFile( bb ); And it was just a way to be able to isolate the (100 byte) header from the rest of the stream, as well as be able to retain all original bytes without a potentially expensive seek operation. Mostly just as a reminder how to slice up a ByteBuffer into smaller buffers so they can be read independently. > // request the next chunk of data > bb.flip(); > BTW, I'd make that comment "reset limit and position for reading" rather than "next chuck" (the next chunk would probably be clear(), because if the buffer isn't full you'd run into problems with limit using flip()). Flip() is used to switch from put()s to get()s. See the docs: http://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html#flip()