Skip to content

p2p-sync/commons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P2P-Sync Commons

Build Status Coverage Status

Install using Maven

To add this library using Maven, add the following to your pom.xml:

<repositories>
  <repository>
    <id>persistence-mvn-repo</id>
    <url>https://raw.github.com/p2p-sync/commons/mvn-repo/</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>org.rmatil.sync.commons</groupId>
    <artifactId>sync-commons</artifactId>
    <version>0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

Overview

This component provides some common functionalities used in p2p-sync/sync.

Hashing

Multiple methods are provided to hash files, directories or just the content of a byte array. This module supports four different hash functions: MD5, SHA 1, SHA 256 and SHA 512.

Hashing Files or Directories

import org.rmatil.sync.commons.hashing.Hash;
import org.rmatil.sync.commons.hashing.HashingAlgorithm;

import java.io.File;

// ...

  File file = new File("path/to/file.txt");
  String hash1 = Hash.hash(HashingAlgorithm.SHA_1, file);

  File directory = new File("path/to/directory");
  String hash2 = Hash.hash(HashingAlgorithm.SHA_1, directory);

  
// ..

Hashing String Values

import org.rmatil.sync.commons.hashing.Hash;
import org.rmatil.sync.commons.hashing.HashingAlgorithm;

// ...

  String string = "some string to hash";
  String hash = Hash.hash(HashingAlgorithm.SHA_1, string);

  
// ..

Hashing Byte contents

import org.rmatil.sync.commons.hashing.Hash;
import org.rmatil.sync.commons.hashing.HashingAlgorithm;

// ...

  byte[] bytes = new byte[10];
  String hash = Hash.hash(HashingAlgorithm.SHA_1, bytes);

  
// ..

Pair

A simple Pair class is also provided:

import org.rmatil.sync.commons.collection.Pair;

// ...

  Pair<String, Integer> pair = new Pair<>("first", 2);
  pair.getFirst();
  pair.getSecond();
  
// ...

Lists

To check whether a particular list contains an element of a provided class, this module provides the following function:

import org.rmatil.sync.commons.list.Lists;

import java.util.ArrayList;
import java.util.List;

// ...

  List<String> list = new ArrayList<>();
  list.add("some string");

  List<String> entries = Lists.getInstances(list, String.class);

// ...

Naming

Finally, some utility methods used for handling or creating file names are included:

import org.rmatil.sync.commons.path.Naming;

// ...

  boolean isFile = true;
  String clientDeviceId = "Node1";

  // returns relative/path/to/myFile_Node1.txt
  String conflictName = Naming.getConflictFileName("relative/path/to/myFile.txt", isFile, "txt", clientDeviceId);

// ...

  // returns relative/path/to
  String pathWithoutFileName = Naming.getPathWithoutFileName("myFile.txt", "relative/path/to/myFile.txt");

// ...

  // returns relative/path/to
  String parentPath = Naming.getParentPath("relative/path/to/myFile.txt");
  
// ...

License

   Copyright 2015 rmatil

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.