📄 welcomemessage.java
字号:
do { int c = in.read(); switch( c ) { case -1 : throw new EOFException( "Stream terminated before end of welcome message" ); case '\r' : if( sawCR ) { welcomeBytes[readAt++] = (byte) 0x0D; } sawCR = true; break; case '\n' : if( sawCR ) { sawCRLF = true; } else { welcomeBytes[readAt++] = (byte) 0x0A; } break; default : welcomeBytes[readAt++] = (byte) c; sawCR = false; } if( readAt == welcomeBytes.length ) { throw new IOException( "Invalid welcome message, too long" ); } } while( !sawCRLF ); byte [] truncatedBytes = new byte [readAt]; System.arraycopy( welcomeBytes, 0, truncatedBytes, 0, readAt ); welcomeBytes = truncatedBytes; welcomeString = new String( welcomeBytes, "UTF-8" ); // we have the message, parse it. 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; } } /** * 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' ); } /** * 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 + -