📄 piecemapperimpl.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: PieceMapperImpl.java
package org.gudy.azureus2.core3.disk.impl.piecemapper.impl;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.gudy.azureus2.core3.disk.impl.DiskManagerFileInfoImpl;
import org.gudy.azureus2.core3.disk.impl.piecemapper.*;
import org.gudy.azureus2.core3.internat.LocaleUtilDecoder;
import org.gudy.azureus2.core3.torrent.TOTorrent;
import org.gudy.azureus2.core3.torrent.TOTorrentFile;
import org.gudy.azureus2.core3.util.FileUtil;
import org.gudy.azureus2.core3.util.StringInterner;
// Referenced classes of package org.gudy.azureus2.core3.disk.impl.piecemapper.impl:
// DMPieceMapImpl, DMPieceMapSimple, PieceListImpl, PieceMapEntryImpl
public class PieceMapperImpl
implements DMPieceMapper
{
protected static class fileInfo
implements DMPieceMapperFile
{
private DiskManagerFileInfoImpl file;
private TOTorrentFile torrent_file;
private String path;
private String name;
public long getLength()
{
return torrent_file.getLength();
}
public File getDataFile()
{
return new File(path, name);
}
public TOTorrentFile getTorrentFile()
{
return torrent_file;
}
public DiskManagerFileInfoImpl getFileInfo()
{
return file;
}
public void setFileInfo(DiskManagerFileInfoImpl _file)
{
file = _file;
}
public fileInfo(TOTorrentFile _torrent_file, String _path, String _name)
{
torrent_file = _torrent_file;
path = StringInterner.intern(_path);
name = _name;
}
}
private TOTorrent torrent;
private int last_piece_length;
protected ArrayList btFileList;
public PieceMapperImpl(TOTorrent _torrent)
{
btFileList = new ArrayList();
torrent = _torrent;
int piece_length = (int)torrent.getPieceLength();
int piece_count = torrent.getNumberOfPieces();
long total_length = torrent.getSize();
last_piece_length = (int)(total_length - (long)(piece_count - 1) * (long)piece_length);
}
public void construct(LocaleUtilDecoder _locale_decoder, String _save_name)
throws UnsupportedEncodingException
{
TOTorrentFile torrent_files[] = torrent.getFiles();
if (torrent.isSimpleTorrent())
buildFileLookupTables(torrent_files[0], _save_name);
else
buildFileLookupTables(torrent_files, _locale_decoder);
}
protected void buildFileLookupTables(TOTorrentFile torrent_file, String fileName)
{
btFileList.add(new fileInfo(torrent_file, "", fileName));
}
protected void buildFileLookupTables(TOTorrentFile torrent_files[], LocaleUtilDecoder locale_decoder)
throws UnsupportedEncodingException
{
char separator = File.separatorChar;
for (int i = 0; i < torrent_files.length; i++)
buildFileLookupTable(torrent_files[i], locale_decoder, separator);
}
private void buildFileLookupTable(TOTorrentFile torrent_file, LocaleUtilDecoder locale_decoder, char separator)
throws UnsupportedEncodingException
{
byte path_components[][] = torrent_file.getPathComponents();
StringBuffer pathBuffer = new StringBuffer(0);
int lastIndex = path_components.length - 1;
for (int j = 0; j < lastIndex; j++)
{
String comp = locale_decoder.decodeString(path_components[j]);
comp = FileUtil.convertOSSpecificChars(comp, true);
pathBuffer.append(comp);
pathBuffer.append(separator);
}
String last_comp = locale_decoder.decodeString(path_components[lastIndex]);
last_comp = FileUtil.convertOSSpecificChars(last_comp, false);
btFileList.add(new fileInfo(torrent_file, pathBuffer.toString(), last_comp));
}
public DMPieceMap getPieceMap()
{
if (btFileList.size() == 1)
return new DMPieceMapSimple(torrent, ((fileInfo)btFileList.get(0)).getFileInfo());
int piece_length = (int)torrent.getPieceLength();
int piece_count = torrent.getNumberOfPieces();
long total_length = torrent.getSize();
DMPieceList pieceMap[] = new DMPieceList[piece_count];
int modified_piece_length = piece_length;
if (total_length < (long)modified_piece_length)
modified_piece_length = (int)total_length;
long fileOffset = 0L;
int currentFile = 0;
for (int i = 0; 1 == piece_count && i < piece_count || i < piece_count - 1; i++)
{
ArrayList pieceToFileList = new ArrayList();
for (int usedSpace = 0; modified_piece_length > usedSpace;)
{
fileInfo tempFile = (fileInfo)btFileList.get(currentFile);
long length = tempFile.getLength();
long availableSpace = length - fileOffset;
PieceMapEntryImpl tempPieceEntry = null;
if (availableSpace <= (long)(modified_piece_length - usedSpace))
{
tempPieceEntry = new PieceMapEntryImpl(tempFile.getFileInfo(), fileOffset, (int)availableSpace);
usedSpace = (int)((long)usedSpace + availableSpace);
fileOffset = 0L;
currentFile++;
} else
{
tempPieceEntry = new PieceMapEntryImpl(tempFile.getFileInfo(), fileOffset, modified_piece_length - usedSpace);
fileOffset += modified_piece_length - usedSpace;
usedSpace += modified_piece_length - usedSpace;
}
pieceToFileList.add(tempPieceEntry);
}
pieceMap[i] = PieceListImpl.convert(pieceToFileList);
}
if (piece_count > 1)
pieceMap[piece_count - 1] = PieceListImpl.convert(buildLastPieceToFileList(btFileList, currentFile, fileOffset));
return new DMPieceMapImpl(pieceMap);
}
private List buildLastPieceToFileList(List file_list, int currentFile, long fileOffset)
{
int piece_length = (int)torrent.getPieceLength();
ArrayList pieceToFileList = new ArrayList();
for (int usedSpace = 0; last_piece_length > usedSpace;)
{
fileInfo tempFile = (fileInfo)file_list.get(currentFile);
long length = tempFile.getLength();
long availableSpace = length - fileOffset;
PieceMapEntryImpl tempPieceEntry = null;
if (availableSpace <= (long)(piece_length - usedSpace))
{
tempPieceEntry = new PieceMapEntryImpl(tempFile.getFileInfo(), fileOffset, (int)availableSpace);
usedSpace = (int)((long)usedSpace + availableSpace);
fileOffset = 0L;
currentFile++;
} else
{
tempPieceEntry = new PieceMapEntryImpl(tempFile.getFileInfo(), fileOffset, last_piece_length - usedSpace);
fileOffset += piece_length - usedSpace;
usedSpace += piece_length - usedSpace;
}
pieceToFileList.add(tempPieceEntry);
}
return pieceToFileList;
}
public long getTotalLength()
{
return torrent.getSize();
}
public int getPieceLength()
{
return (int)torrent.getPieceLength();
}
public int getLastPieceLength()
{
return last_piece_length;
}
public DMPieceMapperFile[] getFiles()
{
DMPieceMapperFile res[] = new DMPieceMapperFile[btFileList.size()];
btFileList.toArray(res);
return res;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -