Class AbortablePipedStream

java.lang.Object
com.streamconverter.AbortablePipedStream
All Implemented Interfaces:
AutoCloseable

public final class AbortablePipedStream extends Object implements AutoCloseable
コマンド間の異常終了を伝播するためのPipedStreamペアラッパー。

いずれかのコマンドが異常終了した際に abort() を呼ぶことで、 対向の read()/write() に PipeAbortedException を送出させる。 これにより、対向コマンドは「パイプが閉じた」という IO エラーではなく、 「対向コマンドが異常終了した」という型付きの例外を受け取って終了できる。

使用例:


 AbortablePipedStream pipe = new AbortablePipedStream(bufferSize);
 // 前段コマンドには pipe.outputStream() を渡す
 // 後段コマンドには pipe.inputStream() を渡す

 // 前段コマンドが異常終了した場合:
 pipe.abort();
 // → 後段コマンドの read() が PipeAbortedException を投げる
 
See Also:
  • Constructor Details

    • AbortablePipedStream

      public AbortablePipedStream(int bufferSize) throws IOException
      指定バッファサイズで PipedStream ペアを生成する。
      Parameters:
      bufferSize - パイプバッファのサイズ(バイト)。1 以上であること
      Throws:
      IllegalArgumentException - bufferSize が 1 未満の場合
      IOException - PipedStream の生成に失敗した場合
  • Method Details

    • outputStream

      書き込み側のラッパーを返す(前段コマンドに渡す)。

      このパイプは書き込み側を1つのコマンドのみが使用することを想定しており、 2回目の呼び出しは IllegalStateException を投げる。

      Returns:
      前段コマンド用の OutputStream
      Throws:
      IllegalStateException - 2回目以降の呼び出しの場合
    • inputStream

      読み込み側のラッパーを返す(後段コマンドに渡す)。

      このパイプは読み込み側を1つのコマンドのみが使用することを想定しており、 2回目の呼び出しは IllegalStateException を投げる。

      Returns:
      後段コマンド用の InputStream
      Throws:
      IllegalStateException - 2回目以降の呼び出しの場合
    • abort

      public void abort()
      このパイプを abort 済みとしてマークする。

      以降の read()/write()/flush() 呼び出しが PipeAbortedException を投げるようになる。 パイプのクローズは StreamConvertercloseResources() が担うため、 このメソッドはフラグのセットのみを行う。

      close() の前後どちらで呼んでも安全。ただし close() 後は read()/write() が呼ばれる機会がないため、abort フラグは実質的に効果を持たない。

    • close

      public void close() throws IOException
      パイプを正常にクローズする。

      AutoCloseable の実装。abort フラグはセットしない。 abort() の前後どちらで呼んでも安全。

      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException