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

📄 welcomemessage.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * Attempts to init a welcome object from a socketChannel     *     * @param buffer the data buffer     * @return null if incomplete welcome was received     * @throws IOException if an io error occurs     */    public boolean read(ByteBuffer buffer) throws IOException {        int limit = buffer.limit();        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine(MessageFormat.format("Reading a buffer of size :{0}", limit));        }        if (limit == 0) {            throw new IOException(MessageFormat.format("Invalid welcome message. Invalid length {0}", limit));        }        int eomPos = findEom(buffer, 0, limit);        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine(MessageFormat.format("Buffer size :{0} Welcome End-Of-Message pos :{1}", limit, eomPos));        }        if (eomPos < 0) {            return false;        }        welcomeBytes = new byte[eomPos];        try {            buffer.get(welcomeBytes, 0, eomPos);            // skip <cr><ln>            buffer.position(eomPos + 2);            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine(MessageFormat.format("buffer stats :{0}", buffer.toString()));            }        } catch (BufferUnderflowException buf) {            // not enough data, signal for another read            return false;        }        welcomeString = new String(welcomeBytes, "UTF-8");        parseWelcome(welcomeString);        return true;    }    /**     * returns position of <cr><lf> position in buffer, otherwise -1     *     * @param buffer the byte buffer     * @param offset The offset within the buffer array     * @param length the length     * @return terminating position, or -1 if none found     */    private int findEom(ByteBuffer buffer, int offset, int length) {        int lastOffset = length - 2; // we are looking for 2 chars.                for (int j = offset; j <= lastOffset; j++) {            byte c = buffer.get(j);                       if (c == '\r') {                c = buffer.get(j + 1);                               if (c == '\n') {                    return j;                }            }        }                return -1;    }    private void parseWelcome(String welcomeString) throws IOException {        List<String> thePieces = Arrays.asList(welcomeString.split("\\s"));        if (0 == thePieces.size()) {            throw new IOException("Invalid welcome message, did not contain any tokens.");        }        if (thePieces.size() < 5) {            throw new IOException("Invalid welcome message, did not contain enough tokens.");        }        if (!GREETING.equals(thePieces.get(0))) {            throw new IOException("Invalid welcome message, did not start with greeting");        }        try {            destinationAddress = new EndpointAddress(thePieces.get(1));        } catch (IllegalArgumentException badAddress) {            IOException failed = new IOException("Invalid welcome message, bad destination address");            failed.initCause(badAddress);            throw failed;        }        try {            publicAddress = new EndpointAddress(thePieces.get(2));        } catch (IllegalArgumentException badAddress) {            IOException failed = new IOException("Invalid welcome message, bad publicAddress address");            failed.initCause(badAddress);            throw failed;        }        try {            URI peerURI = new URI(thePieces.get(3));            peerID = IDFactory.fromURI(peerURI);        } catch (URISyntaxException badURI) {            IOException failed = new IOException("Invalid welcome message, bad peer id");            failed.initCause(badURI);            throw failed;        }        versionString = thePieces.get(thePieces.size() - 1);        if (WELCOME_VERSION_1_1.equals(versionString)) {            if (6 != thePieces.size()) {                throw new IOException("Invalid welcome message, incorrect number of tokens.");            }            String noPropagateStr = thePieces.get(4);            if (noPropagateStr.equals("1")) {                noPropagate = true;            } else if (noPropagateStr.equals("0")) {                noPropagate = false;            } else {                throw new IOException("Invalid welcome message, illegal value for propagate flag");            }            // preferred message version is not set in            preferredMessageVersion = 0;        } else if (WELCOME_VERSION_3_0.equals(versionString)) {            if (7 != thePieces.size()) {                throw new IOException("Invalid welcome message, incorrect number of tokens.");            }            String noPropagateStr = thePieces.get(4);            if (noPropagateStr.equals("1")) {                noPropagate = true;            } else if (noPropagateStr.equals("0")) {                noPropagate = false;            } else {                throw new IOException("Invalid welcome message, illegal value for propagate flag");            }            String preferredVersionStr = thePieces.get(5);            try {                preferredMessageVersion = Integer.valueOf(preferredVersionStr);            } catch (IllegalArgumentException failed) {                IOException failure = new IOException("Invalid welcome message, illegal value for preferred message version");                failure.initCause(failed);                throw failure;            }        } else {            // Unrecognized Welcome message version. Use default values.            noPropagate = false;            preferredMessageVersion = 0;        }                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("Successfuly parsed a welcome message :" + getWelcomeString());        }    }    /**     * Write the welcome message to the provided stream.     *     * @param theStream The OutputStream to which to write the welcome message.     * @throws IOException If there is a problem writing the welcome message.     */    public void sendToStream(OutputStream theStream) throws IOException {        theStream.write(welcomeBytes);        theStream.write('\r');        theStream.write('\n');    }    /**     * Write the welcome to a socket channel     *     * @return A ByteBuffer of the welcome message     * @throws java.io.IOException if an io error occurs     */    public ByteBuffer getByteBuffer() throws IOException {        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine(MessageFormat.format("Sending welcome message of size:{0}", welcomeBytes.length + 2));        }        ByteBuffer buffer = ByteBuffer.allocate(welcomeBytes.length + 2);        buffer.put(welcomeBytes);        buffer.put((byte) '\r');        buffer.put((byte) '\n');        buffer.flip();        return buffer;    }    /**     * Return the peerid associated with the Welcome Message.     *     * @return The peer ID from the Welcome Message.     */    public ID getPeerID() {        return peerID;    }    /**     * Return the source address associated with the Welcome Message.     *     * @return The source address from the Welcome Message.     */    public EndpointAddress getPublicAddress() {        return publicAddress;    }    /**     * Return the destination address associated with the Welcome Message.     *     * @return The destination address from the Welcome Message.     */    public EndpointAddress getDestinationAddress() {        return destinationAddress;    }    /**     * Return the propagation preference from the Welcome Message.     *     * @return <tt>true</tt> if <strong>no</strong> propagation is desired     *         otherwise <tt>false</tt>     */    public boolean dontPropagate() {        return noPropagate;    }    /**     * Return the preferred message version from the Welcome Message.     *     * @return The preferred Message Version.     */    public int getPreferredMessageVersion() {        return preferredMessageVersion;    }    /**     * Return the version associated with the Welcome Message.     *     * @return The version from the Welcome Message.     */    public String getWelcomeVersion() {        return versionString;    }    /**     * Return a String containing the Welcome Message.     *     * @return a String containing the Welcome Message.     */    public String getWelcomeString() {        return welcomeString;    }}

⌨️ 快捷键说明

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