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

📄 magneturihandlerimpl.java

📁 这是我模仿window自带的小游戏扫雷编的,很简单,只实现了扫雷的基本功能,现拿出来与大家分享!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}else if ( get.startsWith( "/select/" )){

			String	fail_reason = "";
			
			boolean	ok = false;
			
			String urn = (String)params.get( "xt" );

			if ( urn == null ){
				
				fail_reason	= "xt missing";
				
			}else{
					
				try{
				
					URL	url;
					
					if ( urn.startsWith( "http:") || urn.startsWith( "https:" )){
						
						url = new URL( urn );
						
					}else{
						
						url = new URL( "magnet:?xt=" + urn );
					}
					
					for (int i=0;i<listeners.size();i++){
						
						if (((MagnetURIHandlerListener)listeners.get(i)).download( url )){
							
							ok = true;
							
							break;
						}
					}
					
					if ( !ok ){
						
						fail_reason = "No listeners accepted the operation";
					}
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
					
					fail_reason	= Debug.getNestedExceptionMessage(e);
				}
			}
			
			if ( ok ){
				
				if ( "image".equalsIgnoreCase((String)params.get( "result" ))){
					
					for (int i=0;i<listeners.size();i++){

						byte[]	data = ((MagnetURIHandlerListener)listeners.get(i)).badge();
					
						if ( data != null ){
							
							writeReply( os, "image/gif", data );
							
							return( true );
						}
					}
				}
					
				writeReply( os, "text/plain", "Download initiated" );
				
			}else{
				
				writeReply( os, "text/plain", "Download initiation failed: " + fail_reason );
			}
			
		}else if ( get.startsWith( "/download/" )){
			
			String urn = (String)params.get( "xt" );
			
			if ( urn == null || !( urn.startsWith( "urn:sha1:") || urn.startsWith( "urn:btih:"))){
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
							"MagnetURIHandler: " + "invalid command - '" + get + "'"));
				
				return( true );
			}
			
			final PrintWriter	pw = new PrintWriter( new OutputStreamWriter( os, "UTF-8" ));

			try{			
				pw.print( "HTTP/1.0 200 OK" + NL ); 

				pw.flush();
								
				String	base_32 = urn.substring(9);
				
				List	sources = new ArrayList();
				
				for (int i=0;i<source_params.size();i++){
					
					String	source = (String)source_params.get(i);
					
					int	p = source.indexOf(':');
					
					if ( p != -1 ){
						
						try{
							InetSocketAddress	sa = new InetSocketAddress( source.substring(0,p), Integer.parseInt( source.substring(p+1)));
							
							sources.add( sa );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
					
				InetSocketAddress[]	s = new InetSocketAddress[ sources.size()];
				
				sources.toArray( s );
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, "MagnetURIHandler: download of '"
							+ base_32 + "' starts (initial sources=" + s.length + ")"));

				byte[] sha1 = Base32.decode( base_32 );
				
				byte[]	data = null;
				
				
				for (int i=0;i<listeners.size();i++){
				
					data = ((MagnetURIHandlerListener)listeners.get(i)).download(
							new MagnetURIHandlerProgressListener()
							{
								public void
								reportSize(
									long	size )
								{
									pw.print( "X-Report: " + getMessageText( "torrent_size", String.valueOf( size )) + NL );
									
									pw.flush();
								}
								
								public void
								reportActivity(
									String	str )
								{
									pw.print( "X-Report: " + str + NL );
																			
									pw.flush();
								}
								
								public void
								reportCompleteness(
									int		percent )
								{
									pw.print( "X-Report: " + getMessageText( "percent", String.valueOf(percent)) + NL );
									
									pw.flush();
								}
							},
							sha1, 
							s,
							DOWNLOAD_TIMEOUT );
					
					if ( data != null ){
						
						break;
					}
				}
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, "MagnetURIHandler: download of '"
							+ base_32
							+ "' completes, data "
							+ (data == null ? "not found"
									: ("found, length = " + data.length))));

				if ( data != null ){
					
					pw.print( "Content-Length: " + data.length + NL + NL );
					
					pw.flush();
					
					os.write( data );
					
					os.flush();
					
				}else{
					
						// HACK: don't change the "error:" message below, it is used by TorrentDownloader to detect this
						// condition
					
					pw.print( "X-Report: error: " + getMessageText( "no_sources" ) + NL );
					
					pw.flush();
					
						// pause on error
					
					return( !params.containsKey( "pause_on_error" ));
				}
			}catch( Throwable e ){
				
					// don't remove the "error:" (see above)
				
				pw.print( "X-Report: error: " + getMessageText( "error", Debug.getNestedExceptionMessage(e)) + NL );
				
				pw.flush();
				
				// Debug.printStackTrace(e);
				
					// pause on error
				
				return( !params.containsKey( "pause_on_error" ));
			}
		}else if ( get.startsWith( "/getinfo?" )){

			String name = (String)params.get( "name" );

			if ( name != null ){
				
				Integer	info = (Integer)info_map.get( name );
				
				if ( info != null ){					
					
					ByteArrayOutputStream	baos = new ByteArrayOutputStream();
					
					int	width 	= info.intValue();
					int	height	= 1;
					
					BufferedImage	image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
					
					for (int i=0;i<width;i++){
						for (int j=0;j<height;j++){
							int	red 	= i+j;
							int	green 	= i*255/width;
							int	blue	= j*255/height;
							
							image.setRGB(i,j,((red<<16)&0xff0000)|((green<<8)&0xff00)|((blue)&0xff));
						}
					}
					ImageIO.write( image, "JPG", baos );

					byte[]	data = baos.toByteArray();
											
					writeReply( os, "image/jpeg", data );
						
					return( true );
				}
			}
			
			writeNotFound( os );
			
			return( true );
			
		}
		
		return( true );
	}
	
	protected String
	getMessageText(
		String	resource )
	{
		return( MessageText.getString( "MagnetURLHandler.report." + resource ));
	}
	
	protected String
	getMessageText(
		String	resource,
		String	param )
	{
		return( MessageText.getString( "MagnetURLHandler.report." + resource, new String[]{ param } ));
	}
	
	protected String
	getJS(
		String	s )
	{
		return( "document.write(" + s + ");" + NL );
	}
	
	protected String
	getJSS(
		String	s )
	{
		return( "document.write(\"" + s + "\");" + NL );
	}
	
	protected void
	writeReply(
		OutputStream		os,
		String				content_type,
		String				content )
	
		throws IOException
	{
		writeReply( os, content_type, content.getBytes());
	}
	
	protected void
	writeReply(
		OutputStream		os,
		String				content_type,
		byte[]				content )
	
		throws IOException
	{
		PrintWriter	pw = new PrintWriter( new OutputStreamWriter( os ));

		pw.print( "HTTP/1.1 200 OK" + NL );
		pw.print( "Cache-Control: no-cache" + NL );
		pw.print( "Pragma: no-cache" + NL );
		pw.print( "Content-Type: " + content_type + NL );
		pw.print( "Content-Length: " + content.length + NL + NL );
		
		pw.flush();
		
		os.write( content );

	}
	
	protected void
	writeNotFound(
		OutputStream		os )
	
		throws IOException
	{
		PrintWriter	pw = new PrintWriter( new OutputStreamWriter( os ));

		pw.print( "HTTP/1.0 404 Not Found" + NL + NL );

		pw.flush();
	}
	
	public int
	getPort()
	{
		return( port );
	}
	
	public void
	addInfo(
		String		name,
		int			info )
	{
		info_map.put( name, new Integer(info));
		
		Logger.log(new LogEvent(LOGID, LogEvent.LT_INFORMATION,"MagnetURIHandler: global info registered: " + name + " -> " + info ));
	}
	
	public void
	addListener(
		MagnetURIHandlerListener	l )
	{
		listeners.add( l );
	}
	
	public void
	removeListener(
		MagnetURIHandlerListener	l )
	{
		listeners.remove( l );
	}
	
	public static void
	main(
		String[]	args )
	{
		new MagnetURIHandlerImpl();
		
		try{
			Thread.sleep(1000000);
		}catch( Throwable e ){
			
		}
	}
}

⌨️ 快捷键说明

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