⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interruptiblebuffer.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
package com.sri.oaa2.simplefac;

import java.io.*;
import antlr_oaa.InputBuffer;
import antlr_oaa.CharStreamIOException;
import antlr_oaa.CharStreamException;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class InterruptibleBuffer extends InputBuffer 
{
  // Logger
  static Logger logger = Logger.getLogger(InterruptibleBuffer.class.getName());

  static StringBuffer b = new StringBuffer();
  static final int RINGBUFFERSIZE = 512;

  char[] ringBuffer = new char[RINGBUFFERSIZE];
  int ringBufferOffset = 0;
  int ringBufferUsed = 0;

  // char source
  transient InputStream inputStream;
  
  // connected
  private transient boolean connected = true;
  
  public InterruptibleBuffer(InputStream input) 
  {
    super();
    this.inputStream = input;
  }

  public synchronized void disconnect() 
  {
    connected = false;
  }

  public char[] getContext() 
  {
    char[] copy = new char[RINGBUFFERSIZE];
    int atEnd = RINGBUFFERSIZE - ringBufferOffset;
    int atBeginning = ringBufferOffset;
    System.arraycopy(ringBuffer, ringBufferOffset, copy, 0, atEnd);
    System.arraycopy(ringBuffer, 0, copy, atEnd, atBeginning);

    return copy;
  }

  public void fill(int amount) throws CharStreamException 
  {
    int nextChar = -1;
    syncConsume();
    try {
      READING_CHARS:
      while (queue.getNbrEntries() < amount + markerOffset) {
        // Append the next character
        try {
          nextChar = inputStream.read();
          if(nextChar == -1) {
            if(logger.isEnabledFor(Priority.WARN)) {
              logger.warn("InterruptibleBuffer.fill() got -1 from inputStream.read(): closing input");
            }
            disconnect();
            inputStream.close();
            throw new CharStreamIOException(new EOFException("Stream closed"));
          }
          queue.append((char)nextChar);
          if(logger.isDebugEnabled()) {
            b.append((char)nextChar);
            logger.debug("InterruptibleBuffer.fill() so far, got: " + b.toString());
          }
          int ringIndex = (ringBufferOffset + ringBufferUsed) % RINGBUFFERSIZE;
          ringBuffer[ringIndex] = (char)nextChar;
          ringBufferOffset += (ringBufferUsed ==  RINGBUFFERSIZE ? 1 : 0);
          ringBufferOffset %= RINGBUFFERSIZE;
          ringBufferUsed += (ringBufferUsed == RINGBUFFERSIZE ? 0 : 1);
        }
        catch(InterruptedIOException iio) {
          synchronized(this) {
            if(!connected) {
              if(logger.isEnabledFor(Priority.WARN)) {
                logger.warn("InterruptibleBuffer.fill() InterruptedIOException and not connected: closing input");
              }
              inputStream.close();
              InterruptedIOException miio = new InterruptedIOException("Interrupted " + iio.getMessage());
              miio.fillInStackTrace();
              throw new CharStreamIOException(miio);
            }
            else {
              continue READING_CHARS;
            }
          }
        }
      }
    }
    catch(IOException io) {
      if(logger.isInfoEnabled()) {
        logger.info("InterruptibleBuffer IOException: " + io.toString());
      }
      throw new CharStreamIOException(io);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -