<?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%2FFile_Input_Output%2FPipedInputStream</id>
		<title>Java/File Input Output/PipedInputStream - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FPipedInputStream"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/PipedInputStream&amp;action=history"/>
		<updated>2026-04-21T19:36:02Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/File_Input_Output/PipedInputStream&amp;diff=6221&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/PipedInputStream&amp;diff=6221&amp;oldid=prev"/>
				<updated>2010-06-01T06:04:31Z</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;Версия 06:04, 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/File_Input_Output/PipedInputStream&amp;diff=6220&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/PipedInputStream&amp;diff=6220&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:43Z</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;== Defines utility routines that use Java serialization ==&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;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
import java.io.PipedInputStream;&lt;br /&gt;
import java.io.PipedOutputStream;&lt;br /&gt;
import java.io.Serializable;&lt;br /&gt;
/**&lt;br /&gt;
 * This class defines utility routines that use Java serialization.&lt;br /&gt;
 */&lt;br /&gt;
public class Serializer {&lt;br /&gt;
  /**&lt;br /&gt;
   * Serialize the object o (and any Serializable objects it refers to) and&lt;br /&gt;
   * store its serialized state in File f.&lt;br /&gt;
   */&lt;br /&gt;
  static void store(Serializable o, File f) throws IOException {&lt;br /&gt;
    ObjectOutputStream out = // The class for serialization&lt;br /&gt;
    new ObjectOutputStream(new FileOutputStream(f));&lt;br /&gt;
    out.writeObject(o); // This method serializes an object graph&lt;br /&gt;
    out.close();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Deserialize the contents of File f and return the resulting object&lt;br /&gt;
   */&lt;br /&gt;
  static Object load(File f) throws IOException, ClassNotFoundException {&lt;br /&gt;
    ObjectInputStream in = // The class for de-serialization&lt;br /&gt;
    new ObjectInputStream(new FileInputStream(f));&lt;br /&gt;
    return in.readObject(); // This method deserializes an object graph&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Use object serialization to make a &amp;quot;deep clone&amp;quot; of the object o. This&lt;br /&gt;
   * method serializes o and all objects it refers to, and then deserializes&lt;br /&gt;
   * that graph of objects, which means that everything is copied. This differs&lt;br /&gt;
   * from the clone() method of an object which is usually implemented to&lt;br /&gt;
   * produce a &amp;quot;shallow&amp;quot; clone that copies references to other objects, instead&lt;br /&gt;
   * of copying all referenced objects.&lt;br /&gt;
   */&lt;br /&gt;
  static Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {&lt;br /&gt;
    // Create a connected pair of &amp;quot;piped&amp;quot; streams.&lt;br /&gt;
    // We&amp;quot;ll write bytes to one, and them from the other one.&lt;br /&gt;
    final PipedOutputStream pipeout = new PipedOutputStream();&lt;br /&gt;
    PipedInputStream pipein = new PipedInputStream(pipeout);&lt;br /&gt;
    // Now define an independent thread to serialize the object and write&lt;br /&gt;
    // its bytes to the PipedOutputStream&lt;br /&gt;
    Thread writer = new Thread() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        ObjectOutputStream out = null;&lt;br /&gt;
        try {&lt;br /&gt;
          out = new ObjectOutputStream(pipeout);&lt;br /&gt;
          out.writeObject(o);&lt;br /&gt;
        } catch (IOException e) {&lt;br /&gt;
        } finally {&lt;br /&gt;
          try {&lt;br /&gt;
            out.close();&lt;br /&gt;
          } catch (Exception e) {&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    writer.start(); // Make the thread start serializing and writing&lt;br /&gt;
    // Meanwhile, in this thread, read and deserialize from the piped&lt;br /&gt;
    // input stream. The resulting object is a deep clone of the original.&lt;br /&gt;
    ObjectInputStream in = new ObjectInputStream(pipein);&lt;br /&gt;
    return in.readObject();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This is a simple serializable data structure that we use below for testing&lt;br /&gt;
   * the methods above&lt;br /&gt;
   */&lt;br /&gt;
  public static class DataStructure implements Serializable {&lt;br /&gt;
    String message;&lt;br /&gt;
    int[] data;&lt;br /&gt;
    DataStructure other;&lt;br /&gt;
    public String toString() {&lt;br /&gt;
      String s = message;&lt;br /&gt;
      for (int i = 0; i &amp;lt; data.length; i++)&lt;br /&gt;
        s += &amp;quot; &amp;quot; + data[i];&lt;br /&gt;
      if (other != null)&lt;br /&gt;
        s += &amp;quot;\n\t&amp;quot; + other.toString();&lt;br /&gt;
      return s;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /** This class defines a main() method for testing */&lt;br /&gt;
  public static class Test {&lt;br /&gt;
    public static void main(String[] args) throws IOException, ClassNotFoundException {&lt;br /&gt;
      // Create a simple object graph&lt;br /&gt;
      DataStructure ds = new DataStructure();&lt;br /&gt;
      ds.message = &amp;quot;hello world&amp;quot;;&lt;br /&gt;
      ds.data = new int[] { 1, 2, 3, 4 };&lt;br /&gt;
      ds.other = new DataStructure();&lt;br /&gt;
      ds.other.message = &amp;quot;nested structure&amp;quot;;&lt;br /&gt;
      ds.other.data = new int[] { 9, 8, 7 };&lt;br /&gt;
      // Display the original object graph&lt;br /&gt;
      System.out.println(&amp;quot;Original data structure: &amp;quot; + ds);&lt;br /&gt;
      // Output it to a file&lt;br /&gt;
      File f = new File(&amp;quot;datastructure.ser&amp;quot;);&lt;br /&gt;
      System.out.println(&amp;quot;Storing to a file...&amp;quot;);&lt;br /&gt;
      Serializer.store(ds, f);&lt;br /&gt;
      // Read it back from the file, and display it again&lt;br /&gt;
      ds = (DataStructure) Serializer.load(f);&lt;br /&gt;
      System.out.println(&amp;quot;Read from the file: &amp;quot; + ds);&lt;br /&gt;
      // Create a deep clone and display that. After making the copy&lt;br /&gt;
      // modify the original to prove that the clone is &amp;quot;deep&amp;quot;.&lt;br /&gt;
      DataStructure ds2 = (DataStructure) Serializer.deepclone(ds);&lt;br /&gt;
      ds.other.message = null;&lt;br /&gt;
      ds.other.data = null; // Change original&lt;br /&gt;
      System.out.println(&amp;quot;Deep clone: &amp;quot; + ds2);&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;
== Test PipedInputStream and PipedOutputStream with Thread ==&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;
&lt;br /&gt;
import java.io.BufferedInputStream;&lt;br /&gt;
import java.io.BufferedOutputStream;&lt;br /&gt;
import java.io.DataInputStream;&lt;br /&gt;
import java.io.DataOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.io.PipedInputStream;&lt;br /&gt;
import java.io.PipedOutputStream;&lt;br /&gt;
public class TestPipes {&lt;br /&gt;
  public static void writeData(OutputStream os) {&lt;br /&gt;
    try {&lt;br /&gt;
      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os));&lt;br /&gt;
      int[] numArray = { 1, 2, 3, 4, 5 };&lt;br /&gt;
      for (int i = 0; i &amp;lt; numArray.length; i++) {&lt;br /&gt;
        out.writeInt(numArray[i]);&lt;br /&gt;
      }&lt;br /&gt;
      out.flush();&lt;br /&gt;
      out.close();&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void readData(InputStream is) {&lt;br /&gt;
    DataInputStream in = new DataInputStream(new BufferedInputStream(is));&lt;br /&gt;
    boolean eof = false;&lt;br /&gt;
    try {&lt;br /&gt;
      while (!eof) {&lt;br /&gt;
          int iValue = in.readInt();&lt;br /&gt;
          System.out.println(&amp;quot;read value = &amp;quot; + iValue);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;End of Data&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    final PipedOutputStream pos = new PipedOutputStream();&lt;br /&gt;
    final PipedInputStream pis = new PipedInputStream(pos);&lt;br /&gt;
    Runnable runOutput = new Runnable() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        writeData(pos);&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    Thread outThread = new Thread(runOutput, &amp;quot;outThread&amp;quot;);&lt;br /&gt;
    outThread.start();&lt;br /&gt;
    Runnable runInput = new Runnable() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        readData(pis);&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    Thread inThread = new Thread(runInput, &amp;quot;inThread&amp;quot;);&lt;br /&gt;
    inThread.start();&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;/div&gt;</summary>
			</entry>

	</feed>