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

📄 findaccessory.java

📁 j2se蓝牙发送文件很适合初学者 大师构架特别有帮助
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	/**
		Convenience method for converting the end date text to milliseconds
		since January 1, 1970. The end time is the end of the specified day.

		@return milliseconds since January 1, 1970
	*/
	protected long endDateToTime (String s)
	{
		if (s == null) return -1;
		SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

		long time = -1;
		Date d = dateFormatter.parse(s,new ParsePosition(0));
		if (d == null)
		{
			if (s.equalsIgnoreCase(TODAY))
			{
				String today = dateFormatter.format(new Date());
				d = dateFormatter.parse(today,new ParsePosition(0));
				if (d != null) time = d.getTime() + (24L*3600L*1000L);
			}
			else if (s.equalsIgnoreCase(YESTERDAY))
			{
				String yesterday = dateFormatter.format(
					new Date(new Date().getTime() - 24*60*60*1000));
				d = dateFormatter.parse(yesterday,new ParsePosition(0));
				if (d != null) time = d.getTime() + (24L*3600L*1000L);
			}
			else if (s.equalsIgnoreCase(NOW))
			{
				d = new Date();
				if (d != null) time = d.getTime();
			}
			else if (s.equalsIgnoreCase(THE_BIG_CRUNCH))
			{
				time = Long.MAX_VALUE;
			}
		}
		else
		{
			// Valid date. Now add 24 hours to make sure that the
			// date is inclusive
			time = d.getTime() + (24L*3600L*1000L);
		}

		return time;
	}

	/**
		Filter object for selecting files by the date range specified by the UI.
	*/
	class DateFilter implements FindFilter
	{
		protected long	startTime = -1;
		protected long	endTime = -1;


		DateFilter (long from, long to)
		{
			startTime = from;
			endTime = to;
		}

		public boolean accept (File f, FindProgressCallback callback)
		{
			if (f == null) return false;

			long t = f.lastModified();

			if (startTime >= 0)
			{
				if (t < startTime) return false;
			}
			if (endTime >= 0)
			{
				if (t > endTime) return false;
			}

			return true;
		}
	}

}


/**
Implements user interface and generates FindFilter for selecting
files by name.
*/
class FindByName extends JPanel implements FindFilterFactory
{
	protected String		NAME_CONTAINS = "contains";
	protected String		NAME_IS = "is";
	protected String		NAME_STARTS_WITH = "starts with";
	protected String		NAME_ENDS_WITH = "ends with";
	protected int			NAME_CONTAINS_INDEX = 0;
	protected int			NAME_IS_INDEX = 1;
	protected int			NAME_STARTS_WITH_INDEX = 2;
	protected int			NAME_ENDS_WITH_INDEX = 3;
	protected String[]		criteria = {NAME_CONTAINS,
										NAME_IS,
										NAME_STARTS_WITH,
										NAME_ENDS_WITH};

	protected JTextField	nameField = null;
	protected JComboBox		combo = null;
	protected JCheckBox		ignoreCaseCheck = null;

	FindByName ()
	{
		super();
		setLayout(new BorderLayout());

		// Grid Layout
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(0,2,2,2));

		// Name
		combo = new JComboBox(criteria);
		combo.setFont(new Font("Helvetica",Font.PLAIN,10));
		combo.setPreferredSize(combo.getPreferredSize());
		p.add(combo);

		nameField = new JTextField(12);
		nameField.setFont(new Font("Helvetica",Font.PLAIN,10));
		p.add(nameField);

		// ignore case
		p.add(new JLabel("",SwingConstants.RIGHT));

		ignoreCaseCheck = new JCheckBox("ignore case",true);
		ignoreCaseCheck.setForeground(Color.black);
		ignoreCaseCheck.setFont(new Font("Helvetica",Font.PLAIN,10));
		p.add(ignoreCaseCheck);

