<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FPipedOutputStream</id>
		<title>Java/File Input Output/PipedOutputStream - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FPipedOutputStream"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/File_Input_Output/PipedOutputStream&amp;action=history"/>
		<updated>2026-04-21T20:09:45Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/File_Input_Output/PipedOutputStream&amp;diff=6219&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/File_Input_Output/PipedOutputStream&amp;diff=6219&amp;oldid=prev"/>
				<updated>2010-06-01T06:04:30Z</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://www.jexp.ru/index.php?title=Java/File_Input_Output/PipedOutputStream&amp;diff=6218&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/File_Input_Output/PipedOutputStream&amp;diff=6218&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;== Implements a growable array of ints, and knows how to serialize itself as efficiently as a non-growable array ==&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;
 * A simple class that implements a growable array of ints, and knows how to&lt;br /&gt;
 * serialize itself as efficiently as a non-growable array.&lt;br /&gt;
 */&lt;br /&gt;
public class SerialIntList implements Serializable {&lt;br /&gt;
  // These are the fields of this class. By default the serialization&lt;br /&gt;
  // mechanism would just write them out. But we&amp;quot;ve declared size to be&lt;br /&gt;
  // transient, which means it will not be serialized. And we&amp;quot;ve&lt;br /&gt;
  // provided writeObject() and readObject() methods below to customize&lt;br /&gt;
  // the serialization process.&lt;br /&gt;
  protected int[] data = new int[8]; // An array to store the numbers.&lt;br /&gt;
  protected transient int size = 0; // Index of next unused element of array&lt;br /&gt;
  /** Return an element of the array */&lt;br /&gt;
  public int get(int index) {&lt;br /&gt;
    if (index &amp;gt;= size)&lt;br /&gt;
      throw new ArrayIndexOutOfBoundsException(index);&lt;br /&gt;
    else&lt;br /&gt;
      return data[index];&lt;br /&gt;
  }&lt;br /&gt;
  /** Add an int to the array, growing the array if necessary */&lt;br /&gt;
  public void add(int x) {&lt;br /&gt;
    if (data.length == size)&lt;br /&gt;
      resize(data.length * 2); // Grow array if needed.&lt;br /&gt;
    data[size++] = x; // Store the int in it.&lt;br /&gt;
  }&lt;br /&gt;
  /** An internal method to change the allocated size of the array */&lt;br /&gt;
  protected void resize(int newsize) {&lt;br /&gt;
    int[] newdata = new int[newsize]; // Create a new array&lt;br /&gt;
    System.arraycopy(data, 0, newdata, 0, size); // Copy array elements.&lt;br /&gt;
    data = newdata; // Replace old array&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Get rid of unused array elements before serializing the array. This may&lt;br /&gt;
   * reduce the number of array elements to serialize. It also makes data.length ==&lt;br /&gt;
   * size, so there is no need to safe the (transient) size field. The&lt;br /&gt;
   * serialization mechanism will automatically call this method when&lt;br /&gt;
   * serializing an object of this class. Note that this must be declared&lt;br /&gt;
   * private.&lt;br /&gt;
   */&lt;br /&gt;
  private void writeObject(ObjectOutputStream out) throws IOException {&lt;br /&gt;
    if (data.length &amp;gt; size)&lt;br /&gt;
      resize(size); // Compact the array.&lt;br /&gt;
    out.defaultWriteObject(); // Then write it out normally.&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Restore the transient size field after deserializing the array. The&lt;br /&gt;
   * serialization mechanism automatically calls this method.&lt;br /&gt;
   */&lt;br /&gt;
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {&lt;br /&gt;
    in.defaultReadObject(); // Read the array normally.&lt;br /&gt;
    size = data.length; // Restore the transient field.&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Does this object contain the same values as the object o? We override this&lt;br /&gt;
   * Object method so we can test the class.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean equals(Object o) {&lt;br /&gt;
    if (!(o instanceof SerialIntList))&lt;br /&gt;
      return false;&lt;br /&gt;
    SerialIntList that = (SerialIntList) o;&lt;br /&gt;
    if (this.size != that.size)&lt;br /&gt;
      return false;&lt;br /&gt;
    for (int i = 0; i &amp;lt; this.size; i++)&lt;br /&gt;
      if (this.data[i] != that.data[i])&lt;br /&gt;
        return false;&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
  /** We must override this method when we override equals(). */&lt;br /&gt;
  public int hashCode() {&lt;br /&gt;
    int code = 1; // non-zero to hash [0] and [] to distinct values&lt;br /&gt;
    for (int i = 0; i &amp;lt; size; i++)&lt;br /&gt;
      code = code * 997 + data[i]; // ignore overflow&lt;br /&gt;
    return code;&lt;br /&gt;
  }&lt;br /&gt;
  /** A main() method to prove that it works */&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    SerialIntList list = new SerialIntList();&lt;br /&gt;
    for (int i = 0; i &amp;lt; 100; i++)&lt;br /&gt;
      list.add((int) (Math.random() * 40000));&lt;br /&gt;
    SerialIntList copy = (SerialIntList) Serializer.deepclone(list);&lt;br /&gt;
    if (list.equals(copy))&lt;br /&gt;
      System.out.println(&amp;quot;equal copies&amp;quot;);&lt;br /&gt;
    Serializer.store(list, new File(&amp;quot;intlist.ser&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
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;
== Ring the bell using AWT ==&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;
// Ring the bell!&lt;br /&gt;
public class Beep {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    // In terminal-based applications, this is a non-portable, unreliable&lt;br /&gt;
    // way to sound the terminal bell (if there is one) and get the&lt;br /&gt;
    // user&amp;quot;s attention. \u0007 is the ASCII BEL or Ctrl-G character.&lt;br /&gt;
    System.out.println(&amp;quot;BEEP\u0007!&amp;quot;);&lt;br /&gt;
    // For applications that can use AWT, there is another way&lt;br /&gt;
    // to ring the bell.&lt;br /&gt;
    String[] words = new String[] { &amp;quot;Shave &amp;quot;, &amp;quot;and &amp;quot;, &amp;quot;a &amp;quot;, &amp;quot;hair&amp;quot;, &amp;quot;cut &amp;quot;, &amp;quot;two &amp;quot;, &amp;quot;bits.&amp;quot; };&lt;br /&gt;
    int[] pauses = new int[] { 300, 150, 150, 250, 450, 250, 1 };&lt;br /&gt;
    for (int i = 0; i &amp;lt; pauses.length; i++) {&lt;br /&gt;
      // Ring the bell using AWT&lt;br /&gt;
      java.awt.Toolkit.getDefaultToolkit().beep();&lt;br /&gt;
      System.out.print(words[i]);&lt;br /&gt;
      System.out.flush();&lt;br /&gt;
      // Wait a while before beeping again.&lt;br /&gt;
      try {&lt;br /&gt;
        Thread.sleep(pauses[i]);&lt;br /&gt;
      } catch (InterruptedException e) {&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println();&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>