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

📄 fileutil.java

📁 java 文件下载器。可自定义
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			res = readResilientFile(file_name, parent_dir, file_name, 0, true);
			if (res != null)
				Logger.log(new LogAlert(false, 1, (new StringBuilder()).append("File '").append(file_name).append("' has been partially recovered, ").append("information may have been lost!").toString()));
		}
		map = res;
		releaseReservedFileHandles();
		class_mon.exit();
		return map;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		map = null;
		releaseReservedFileHandles();
		class_mon.exit();
		return map;
		Exception exception;
		exception;
		releaseReservedFileHandles();
		throw exception;
		Exception exception1;
		exception1;
		class_mon.exit();
		throw exception1;
	}

	private static Map readResilientFile(String original_file_name, File parent_dir, String file_name, int fail_count, boolean recovery_mode)
	{
		boolean using_backup;
		File file;
		BufferedInputStream bin;
		using_backup = file_name.endsWith(".saving");
		file = new File(parent_dir, file_name);
		if (!file.exists() || file.length() <= 1L)
			if (using_backup)
			{
				if (!recovery_mode && fail_count == 1)
					Logger.log(new LogAlert(false, 3, (new StringBuilder()).append("Load of '").append(original_file_name).append("' fails, no usable file or backup").toString()));
				return null;
			} else
			{
				if (recovery_mode);
				return readResilientFile(original_file_name, parent_dir, (new StringBuilder()).append(file_name).append(".saving").toString(), 0, recovery_mode);
			}
		bin = null;
		Map map1;
		int retry_limit = 5;
		do
			try
			{
				bin = new BufferedInputStream(new FileInputStream(file), 16384);
				break;
			}
			catch (IOException e)
			{
				if (--retry_limit == 0)
					throw e;
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, (new StringBuilder()).append("Failed to open '").append(file.toString()).append("', retrying").toString(), e));
				Thread.sleep(500L);
			}
		while (true);
		BDecoder decoder = new BDecoder();
		if (recovery_mode)
			decoder.setRecoveryMode(true);
		Map res = decoder.decodeStream(bin);
		if (using_backup && !recovery_mode)
			Logger.log(new LogAlert(false, 1, (new StringBuilder()).append("Load of '").append(original_file_name).append("' had to revert to backup file").toString()));
		map1 = res;
		try
		{
			if (bin != null)
				bin.close();
		}
		catch (Exception e)
		{
			Debug.printStackTrace(e);
		}
		return map1;
		Throwable e;
		e;
		Map map;
		Debug.printStackTrace(e);
		try
		{
			if (bin != null)
			{
				bin.close();
				bin = null;
			}
		}
		catch (Exception x)
		{
			Debug.printStackTrace(x);
		}
		if (!recovery_mode)
		{
			int bad_id = 0;
			File bad;
			do
			{
				File test = new File(parent_dir, (new StringBuilder()).append(file.getName()).append(".bad").append(bad_id != 0 ? (new StringBuilder()).append("").append(bad_id).toString() : "").toString());
				if (!test.exists())
				{
					bad = test;
					break;
				}
				bad_id++;
			} while (true);
			if (Logger.isEnabled())
				Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Read of '").append(original_file_name).append("' failed, decoding error. ").append("Renaming to ").append(bad.getName()).toString()));
			copyFile(file, bad);
		}
		if (!using_backup)
			break MISSING_BLOCK_LABEL_588;
		if (!recovery_mode)
			Logger.log(new LogAlert(false, 3, (new StringBuilder()).append("Load of '").append(original_file_name).append("' fails, no usable file or backup").toString()));
		map = null;
		try
		{
			if (bin != null)
				bin.close();
		}
		catch (Exception e)
		{
			Debug.printStackTrace(e);
		}
		return map;
		map = readResilientFile(original_file_name, parent_dir, (new StringBuilder()).append(file_name).append(".saving").toString(), 1, recovery_mode);
		try
		{
			if (bin != null)
				bin.close();
		}
		catch (Exception e)
		{
			Debug.printStackTrace(e);
		}
		return map;
		Exception exception;
		exception;
		try
		{
			if (bin != null)
				bin.close();
		}
		catch (Exception e)
		{
			Debug.printStackTrace(e);
		}
		throw exception;
	}

	public static void deleteResilientFile(File file)
	{
		file.delete();
		(new File(file.getParentFile(), (new StringBuilder()).append(file.getName()).append(".bak").toString())).delete();
	}

	public static void deleteResilientConfigFile(String name)
	{
		File parent_dir = new File(SystemProperties.getUserPath());
		(new File(parent_dir, name)).delete();
		(new File(parent_dir, (new StringBuilder()).append(name).append(".bak").toString())).delete();
	}

	private static void getReservedFileHandles()
	{
		class_mon.enter();
		while (reserved_file_handles.size() > 0) 
		{
			InputStream is = (InputStream)reserved_file_handles.remove(0);
			try
			{
				is.close();
			}
			catch (Throwable e)
			{
				Debug.printStackTrace(e);
			}
		}
		class_mon.exit();
		break MISSING_BLOCK_LABEL_63;
		Exception exception;
		exception;
		class_mon.exit();
		throw exception;
	}

	private static void releaseReservedFileHandles()
	{
		class_mon.enter();
		File lock_file = new File((new StringBuilder()).append(SystemProperties.getUserPath()).append(".lock").toString());
		lock_file.createNewFile();
		InputStream is;
		for (; reserved_file_handles.size() < 4; reserved_file_handles.add(is))
			is = new FileInputStream(lock_file);

		class_mon.exit();
		break MISSING_BLOCK_LABEL_106;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		class_mon.exit();
		break MISSING_BLOCK_LABEL_106;
		Exception exception;
		exception;
		class_mon.exit();
		throw exception;
	}

	public static void backupFile(String _filename, boolean _make_copy)
	{
		backupFile(new File(_filename), _make_copy);
	}

	public static void backupFile(File _file, boolean _make_copy)
	{
		if (_file.length() > 0L)
		{
			File bakfile = new File((new StringBuilder()).append(_file.getAbsolutePath()).append(".bak").toString());
			if (bakfile.exists())
				bakfile.delete();
			if (_make_copy)
				copyFile(_file, bakfile);
			else
				_file.renameTo(bakfile);
		}
	}

	public static boolean copyFile(String _source_name, String _dest_name)
	{
		return copyFile(new File(_source_name), new File(_dest_name));
	}

	public static boolean copyFile(File _source, File _dest)
	{
		copyFile(((InputStream) (new FileInputStream(_source))), ((OutputStream) (new FileOutputStream(_dest))));
		return true;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return false;
	}

	public static boolean copyFile(File _source, OutputStream _dest, boolean closeInputStream)
	{
		copyFile(((InputStream) (new FileInputStream(_source))), _dest, closeInputStream);
		return true;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return false;
	}

	public static void copyFile(InputStream _source, File _dest)
		throws IOException
	{
		FileOutputStream dest;
		boolean close_input;
		dest = null;
		close_input = true;
		dest = new FileOutputStream(_dest);
		close_input = false;
		copyFile(_source, ((OutputStream) (dest)), true);
		try
		{
			if (close_input)
				_source.close();
		}
		catch (IOException e) { }
		if (dest != null)
			dest.close();
		break MISSING_BLOCK_LABEL_71;
		Exception exception;
		exception;
		try
		{
			if (close_input)
				_source.close();
		}
		catch (IOException e) { }
		if (dest != null)
			dest.close();
		throw exception;
	}

	public static void copyFile(InputStream is, OutputStream os)
		throws IOException
	{
		copyFile(is, os, true);
	}

	public static void copyFile(InputStream is, OutputStream os, boolean closeInputStream)
		throws IOException
	{
		if (!(is instanceof BufferedInputStream))
			is = new BufferedInputStream(is);
		byte buffer[] = new byte[0x20000];
		do
		{
			int len = is.read(buffer);
			if (len == -1)
				break;
			os.write(buffer, 0, len);
		} while (true);
		try
		{
			if (closeInputStream)
				is.close();
		}
		catch (IOException e) { }
		os.close();
		break MISSING_BLOCK_LABEL_89;
		Exception exception;
		exception;
		try
		{
			if (closeInputStream)
				is.close();
		}
		catch (IOException e) { }
		os.close();
		throw exception;
	}

	public static void copyFileOrDirectory(File from_file_or_dir, File to_parent_dir)
		throws IOException
	{
		if (!from_file_or_dir.exists())
			throw new IOException((new StringBuilder()).append("File '").append(from_file_or_dir.toString()).append("' doesn't exist").toString());
		if (!to_parent_dir.exists())
			throw new IOException((new StringBuilder()).append("File '").append(to_parent_dir.toString()).append("' doesn't exist").toString());
		if (!to_parent_dir.isDirectory())
			throw new IOException((new StringBuilder()).append("File '").append(to_parent_dir.toString()).append("' is not a directory").toString());
		if (from_file_or_dir.isDirectory())
		{
			File files[] = from_file_or_dir.listFiles();
			File new_parent = new File(to_parent_dir, from_file_or_dir.getName());
			mkdirs(new_parent);
			for (int i = 0; i < files.length; i++)
			{
				File from_file = files[i];
				copyFileOrDirectory(from_file, new_parent);
			}

		} else
		{
			File target = new File(to_parent_dir, from_file_or_dir.getName());
			if (!copyFile(from_file_or_dir, target))
				throw new IOException((new StringBuilder()).append("File copy from ").append(from_file_or_dir).append(" to ").append(target).append(" failed").toString());
		}
	}

	public static File getFileOrBackup(String _filename)
	{
		File file;
		File bakfile;
		file = new File(_filename);
		if (file.length() > 1L)
			break MISSING_BLOCK_LABEL_58;
		bakfile = new File((new StringBuilder()).append(_filename).append(".bak").toString());
		if (bakfile.length() <= 1L)
			return null;
		return bakfile;
		return file;
		Exception e;
		e;
		Debug.out(e);
		return null;
	}

	public static File getJarFileFromClass(Class cla)
	{
		File jar_file;
		String str = cla.getName();
		str = (new StringBuilder()).append(str.replace('.', '/')).append(".class").toString();
		URL url = cla.getClassLoader().getResource(str);
		if (url == null)
			break MISSING_BLOCK_LABEL_84;
		String url_str = url.toExternalForm();
		if (!url_str.startsWith("jar:file:"))
			break MISSING_BLOCK_LABEL_84;
		jar_file = getJarFileFromURL(url_str);
		if (jar_file.exists())
			return jar_file;
		break MISSING_BLOCK_LABEL_84;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return null;
	}

	public static File getJarFileFromURL(String url_str)
	{
		if (!url_str.startsWith("jar:file:"))
			break MISSING_BLOCK_LABEL_76;
		url_str = url_str.replaceAll(" ", "%20");
		if (!url_str.startsWith("jar:file:/"))
			url_str = "jar:file:/".concat(url_str.substring(9));
		File jar;
		int posPling = url_str.lastIndexOf('!');
		String jarName = url_str.substring(4, posPling);
		URI uri = URI.create(jarName);
		jar = new File(uri);
		return jar;
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return null;
	}

	public static boolean renameFile(File from_file, File to_file)
	{
		return renameFile(from_file, to_file, true);
	}

	public static boolean renameFile(File from_file, File to_file, boolean fail_on_existing_directory)
	{
		return renameFile(from_file, to_file, fail_on_existing_directory, null);

⌨️ 快捷键说明

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