		add(p,BorderLayout.NORTH);
	}

	public FindFilter createFindFilter ()
	{
		return new NameFilter(nameField.getText(),combo.getSelectedIndex(),
								ignoreCaseCheck.isSelected());
	}

	/**
		Filter object for selecting files by name.
	*/
	class NameFilter implements FindFilter
	{
		protected String	match = null;
		protected int		howToMatch = -1;
		protected boolean	ignoreCase = true;

		NameFilter (String name, int how, boolean ignore)
		{
			match = name;
			howToMatch = how;
			ignoreCase = ignore;
		}

		public boolean accept (File f, FindProgressCallback callback)
		{
			if (f == null) return false;

			if ((match == null) || (match.length() == 0)) return true;
			if (howToMatch < 0) return true;

			String filename = f.getName();


			if (howToMatch == NAME_CONTAINS_INDEX)
			{
				if (ignoreCase)
				{
					if (filename.toLowerCase().indexOf(match.toLowerCase()) >= 0)
						return true;
					else return false;
				}
				else
				{
					if (filename.indexOf(match) >= 0) return true;
					else return false;
				}
			}
			else if (howToMatch == NAME_IS_INDEX)
			{
				if (ignoreCase)
				{
					if (filename.equalsIgnoreCase(match)) return true;
					else return false;
				}
				else
				{
					if (filename.equals(match)) return true;
					else return false;
				}
			}
			else if (howToMatch == NAME_STARTS_WITH_INDEX)
			{
				if (ignoreCase)
				{
					if (filename.toLowerCase().startsWith(match.toLowerCase()))
						return true;
					else return false;
				}
				else
				{
					if (filename.startsWith(match)) return true;
					else return false;
				}
			}
			else if (howToMatch == NAME_ENDS_WITH_INDEX)
			{
				if (ignoreCase)
				{
					if (filename.toLowerCase().endsWith(match.toLowerCase()))
						return true;
					else return false;
				}
				else
				{
					if (filename.endsWith(match)) return true;
					else return false;
				}
			}
			
			return true;
		}
	}

}


/**
Implements user interface and generates FindFilter for selecting
files by content.
<P>
<b>WARNING:</B> The FindFilter inner class for this object does
not implement an efficient strng search algorithm. Efficiency was
traded for code simplicity.
*/
class FindByContent extends JPanel implements FindFilterFactory
{

	/**
		Find for the first occurrence of the text in this field.
	*/
	protected JTextField	contentField = null;

	protected JCheckBox		ignoreCaseCheck = null;


