Android Open Source - android-chess Timeseal Pipe






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jwtc.android.chess.ics;
/* w  w  w . j  ava2s .  c  om*/
import java.io.IOException;
import java.io.InterruptedIOException;

/**
 * This code was reverse engineered from the JIN project. JIN is a gpled
 * project. Its url can be found here: http://www.jinchess.com/
 */
public class TimesealPipe {

  private int _fldbyte;

  private int _fldchar;

  private volatile int _flddo;

  private final byte buffer[];

  private boolean closedFlag;

  private boolean closedFlag2;

  private Object readLock;

  private final TimesealInputStream timesealInputStream;

  private final TimesealOutputStream timesealOutputStream;

  private Object writeLock;

  public TimesealPipe() {
    this(2048);
  }

  public TimesealPipe(int i) {
    _flddo = 0;
    _fldchar = 0;
    _fldbyte = 0;
    closedFlag = false;
    closedFlag2 = false;
    writeLock = "Write Lock for PipedStreams";
    readLock = "Read Lock for PipedStream";
    timesealInputStream = new TimesealInputStream(this);
    timesealOutputStream = new TimesealOutputStream(this);
    buffer = new byte[i];
  }

  public int _mthdo() {
    return _flddo;
  }

  public void _mthif(int i) {
    synchronized (readLock) {
      _flddo = i;
    }
  }

  public TimesealInputStream getTimesealInputStream() {
    return timesealInputStream;
  }

  public TimesealOutputStream getTimesealOutputStream() {
    return timesealOutputStream;
  }

  synchronized int _mthcase() {
    if (closedFlag2) {
      return 0;
    } else {
      return _mthint();
    }
  }

  synchronized int _mthfor() throws IOException {
    synchronized (readLock) {
      if (closedFlag2) {
        throw new IOException("Stream closed");
      }
      long l = System.currentTimeMillis();
      while (_mthcase() == 0) {
        if (closedFlag) {
          byte byte0 = -1;
          return byte0;
        }
        long currentTime = System.currentTimeMillis();
        if (_flddo != 0 && currentTime - l >= _flddo) {
          throw new InterruptedIOException();
        }
        try {
          if (_flddo == 0) {
            wait();
          } else {
            wait(_flddo + currentTime - l);
          }
        } catch (InterruptedException _ex) {
          throw new InterruptedIOException();
        }
        if (closedFlag2) {
          throw new IOException("Stream closed");
        }
      }
      byte byte1 = buffer[_fldchar++];
      if (_fldchar == buffer.length) {
        _fldchar = 0;
      }
      notifyAll();
      int i = byte1 >= 0 ? (int) byte1 : byte1 + 256;
      return i;
    }
  }

  synchronized int _mthif(byte abyte0[], int i, int j) throws IOException {
    synchronized (readLock) {
      if (closedFlag2) {
        throw new IOException("Stream closed");
      }
      long currentTimeMillis = System.currentTimeMillis();
      while (_mthcase() == 0) {
        if (closedFlag) {
          byte byte0 = -1;
          return byte0;
        }
        long l1 = System.currentTimeMillis();
        if (_flddo != 0 && l1 - currentTimeMillis >= _flddo) {
          throw new InterruptedIOException();
        }
        try {
          if (_flddo == 0) {
            wait();
          } else {
            wait(_flddo + l1 - currentTimeMillis);
          }
        } catch (InterruptedException _ex) {
          throw new InterruptedIOException();
        }
        if (closedFlag2) {
          throw new IOException("Stream closed");
        }
      }
      int i1 = _mthcase();
      int j1 = j <= i1 ? j : i1;
      int k1 = buffer.length - _fldchar <= j1 ? buffer.length - _fldchar
          : j1;
      int i2 = j1 - k1 <= 0 ? 0 : j1 - k1;
      System.arraycopy(buffer, _fldchar, abyte0, i, k1);
      System.arraycopy(buffer, 0, abyte0, i + k1, i2);
      _fldchar = (_fldchar + j1) % buffer.length;
      notifyAll();
      int k = j1;
      return k;
    }
  }

  synchronized void _mthnew() {
    if (closedFlag2) {
      throw new IllegalStateException("Already closed");
    } else {
      closedFlag2 = true;
      notifyAll();
      return;
    }
  }

  synchronized void _mthtry() {
    if (closedFlag) {
      throw new IllegalStateException("Already closed");
    } else {
      closedFlag = true;
      notifyAll();
      return;
    }
  }

  synchronized void a(int i) throws IOException {
    synchronized (writeLock) {
      if (closedFlag2 || closedFlag) {
        throw new IOException("Stream closed");
      }
      while (duno() == 0) {
        try {
          wait();
        } catch (InterruptedException _ex) {
          throw new InterruptedIOException();
        }
      }
      if (closedFlag2 || closedFlag) {
        throw new IOException("Stream closed");
      }
      buffer[_fldbyte++] = (byte) (i & 0xff);
      if (_fldbyte == buffer.length) {
        _fldbyte = 0;
      }
      notifyAll();
    }
  }

  synchronized void write(byte bytes[], int i, int j) throws IOException {
    synchronized (writeLock) {
      if (closedFlag2 || closedFlag) {
        throw new IOException("Stream closed");
      }
      while (j > 0) {
        while (duno() == 0) {
          try {
            wait();
          } catch (InterruptedException _ex) {
            throw new InterruptedIOException();
          }
        }
        int k = duno();
        int l = j <= k ? j : k;
        int i1 = buffer.length - _fldbyte < l ? buffer.length
            - _fldbyte : l;
        int j1 = l - i1 <= 0 ? 0 : l - i1;
        System.arraycopy(bytes, i, buffer, _fldbyte, i1);
        System.arraycopy(bytes, i + i1, buffer, 0, j1);
        i += l;
        j -= l;
        _fldbyte = (_fldbyte + l) % buffer.length;
        notifyAll();
      }
    }
  }

  private int _mthint() {
    if (_fldbyte >= _fldchar) {
      return _fldbyte - _fldchar;
    } else {
      return _fldbyte + buffer.length - _fldchar;
    }
  }

  private int duno() {
    return buffer.length - _mthint() - 1;
  }
}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java