PipedOutputStream
| 日本語 | 管化出力流れ |
| 英語 | piped output stream |
| ふりがな | ぱいぷとあうとぷっとすとりーむ、ぱいぷどあうとぷっとすとりーむ |
| フリガナ | パイプトアウトプットストリーム、パイプドアウトプットストリーム |
J2SEに含まれるクラスのひとつ。パッケージも含めたクラス名はjava.io.PipedOutputStream。
ストリームクラス。OutputStreamクラスのサブクラスであり、バイト出力ストリームクラスである。
バイト出力ストリームクラスの中では「出力先」に位置する。
対になる入力側のクラスはPipedInputStreamクラスである。
PipedInputStreamクラスを「出力先」とするクラス。
PipedInputStreamクラスへとバイトストリームを送信するために使用する。
マルチスレッドの使用時に、あるスレッドから他のスレッドへバイトストリームを送信する際に使用する。
PipedInputStreamクラスのコンストラクタにPipedOutputStreamクラスを渡すことで、この2クラスが接続される。
PipedInputStreamクラスのread()メソッドを呼び出すと、read()メソッドはバイトストリームを受け取るまで待つ。
PipedOutputStreamクラスからwrite()メソッドでint型の値を出力すると、PipedInputStreamクラスのread()メソッドから返り、その出力されたint型の値を返す。
PipedOutputStreamクラスは処理が完了したらclose()メソッドを呼び出す。すると、PipedInputStreamクラスのread()メソッドは「-1」を返すため、ここで処理を終了し、PipedInputStreamクラスのclose()メソッドを呼び出す。
このように、PipedOutputStream→PipedInputStream、という送信処理を行うために使用する。
ストリームクラス。OutputStreamクラスのサブクラスであり、バイト出力ストリームクラスである。
バイト出力ストリームクラスの中では「出力先」に位置する。
対になる入力側のクラスはPipedInputStreamクラスである。
PipedInputStreamクラスを「出力先」とするクラス。
PipedInputStreamクラスへとバイトストリームを送信するために使用する。
マルチスレッドの使用時に、あるスレッドから他のスレッドへバイトストリームを送信する際に使用する。
PipedInputStreamクラスのコンストラクタにPipedOutputStreamクラスを渡すことで、この2クラスが接続される。
PipedInputStreamクラスのread()メソッドを呼び出すと、read()メソッドはバイトストリームを受け取るまで待つ。
PipedOutputStreamクラスからwrite()メソッドでint型の値を出力すると、PipedInputStreamクラスのread()メソッドから返り、その出力されたint型の値を返す。
PipedOutputStreamクラスは処理が完了したらclose()メソッドを呼び出す。すると、PipedInputStreamクラスのread()メソッドは「-1」を返すため、ここで処理を終了し、PipedInputStreamクラスのclose()メソッドを呼び出す。
このように、PipedOutputStream→PipedInputStream、という送信処理を行うために使用する。
// Sample.java
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;
public class Sample
{
public static void main( String[] args )
{
PipedOutputStream pipedOutputStream = null;
PipedInputStream pipedInputStream = null;
try
{
// PipedOutputStreamクラスとPipedInputStreamクラスを作り、
// 片方をもう片方のコンストラクタに渡して接続します。
pipedOutputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream( pipedOutputStream );
// 入力側は別スレッドで実行します。
InputThread inputThread = new InputThread( pipedInputStream );
inputThread.start();
// こちら、出力側。
for( int iF1 = 0; iF1 < 10; ++iF1 )
{
// こちらからiF1の値を出力します。
pipedOutputStream.write( iF1 );
try
{
// このスレッドを1秒止めます。
Thread.sleep( 1 * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドを呼んでいる間に割り込みが入ると
// InterruptedException例外が投げられます。
e.printStackTrace();
}
}
System.out.println( "main() : 終了します。" );
// 0x0 ( 0 )
// 0x1 ( 1 )
// 0x2 ( 2 )
// 0x3 ( 3 )
// 0x4 ( 4 )
// 0x5 ( 5 )
// 0x6 ( 6 )
// 0x7 ( 7 )
// 0x8 ( 8 )
// 0x9 ( 9 )
// main() : 終了します。
// InputThread : 終了します。
// このように、PipedInputStreamクラスから書き込んだ
// バイト情報が、PipedInputStreamクラスで取得できる
// というわけです。
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
finally
{
try
{
if( pipedInputStream != null )
{
pipedInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( pipedOutputStream != null )
{
pipedOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}
/**
* 入力側。
*/
class InputThread extends Thread
{
/** 入力用PipedInputStreamクラス */
private PipedInputStream pipedInputStream;
/**
* コンストラクタ。
*/
public InputThread( PipedInputStream pipedInputStream )
{
this.pipedInputStream = pipedInputStream;
}
/**
* 読み取り処理を開始します。
* 別スレッドで実行されます。
*/
public void run()
{
// こちら、入力側。
try
{
while( true )
{
// 1バイト取得します。
int i = pipedInputStream.read();
if( i == -1 )
{
// -1が返されてきたら終わりです。
break;
}
// 出力します。
System.out.println( "0x" + Integer.toHexString( i ) + " ( " + i + " )" );
}
System.out.println( "InputThread : 終了します。" );
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
}
}
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;
public class Sample
{
public static void main( String[] args )
{
PipedOutputStream pipedOutputStream = null;
PipedInputStream pipedInputStream = null;
try
{
// PipedOutputStreamクラスとPipedInputStreamクラスを作り、
// 片方をもう片方のコンストラクタに渡して接続します。
pipedOutputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream( pipedOutputStream );
// 入力側は別スレッドで実行します。
InputThread inputThread = new InputThread( pipedInputStream );
inputThread.start();
// こちら、出力側。
for( int iF1 = 0; iF1 < 10; ++iF1 )
{
// こちらからiF1の値を出力します。
pipedOutputStream.write( iF1 );
try
{
// このスレッドを1秒止めます。
Thread.sleep( 1 * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドを呼んでいる間に割り込みが入ると
// InterruptedException例外が投げられます。
e.printStackTrace();
}
}
System.out.println( "main() : 終了します。" );
// 0x0 ( 0 )
// 0x1 ( 1 )
// 0x2 ( 2 )
// 0x3 ( 3 )
// 0x4 ( 4 )
// 0x5 ( 5 )
// 0x6 ( 6 )
// 0x7 ( 7 )
// 0x8 ( 8 )
// 0x9 ( 9 )
// main() : 終了します。
// InputThread : 終了します。
// このように、PipedInputStreamクラスから書き込んだ
// バイト情報が、PipedInputStreamクラスで取得できる
// というわけです。
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
finally
{
try
{
if( pipedInputStream != null )
{
pipedInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( pipedOutputStream != null )
{
pipedOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}
/**
* 入力側。
*/
class InputThread extends Thread
{
/** 入力用PipedInputStreamクラス */
private PipedInputStream pipedInputStream;
/**
* コンストラクタ。
*/
public InputThread( PipedInputStream pipedInputStream )
{
this.pipedInputStream = pipedInputStream;
}
/**
* 読み取り処理を開始します。
* 別スレッドで実行されます。
*/
public void run()
{
// こちら、入力側。
try
{
while( true )
{
// 1バイト取得します。
int i = pipedInputStream.read();
if( i == -1 )
{
// -1が返されてきたら終わりです。
break;
}
// 出力します。
System.out.println( "0x" + Integer.toHexString( i ) + " ( " + i + " )" );
}
System.out.println( "InputThread : 終了します。" );
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
}
}
// Sample.java
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;
public class Sample
{
public static void main( String[] args )
{
PipedOutputStream pipedOutputStream = null;
PipedInputStream pipedInputStream = null;
try
{
// PipedOutputStreamクラスとPipedInputStreamクラスを作り、
// 片方をもう片方のコンストラクタに渡して接続します。
pipedOutputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream( pipedOutputStream );
// 入力側は別スレッドで実行します。
InputThread inputThread = new InputThread( pipedInputStream );
inputThread.start();
// こちら、出力側。
for( int iF1 = 0; iF1 < 10; ++iF1 )
{
// こちらからiF1の値を出力します。
pipedOutputStream.write( iF1 );
try
{
// このスレッドを1秒止めます。
Thread.sleep( 1 * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドを呼んでいる間に割り込みが入ると
// InterruptedException例外が投げられます。
e.printStackTrace();
}
}
System.out.println( "main() : 終了します。" );
// 0x0 ( 0 )
// 0x1 ( 1 )
// 0x2 ( 2 )
// 0x3 ( 3 )
// 0x4 ( 4 )
// 0x5 ( 5 )
// 0x6 ( 6 )
// 0x7 ( 7 )
// 0x8 ( 8 )
// 0x9 ( 9 )
// main() : 終了します。
// InputThread : 終了します。
// このように、PipedInputStreamクラスから書き込んだ
// バイト情報が、PipedInputStreamクラスで取得できる
// というわけです。
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
finally
{
try
{
if( pipedInputStream != null )
{
pipedInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( pipedOutputStream != null )
{
pipedOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}
/**
* 入力側。
*/
class InputThread extends Thread
{
/** 入力用PipedInputStreamクラス */
private PipedInputStream pipedInputStream;
/**
* コンストラクタ。
*/
public InputThread( PipedInputStream pipedInputStream )
{
this.pipedInputStream = pipedInputStream;
}
/**
* 読み取り処理を開始します。
* 別スレッドで実行されます。
*/
public void run()
{
// こちら、入力側。
try
{
while( true )
{
// 1バイト取得します。
int i = pipedInputStream.read();
if( i == -1 )
{
// -1が返されてきたら終わりです。
break;
}
// 出力します。
System.out.println( "0x" + Integer.toHexString( i ) + " ( " + i + " )" );
}
System.out.println( "InputThread : 終了します。" );
}
catch( IOException e )
{
// 出力時に問題があったらIOException例外が投げられます。
e.printStackTrace();
}
}
}




