<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FMappedByteBuffer</id>
		<title>Java Tutorial/File/MappedByteBuffer - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FMappedByteBuffer"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/MappedByteBuffer&amp;action=history"/>
		<updated>2026-04-11T07:57:51Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/MappedByteBuffer&amp;diff=5366&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/MappedByteBuffer&amp;diff=5366&amp;oldid=prev"/>
				<updated>2010-06-01T05:19:54Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:19, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/MappedByteBuffer&amp;diff=5365&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/MappedByteBuffer&amp;diff=5365&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Create MappedByteBuffer from FileInputStream ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  private static final int LENGTH = 100; // Small&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    MappedByteBuffer in = new FileInputStream(&amp;quot;test.txt&amp;quot;).getChannel().map(&lt;br /&gt;
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    while (i &amp;lt; LENGTH)&lt;br /&gt;
      System.out.print((char) in.get(i++));&lt;br /&gt;
    System.out.println((char) in.get(i++));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create Read only buffer ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  private static final int LENGTH = 100; // Small&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    MappedByteBuffer in = new FileInputStream(&amp;quot;test.txt&amp;quot;).getChannel().map(&lt;br /&gt;
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    while (i &amp;lt; LENGTH)&lt;br /&gt;
      System.out.print((char) in.get(i++));&lt;br /&gt;
    System.out.println((char) in.get(i++));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Creating a very large file using mapping ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.RandomAccessFile;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  static int length = 0x8FFFFFF; // 128 Mb&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    MappedByteBuffer out = new RandomAccessFile(&amp;quot;test.dat&amp;quot;, &amp;quot;rw&amp;quot;).getChannel().map(&lt;br /&gt;
        FileChannel.MapMode.READ_WRITE, 0, length);&lt;br /&gt;
    for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
      out.put((byte) &amp;quot;x&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Finished writing&amp;quot;);&lt;br /&gt;
    for (int i = length / 2; i &amp;lt; length / 2 + 6; i++)&lt;br /&gt;
      System.out.print((char) out.get(i));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get MappedByteBuffer from FileChannel ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.RandomAccessFile;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    RandomAccessFile raf = new RandomAccessFile(&amp;quot;test.txt&amp;quot;, &amp;quot;r&amp;quot;);&lt;br /&gt;
    FileChannel fc = raf.getChannel();&lt;br /&gt;
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());&lt;br /&gt;
    buffer.clear();&lt;br /&gt;
    buffer.flip();&lt;br /&gt;
    System.out.println(&amp;quot;hasArray=&amp;quot; + buffer.hasArray());&lt;br /&gt;
    System.out.println(buffer.toString());&lt;br /&gt;
    System.out.flush();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Locking portions of a mapped file ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
// From &amp;quot;Thinking in Java, 3rd ed.&amp;quot; (c) Bruce Eckel 2002&lt;br /&gt;
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.&lt;br /&gt;
// Locking portions of a mapped file.&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.RandomAccessFile;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
import java.nio.channels.FileLock;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  static final int LENGTH = 0x8FFFFFF; // 128 Mb&lt;br /&gt;
  static FileChannel fc;&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    fc = new RandomAccessFile(&amp;quot;test.dat&amp;quot;, &amp;quot;rw&amp;quot;).getChannel();&lt;br /&gt;
    MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);&lt;br /&gt;
    for (int i = 0; i &amp;lt; LENGTH; i++)&lt;br /&gt;
      out.put((byte) &amp;quot;x&amp;quot;);&lt;br /&gt;
    new LockAndModify(out, 0, 0 + LENGTH / 3);&lt;br /&gt;
    new LockAndModify(out, LENGTH / 2, LENGTH / 2 + LENGTH / 4);&lt;br /&gt;
  }&lt;br /&gt;
  private static class LockAndModify extends Thread {&lt;br /&gt;
    private ByteBuffer buff;&lt;br /&gt;
    private int start, end;&lt;br /&gt;
    LockAndModify(ByteBuffer mbb, int start, int end) {&lt;br /&gt;
      this.start = start;&lt;br /&gt;
      this.end = end;&lt;br /&gt;
      mbb.limit(end);&lt;br /&gt;
      mbb.position(start);&lt;br /&gt;
      buff = mbb.slice();&lt;br /&gt;
      start();&lt;br /&gt;
    }&lt;br /&gt;
    public void run() {&lt;br /&gt;
      try {&lt;br /&gt;
        FileLock fl = fc.lock(start, end, false);&lt;br /&gt;
        System.out.println(&amp;quot;Locked: &amp;quot; + start + &amp;quot; to &amp;quot; + end);&lt;br /&gt;
        while (buff.position() &amp;lt; buff.limit() - 1){&lt;br /&gt;
          buff.put((byte) (buff.get() + 1));&lt;br /&gt;
        }&lt;br /&gt;
        fl.release();&lt;br /&gt;
        System.out.println(&amp;quot;Released: &amp;quot; + start + &amp;quot; to &amp;quot; + end);&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
        throw new RuntimeException(e);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Mapping an entire file into memory for reading ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    long length = new File(&amp;quot;test.txt&amp;quot;).length();&lt;br /&gt;
    MappedByteBuffer in = new FileInputStream(&amp;quot;test.txt&amp;quot;).getChannel().map(&lt;br /&gt;
        FileChannel.MapMode.READ_ONLY, 0, length);&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    while (i &amp;lt; length)&lt;br /&gt;
      System.out.print((char) in.get(i++));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Read file upside/down with RandomAccessFile ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.RandomAccessFile;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    RandomAccessFile f = new RandomAccessFile(&amp;quot;hello.txt&amp;quot;, &amp;quot;rw&amp;quot;);&lt;br /&gt;
    FileChannel fc = f.getChannel();&lt;br /&gt;
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, f.length());&lt;br /&gt;
    int len = (int) f.length();&lt;br /&gt;
    for (int i = 0, j = len - 1; i &amp;lt; j; i++, j--) {&lt;br /&gt;
      byte b = mbb.get(i);&lt;br /&gt;
      mbb.put(i, mbb.get(j));&lt;br /&gt;
      mbb.put(j, b);&lt;br /&gt;
    }&lt;br /&gt;
    fc.close();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  What happens when the entire file isn&amp;quot;t in your mapping region? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  private static final int LENGTH = 100; // Small&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    MappedByteBuffer in = new FileInputStream(&amp;quot;test.txt&amp;quot;).getChannel().map(&lt;br /&gt;
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    while (i &amp;lt; LENGTH)&lt;br /&gt;
      System.out.print((char) in.get(i++));&lt;br /&gt;
    System.out.println((char) in.get(i++));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>