Example usage for java.util Collections synchronizedSortedSet

List of usage examples for java.util Collections synchronizedSortedSet

Introduction

In this page you can find the example usage for java.util Collections synchronizedSortedSet.

Prototype

public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) 

Source Link

Document

Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

Usage

From source file:com.p2p.peercds.client.SharedTorrent.java

/**
 * Create a new shared torrent from meta-info binary data.
 *
 * @param torrent The meta-info byte data.
 * @param parent The parent directory or location the torrent files.
 * @param seeder Whether we're a seeder for this torrent or not (disables
 * validation).//from   w  w w  . ja  v a2  s .c o  m
 * @throws FileNotFoundException If the torrent file location or
 * destination directory does not exist and can't be created.
 * @throws IOException If the torrent file cannot be read or decoded.
 */
public SharedTorrent(byte[] torrent, File parent, boolean seeder) throws FileNotFoundException, IOException {
    super(torrent, seeder);

    if (parent == null || !parent.isDirectory()) {
        throw new IllegalArgumentException("Invalid parent directory!");
    }

    String parentPath = parent.getCanonicalPath();

    try {
        this.pieceLength = this.decoded_info.get("piece length").getInt();
        this.piecesHashes = ByteBuffer.wrap(this.decoded_info.get("pieces").getBytes());

        if (this.piecesHashes.capacity() / PIECE_HASH_SIZE * (long) this.pieceLength < this.getSize()) {
            throw new IllegalArgumentException(
                    "Torrent size does not " + "match the number of pieces and the piece size!");
        }
    } catch (InvalidBEncodingException ibee) {
        throw new IllegalArgumentException("Error reading torrent meta-info fields!");
    }

    List<FileStorage> files = new LinkedList<FileStorage>();
    long offset = 0L;
    for (Torrent.TorrentFile file : this.files) {
        File actual = new File(parent, file.file.getPath());

        if (!actual.getCanonicalPath().startsWith(parentPath)) {
            throw new SecurityException("Torrent file path attempted " + "to break directory jail!");
        }

        actual.getParentFile().mkdirs();
        files.add(new FileStorage(actual, offset, file.size));
        offset += file.size;
    }
    this.bucket = new FileCollectionStorage(files, this.getSize());

    this.random = new Random(System.currentTimeMillis());
    this.stop = false;

    this.uploaded = 0;
    this.downloaded = 0;
    this.numBytesFetchedFromCloud = 0;
    this.lastCloudFetchTime = 0;
    this.left = this.getSize();

    this.initialized = false;
    this.pieces = new Piece[0];
    this.rarest = Collections.synchronizedSortedSet(new TreeSet<Piece>());
    this.completedPieces = new BitSet();
    this.requestedPieces = new BitSet();
}

From source file:com.turn.ttorrent.client.SharedTorrent.java

/**
 * Create a new shared torrent from meta-info binary data.
 *
 * @param torrent The meta-info byte data.
 * @param parent The parent directory or location the torrent files.
 * @param seeder Whether we're a seeder for this torrent or not (disables
 * validation)./*from w  w  w .j  ava 2s. c  om*/
 * @param requestStrategy The request strategy implementation.
 * @throws FileNotFoundException If the torrent file location or
 * destination directory does not exist and can't be created.
 * @throws IOException If the torrent file cannot be read or decoded.
 */