	/**
		Constructs a user interface and a FindFilterFactory for searching
		files containing specified text.
	*/
	FindByContent ()
	{
		super();
		setLayout(new BorderLayout());

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));

		// Name
		JLabel l = new JLabel("File contains...",SwingConstants.LEFT);
		l.setForeground(Color.black);
		l.setFont(new Font("Helvetica",Font.PLAIN,10));
		p.add(l);

		contentField = new JTextField();
		contentField.setForeground(Color.black);
		contentField.setFont(new Font("Helvetica",Font.PLAIN,10));
		p.add(contentField);

		// ignore case
		ignoreCaseCheck = new JCheckBox("ignore case",true);
		ignoreCaseCheck.setForeground(Color.black);
		ignoreCaseCheck.setFont(new Font("Helvetica",Font.PLAIN,9));
		p.add(ignoreCaseCheck);

		add(p,BorderLayout.NORTH);
	}

	public FindFilter createFindFilter ()
	{
		return new ContentFilter(contentField.getText(),
									ignoreCaseCheck.isSelected());
	}

	/**
		Implements a simple content filter.
	*/
	class ContentFilter implements FindFilter
	{
		protected String	content = null;
		protected boolean	ignoreCase = true;

		ContentFilter (String s, boolean ignore)
		{
			content = s;
			ignoreCase = ignore;
		}

		public boolean accept (File f, FindProgressCallback callback)
		{
			if (f == null) return false;
			if (f.isDirectory()) return false;
			if ((content == null) || (content.length() == 0)) return true;

			boolean result = false;
			BufferedInputStream in = null;
			try
			{
				long fileLength = f.length();
				in = new BufferedInputStream(new FileInputStream(f));
				byte[] contentBytes = null;
				if (ignoreCase)
					contentBytes = content.toLowerCase().getBytes();
				else
					contentBytes = content.getBytes();
				LocatorStream locator = new LocatorStream(contentBytes);
				long counter = 0;
				int callbackCounter = 20; // Only call back every 20 bytes
				int c = -1;
				while((c = in.read()) != -1)
				{
					counter++;
					int matchChar = c;
					if (ignoreCase) matchChar =
						(int)Character.toLowerCase((char)c);
					locator.write(matchChar);

					// This search could be time consuming, especially since
					// this algorithm is not exactly the most efficient.
					// Report progress to search monitor and abort
					// if method returns false.
					if (callback != null)
					{
						if (--callbackCounter <= 0)
						{
							if (!callback.reportProgress(this,
												f,counter,fileLength))
								return false;
							callbackCounter = 20;
						}
					}
				}
			}
			catch (LocatedException e)
			{
				result = true;
			}
			catch (Throwable e)
			{
			}
			finally
			{
				try
				{
					if (in != null) in.close();
				}
				catch (IOException e)
				{
				}
				return result;
			}
		}


		/**
			Thrown when a LocatorStream object finds a byte array.
		*/
		class LocatedException extends IOException
		{
			public LocatedException (String msg)
			{
				super(msg);
			}

			public LocatedException (long location)
			{
				super(String.valueOf(location));
			}
		}

		/**
			Locate an array of bytes on the output stream. Throws
			a LocatedStream exception for every occurrence of the byte
			array.
		*/
		class LocatorStream extends OutputStream
		{
			protected byte[]	locate = null;
			protected Vector	matchMakers = new Vector();
			protected long		mark = 0;

			LocatorStream (byte[] b)
			{
				locate = b;
			}

			public void write (int b) throws IOException
			{
				if (locate == null)
					throw new IOException("NULL locator array");
				if (locate.length == 0)
					throw new IOException("Empty locator array");

				long foundAt = -1;

				for (int i=matchMakers.size()-1; i>=0; i--)
				{
					MatchStream m = (MatchStream)matchMakers.elementAt(i);
					try
					{
						m.write(b);
					}
					catch (MatchMadeException e)
					{
						foundAt = m.getMark();
						matchMakers.removeElementAt(i);
					}
					catch (IOException e)
					{
						// Match not made. Remove current matchMaker stream.
						matchMakers.removeElementAt(i);
					}
				}

				if (b == locate[0])
				{
					MatchStream m = new MatchStream(locate,mark);
					m.write(b); // This will be accepted
					matchMakers.addElement(m);
				}
				mark++;

				if (foundAt >= 0)
				{
					throw new LocatedException(foundAt);
				}
			}

			/**
				Thrown when the bytes written match the byte pattern.
			*/
			class MatchMadeException extends IOException
			{
				public MatchMadeException (String msg)
				{
					super(msg);
				}

				public MatchMadeException (long mark)
				{
					super(String.valueOf(mark));
				}
			}

			/**
				Accept "output" as long as it matches a specified array of
				bytes.
				Throw a MatchMadeException when the bytes written equals
				the match array.
				Throw an IOException when a byte does not match. Ignore
				everything after a match is made.
			*/
			class MatchStream extends OutputStream
			{
				protected long		mark = -1;
				protected int		pos = 0;
				protected byte[]	match = null;
				protected boolean	matchMade = false;

				MatchStream (byte[] b, long m)
				{
					mark = m;
					match = b;
				}

				public void write (int b) throws IOException
				{
					if (matchMade) return;
					if (match == null)
						throw new IOException("NULL match array");

					if (match.length == 0)
						throw new IOException("Empty match array");

					if (pos >= match.length)
						throw new IOException("No match");

					if (b != (byte)match[pos])
						throw new IOException("No match");

					pos++;
					if (pos >= match.length)
					{
						matchMade = true;
						throw new MatchMadeException(mark);
					}
				}

				public long getMark ()
				{
					return mark;
				}
			}
		}

	}

}


⌨️ 快捷键说明

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