Path: csiph.com!usenet.pasdenom.info!aioe.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: offsets in a FileChannel ... Date: Sat, 23 Feb 2013 15:39:08 +0100 Lines: 41 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net aiOaajiJkertlxxmTSR10whEolE7bO96cCULP5C3gmjjbF8F1KQy15/IVCrMa3qww= Cancel-Lock: sha1:9dF7sSsGrtwXmTPBlTSqnU2X78g= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:22466 On 23.02.2013 15:11, qwertmonkey@syberianoutpost.ru wrote: > What is missing in this code snippet to get the offsets in the underlying > FileChannel on which the MappedByteBuffer and then the CharBuffer are built? > ~ > CharBuffer.position() gives you the position alright, but how about wanting > to get the actual offset of certain characters in the actual data feed exposed > through the FileInputStream? > ~ > char c; > long lPsx; > FIS = new FileInputStream(IFl); > FileChannel FlChnl = FIS.getChannel(); > MappedByteBuffer MptbChnlBfr = FlChnl.map(FileChannel.MapMode.READ_ONLY, > 0, FlChnl.size()); > CharBuffer cBfrUTF8 = ChrStDkdr.decode(MptbChnlBfr); > // __ > while(cBfrUTF8.hasRemaining()){ > c = cBfrUTF8.get(); > lPsx = cBfrUTF8.position(); > System.err.println("// __ |" + lPsx + "|" + c + "|" + (int)c + "|"); > } > // __ > FlChnl.close(); > FIS.close(); > ~ > Or do you know of any other way to basically do the same thing? UTF8 is not an encoding with a fixed width. You would have to create more complex code if you want to align char position and byte position. Basically you need to read the file from the beginning and observe the width of every char as it is being decoded. You could of course apply heuristics if you have more knowledge about the file but I guess that soon gets messy. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/