public SharedTorrent(byte[] torrent, File parent, boolean seeder, RequestStrategy requestStrategy)
        throws FileNotFoundException, IOException, NoSuchAlgorithmException {
    super(torrent, seeder);

    if (parent == null || !parent.isDirectory()) {
        throw new IllegalArgumentException("Invalid parent directory!");
    }

    String parentPath = parent.getCanonicalPath();

    try {
        this.pieceLength = this.decoded_info.get("piece length").getInt();
        this.piecesHashes = ByteBuffer.wrap(this.decoded_info.get("pieces").getBytes());

        if (this.piecesHashes.capacity() / Torrent.PIECE_HASH_SIZE * (long) this.pieceLength < this.getSize()) {
            throw new IllegalArgumentException(
                    "Torrent size does not " + "match the number of pieces and the piece size!");
        }
    } catch (InvalidBEncodingException ibee) {
        throw new IllegalArgumentException("Error reading torrent meta-info fields!");
    }

    List<FileStorage> files = new LinkedList<FileStorage>();
    long offset = 0L;
    for (Torrent.TorrentFile file : this.files) {
        File actual = new File(parent, file.file.getPath());

        if (!actual.getCanonicalPath().startsWith(parentPath)) {
            throw new SecurityException("Torrent file path attempted " + "to break directory jail!");
        }

        actual.getParentFile().mkdirs();
        files.add(new FileStorage(actual, offset, file.size));
        offset += file.size;
    }
    this.bucket = new FileCollectionStorage(files, this.getSize());

    this.stop = false;

    this.uploaded = 0;
    this.downloaded = 0;
    this.left = this.getSize();

    this.initialized = false;
    this.pieces = new Piece[0];
    this.rarest = Collections.synchronizedSortedSet(new TreeSet<Piece>());
    this.completedPieces = new BitSet();
    this.requestedPieces = new BitSet();

    //TODO: should switch to guice
    this.requestStrategy = requestStrategy;
}

From source file:com.cyberway.issue.crawler.frontier.WorkQueueFrontier.java

/**
 * Set up the various queues-of-queues used by the frontier. Override
 * in implementing subclasses to reduce or eliminate risk of queues
 * growing without bound. /*from ww  w.  j  a  v a 2 s .co m*/
 */
protected void initQueuesOfQueues() {
    // small risk of OutOfMemoryError: if 'hold-queues' is false,
    // readyClassQueues may grow in size without bound
    readyClassQueues = new LinkedBlockingQueue<String>();
    // risk of OutOfMemoryError: in large crawls, 
    // inactiveQueues may grow in size without bound
    inactiveQueues = new LinkedBlockingQueue<String>();
    // risk of OutOfMemoryError: in large crawls with queue max-budgets, 
    // inactiveQueues may grow in size without bound
    retiredQueues = new LinkedBlockingQueue<String>();
    // small risk of OutOfMemoryError: in large crawls with many 
    // unresponsive queues, an unbounded number of snoozed queues 
    // may exist
    snoozedClassQueues = Collections.synchronizedSortedSet(new TreeSet<WorkQueue>());
}

From source file:org.ralasafe.entitle.BackupManagerImpl.java

public Collection getBackups() {
    Collection backups = selector.select(new SelectCondition(), null);
    SortedSet result = Collections.synchronizedSortedSet(new TreeSet(comp));
    result.addAll(backups);/*from   w  w  w  .j av  a 2 s  . co m*/
    return result;
}

From source file:uk.ac.ebi.arrayexpress.jobs.SimilarityJob.java

private SortedSet<String> getLowPriorityURIList(IEFO efo, String ignoreListFileLocation) throws IOException {
    SortedSet<String> lowPriorityURIs = new TreeSet<>();

    if (null != ignoreListFileLocation) {

        try (InputStream is = getApplication().getResource(ignoreListFileLocation).openStream()) {
            Set<String> lowPriorityURIList = StringTools.streamToStringSet(is, "UTF-8");

            logger.debug("Loaded low priority ontology classes from [{}]", ignoreListFileLocation);
            for (String uri : lowPriorityURIList) {
                if (null != uri && !"".equals(uri) && !uri.startsWith("#")) {
                    if (efo.getMap().containsKey(uri))
                        lowPriorityURIs.add(uri);
                    else
                        logger.warn("URI: " + uri + " doesn't exist in EFO");
                }/*from  w  w  w .  j av  a2s.  c  o m*/
            }
        }
    }
    return Collections.synchronizedSortedSet(lowPriorityURIs);
}