OpenNapResumeRepository.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.opennap.net |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.plugin.opennap.net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.xnap.util.FileHelper;
import org.xnap.util.Preferences;
/**
* Provides a repository that handles the resume information for
* downloads in the incomplete directory.
*
* <p>This class is mostly thread safe (except for {@link #iterator()}).
*/
public class OpenNapResumeRepository {
//--- Constant(s) ---
/**
* The prefix used for resume files.
*/
public static final String FILENAME_PREFIX = ".xnap-openap-resume";
//--- Data Field(s) ---
private static Logger logger
= Logger.getLogger(OpenNapResumeRepository.class);
private List list = new LinkedList();
//--- Constructor(s) ---
public OpenNapResumeRepository()
{
}
//--- Method(s) ---
/**
* Adds a download to the repository. Creates a resume data file in path.
*
* @param path the incomplete directory
*/
public synchronized void add
(File path, OpenNapDownloadContainerData download)
{
list.add(download);
if (download.resumeFile == null) {
try {
download.resumeFile
= FileHelper.createUnique
(path,
FILENAME_PREFIX + "." + download.filename + ".xnap3");
write(download);
}
catch(IOException e) {
logger.error("Could not create resume file.", e);
}
}
}
/**
* Returns an iterator over all items in the repository.
*/
public Iterator iterator()
{
return list.iterator();
}
/**
* Removes a download from the repository, i.e. when the download
* has succeeded.
*/
public synchronized void remove(OpenNapDownloadContainerData download)
{
if (download.resumeFile != null) {
download.resumeFile.delete();
}
list.remove(download);
}
/**
* Restores a download from file.
*/
private synchronized OpenNapDownloadContainerData read(File file)
throws IOException
{
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
Properties p = new Properties();
p.load(in);
OpenNapDownloadContainerData data
= new OpenNapDownloadContainerData();
data.setAutoSearchingEnabled
(p.getProperty
("autoSearchingEnabled",
Preferences.getInstance().getAlwaysAutoDownload() + ""));
data.setFilename(p.getProperty("filename"));
data.setFilesize(p.getProperty("filesize"));
data.setRealm(p.getProperty("searchRealm"));
data.setSearchText(p.getProperty("searchText"));
// restore segments
List segments = new LinkedList();
int i = 0;
while (p.getProperty("segment." + i + ".filename") != null) {
try {
OpenNapSegmentData segment = new OpenNapSegmentData();
segment.setFile
(file.getParentFile(),
p.getProperty("segment." + i + ".filename"));
segment.setStart
(p.getProperty("segment." + i + ".start"));
segment.setMergeFailCount
(p.getProperty("segment." + i + ".mergeFailCount"));
segments.add(segment);
}
catch (IllegalArgumentException e) {
// invalid segment, ignore
logger.warn("Invalid segment: " + i + ", "
+ file.getAbsolutePath());
}
i++;
}
data.setSegments
((OpenNapSegmentData[])segments.toArray(new OpenNapSegmentData[0]));
data.resumeFile = file;
return data;
}
catch (IllegalArgumentException e) {
throw new IOException("Invalid data");
}
finally {
try {
in.close();
}
catch (IOException e) {
// this exception gets lost
}
}
}
/**
* Scans a directory for resume files and adds the found downloads.
*
* @param path the directory to scan
* @see #add(OpenNapDownloadContainerData)
*/
public synchronized void restore(File path)
{
logger.debug("Restoring resumes from " + path.getAbsolutePath());
File[] files = path.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()
&& files[i].getName().startsWith(FILENAME_PREFIX)) {
try {
OpenNapDownloadContainerData data = read(files[i]);
add(path, data);
}
catch (IOException e) {
logger.warn("Could not restore incomplete file", e);
}
}
}
}
}
/**
* Writes repository content to <code>filename</code>.
*/
public synchronized void save(File path)
{
for (Iterator i = list.iterator(); i.hasNext();) {
try {
OpenNapDownloadContainerData data
= (OpenNapDownloadContainerData)i.next();
write(data);
}
catch(IOException e) {
logger.warn("Could not write resume data", e);
}
}
}
/**
* Saves the resume information for data.
*/
private synchronized void write(OpenNapDownloadContainerData data)
throws IOException
{
OutputStream out
= new BufferedOutputStream(new FileOutputStream(data.resumeFile));
try {
Properties p = new Properties();
p.setProperty("autoSearchingEnabled",
data.autoSearchingEnabled + "");
p.setProperty("filename", data.filename);
p.setProperty("filesize", data.filesize + "");
if (data.realm != null) {
p.setProperty("searchRealm", data.realm);
}
p.setProperty("searchText", data.searchText);
// save segments
if (data.segments != null) {
int c = 0;
for (int i = 0; i < data.segments.length; i++) {
if (data.segments[i].file == null) {
// no need to keep track of empty segments
continue;
}
p.setProperty
("segment." + c + ".filename",
data.segments[i].file.getAbsolutePath());
p.setProperty
("segment." + c + ".start",
data.segments[i].start + "");
p.setProperty
("segment." + c + ".mergeFailCount",
data.segments[i].mergeFailCount + "");
c++;
}
}
p.store(out, "Automatically generated XNap OpenNap resume file - do not modify");
}
finally {
try {
out.close();
}
catch (IOException e) {
// this exception gets lost
}
}
}
}
The table below shows all metrics for OpenNapResumeRepository.java.



