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

📄 modemio.java

📁 jtapi for telephone
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                 if ((read = read()) > -1){
                    data[i] = (byte)read;
                 }else{
                    result = i - offs;
                    break;
                 }
            }
        }
        return result;
    }

    /**
     * Read a line from the underlying input stream (via the buffer).
     * A line is delimited by a '\r' character.
     * 
     * @return String - An entire line from the input stream.
     * @throws IOException
     */
    public String readLine() throws IOException{
        StringBuffer strBuf = new StringBuffer();
        int read;
        while ((read=read()) != -1 && read != '\r'){
            strBuf.append((char)read);
        }
        return strBuf.toString();
    }

    /**
     * Mark the current position in the buffer. No more than half of
     * the total buffer can be remembered; if readlimit is more than this
     * it is silently reduced.
     *
     * @param readlimit - The maximum number of bytes which can be read without losing the mark
     * @see #match()
     */
    public void mark(int readlimit){
        markPos = readPos;
        limit = limitCount = (Math.min(readlimit, BUF_SIZE/2));
    }

    /**
     * @see #mark()
     * @see #match()
     *
     * @return false because although mark (etc.) is implemented, several
     * private methods may move the mark. In other words use mark() with caution.
     */
    public boolean markSupported(){
        return false;
    }

    public void reset() throws IOException{
        if (markPos != MARK_INVALID){
            readPos = markPos;
            limitCount = limit;
        }
    }

    /**
     * Try to skip n bytes of data. Amount skipped is limited by
     * available data, but we will attempt to fill the buffer if
     * neccesary.
     *
     * @todo This hasn't been tested, need to check skip works whether wrapping
     *       or not. Also need to check mark is dealt with correctly.
     * @param n - the number of bytes to try to skip over.
     * @return long - the number of bytes actually skipped
     * @throws IOException
     */
    public long skip(long n) throws IOException{
        if (available() < n){
            fill();
        }

        n = Math.min(n, available()); //side effect: n must now be in int range!
        if (readPos + n < BUF_SIZE){
            readPos += n;
        }else{
            readPos = (int) n - (BUF_SIZE - readPos);
        }

        if (markPos != MARK_INVALID){
            if ((limitCount -= n) < 0){
                markPos = MARK_INVALID;
            }
        }

        return n;
    }

    /**
     * Close down and tidy up.
     * 
     * @throws IOException
     */
    public void close() throws IOException{
        handler.stop();
        in.close();
        out.close();
        port.close();
        in = null;
        out = null;
        port = null;
        buf = null;
    }

    /**
     * Send a line of text to the modem. A return charcter ('\r') is
     * appended to the line.
     * 
     * @param data - The data to be written to the modem.
     */
    public void writeLine(String data){
        out.print(data);
        out.print('\r');
        out.flush();
    }

    public void write(byte[] wrtBuf){
        write(wrtBuf, 0, wrtBuf.length);
    }

    public void write(byte[] wrtBuf, int off, int len){
        out.write(wrtBuf, off, len);
    }

    public void write(int b){
        out.write(b);
    }

    /**
     * Try to match the String goodMatch in the input buffer.
     *
     * The read position in the input buffer is moved to the first character
     * after the matched string.
     *
     * @param timeout - how long we can look for a match (in milliseconds)
     * @param goodMatch - the definition of success. May be a regular expression
     * @param badMatch - the definition of failure. May be a regular expression
     * @return -  TIMEOUT, GOOD_MATCH or BAD_MATCH as appropriate. Use getMatch()
     *            to get the String which caused either of the matches.
     * @throws IOException
     * @see #getMatch()
     */
    public int match(int timeout, String goodMatch, String badMatch)
        throws IOException{
        int result = TIMEOUT;
        long startTime = System.currentTimeMillis();

        Pattern p = Pattern.compile(goodMatch + '|' + badMatch);
        Matcher m = p.matcher("");

        //Loop until we match one of the Strings or we timeout
        boolean matched = false;
        while(matched == false){
            m.reset(readLine());
            matched = m.find();

            //Throw a TimeoutException if we have taken too long
            if (matched == false && (System.currentTimeMillis() - startTime > timeout)){
                //result = TIMEOUT; //default
                break;
            }
        }

        if (matched){
            lastMatch = m.group(0);
            p = Pattern.compile(goodMatch);
            m = p.matcher(lastMatch);
            if (m.find()){
                result = GOOD_MATCH;
            }else{
                result = BAD_MATCH;
            }
        }else{
            lastMatch = "";
        }

        return result;
    }

    /**
     * Returns the last string that was matched by the match(int, String, String) method.
     *
     * @return - String that was matched by match(int, String, String) if no match
     *            then empty string is returned.
     *
     * @see #match()
     */
    public String getMatch(){
        return lastMatch;
    }

    //SerialPortEventListener implementation
    public void serialEvent(SerialPortEvent evt){
        switch (evt.getEventType()) {
            case SerialPortEvent.RI:
                modem.ringing();
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                System.err.println("Data available");
                break;
            default:
                System.err.println("other event: " + evt.getEventType());
        }
    }

///////////////////////////////////////////////////////////////////////////////
    /**
     * A Runnable to deal with &lt;DLE&gt; shielded characters. the run() method
     * waits on <code>lock</code>, calling either stop() or setShield(char) will
     * call notifyAll() on <code>lock</code> which will wake up the run thread
     * to deal with the cause.
     * 
     * This may be an architectural mistake - maybe events would have worked
     * better?
     */
    private class ShieldHandler implements Runnable{
        private Vector shields = new Vector();
        private boolean stop = false;
        private final Object lock = new Object();

        public void run(){
            do{
                synchronized(lock){
                    try{
                        lock.wait();
                    }catch(InterruptedException ex){}
                }
                if ((stop == false) && (shields.size() > 0)){
                    char shield = ((Character)shields.remove(0)).charValue();
                    modem.dleReceived(shield);
                }
            }while (stop == false);
        }

        public void stop(){
            stop = true;
            synchronized (lock) {
                lock.notifyAll();
            }
        }

        public void setShield(char shield){
            shields.add(new Character(shield));
            synchronized (lock) {
                lock.notifyAll();
            }
        }
    }
}

⌨️ 快捷键说明

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