Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
The ConvertAPI helps converting various file formats.
Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes.
Merge, Encrypt, Split, Repair and Decrypt PDF files.
And many others files manipulations.
In just few minutes you can integrate it into your application and use it easily.
The ConvertAPI client library makes it easier to use the Convert API from your Java 8 projects without having to
build your own API calls.
/*
* Copyright (c) 2018 Tarokun LLC. All rights reserved.
*
* This file is part of BlobBase.
*
* 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.
*
*/
package org.blobbase;
import org.blobbase.exeptions.CorruptDatabaseException;
import org.blobbase.exeptions.DeleteFileException;
import org.blobbase.exeptions.DuplicateKeyException;
import org.blobbase.exeptions.FileInUseException;
import org.blobbase.exeptions.IllegalDatabaseModificationException;
import org.blobbase.exeptions.MakeDirectoryException;
import org.blobbase.exeptions.OrphanedFileException;
import org.blobbase.exeptions.RelocateFileException;
import org.blobbase.exeptions.CreateFileException;
import org.blobbase.exeptions.KeyNotFoundException;
import org.blobbase.exeptions.RenameFileException;
import org.blobbase.utils.FileUtils;
import org.blobbase.utils.LockFile;
import org.blobbase.utils.PrimeNumber;
import org.blobbase.utils.Sha1Util;
import org.blobbase.utils.ThreadUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Create an abstract storage mechanism used for mapping keys to binary large
* objects (BLOB) stored on the file system.
*
* The key mapping is accomplished algorithmically by creating a hash out of the
* key and mapping it to a slot (file) in a directory. Key collisions are
* handled by the creation of subdirectories to handle the collided keys and
* files.
*
*
*
* Example 1: Create file
*
* // location of database
* File root = new File("./myDb");
*
* // instantiate BlobBase
* BlobBase blobBase = BlobBase.getInstance(root);
*
* // create a file in database
* File file = blobBase.createFile("my key");
*
* // I can now use 'file' normally to write data.
* FileOutputStream fOut = new FileOutputStream(file);
*
* Example 2: Read file
*
* File root = new File("./myDb");
*
* // setup location
* BlobBase blobBase = BlobBase.getInstance(root);
*
* // get file from database
* File file = blobBase.getFile("my key");
* if (file == null)
* {
* System.out.println("file with key was not found");
* }
* else
* {
* // I can now use file object returned to read data
* FileInputStream in = new FileInputStream(file);
* ... do something with data ....
* }
*
* Example 2: Delete file
*
* File root = new File("./myDb");
*
* // setup location
* BlobBase blobBase = new BlobBase(root);
*
* // get file from database
* File file = blobBase.getFile("my key");
* if (file != null)
* {
* // delete just like any other file
* file.delete();
* }
*
*
*
*
* Using Compression
*
* BlobBase can optionally compress files stored in it. Do do so create a
* BlobBase and then set the compression flag.
* < br />
* You can turn compression on later after creating a database but beware -
* Compression once enabled can not be disabled.
*
*
*
* // set location of database
* File root = new File("./myDb");
*
* // instantiate BlobBase
* BlobBase blobBase = BlobBase.getInstance(root);
* // turn compression on
* blobBase.setCompressed();
*
* OutputStream blobOut = BlobBaseOutputStream(blobBase, "myId");
* blobOut.write("mydata".getBytes()); // this will be automatically compressed when written
*
* // To read compressed data transparently
* InputStream blobIn = new BlobBaseInputStream(blobBase,"myId");
* int b = blobIn.read(); // this byte will be uncompressed automatically wwhen read.
*
*
*/
public class BlobBase implements Serializable, Comparable
{
final private static long serialVersionUID = 1L;
final private static String MAP_FILE = ".key.map";
private File rootDir;
private int startingPrime = 0;
private int startingOffset = 1;
// todo: parameterise these two options.
private boolean bMoveFiles = true;
// todo: implement this
private int maxFiles; // should be specified when bMoveFiles is false. We will use this value to compute required directory structure.
private boolean compressed; // if true we are compressing files
private int lockWaitSleep = 500;
private int maxCreateFileAttempts = 10;
private transient ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private static HashMap instanceMap = new HashMap();
public static synchronized BlobBase getInstance(File rootDir) throws IOException
{
BlobBase bBase = instanceMap.get(rootDir);
if (bBase == null)
{
bBase = new BlobBase(rootDir);
instanceMap.put(rootDir, bBase);
}
return bBase;
}
public static synchronized BlobBase getInstance(File rootDir, int startingPrime) throws IOException
{
BlobBase bBase = instanceMap.get(rootDir);
if (bBase == null)
{
bBase = new BlobBase(rootDir, startingPrime);
instanceMap.put(rootDir, bBase);
}
return bBase;
}
protected BlobBase(File rootDir) throws IllegalArgumentException, IOException
{
this(rootDir, PrimeNumber.primeNumbers[PrimeNumber.findIndex(8009)]);
}
/**
* Create a FileMap database at location with initial directory capacity.
*
* @param rootDir location of files and directories
* @param startingPrime a prime number that indicates the maximum number of
* files/directories in root directory.
*
* @throws IllegalArgumentException if startingPrime is not a prime number
* (p) in the range of 2 >= p <= 8011
* @throws IOException if an error occurs in the course of setting up
* database
* @throws CorruptDatabaseException if unable to read/parse our '.dbInfo'
* file
* @throws IllegalDatabaseModificationException if you specified a different
* startingPrime for a previously created database
*/
protected BlobBase(File rootDir, int startingPrime) throws IllegalArgumentException, IOException, CorruptDatabaseException, IllegalDatabaseModificationException
{
if (rootDir == null || rootDir.isFile())
{
throw new IllegalArgumentException();
}
this.rootDir = rootDir;
if (!PrimeNumber.isPrime(startingPrime) || startingPrime < 2 || startingPrime > PrimeNumber.primeNumbers[PrimeNumber.primeNumbers.length - 1])
{
throw new IllegalArgumentException(Integer.toString(startingPrime));
}
this.startingPrime = startingPrime;
this.startingOffset = PrimeNumber.findIndex(startingPrime);
synchronized (this.getClass())
{
// initialize root
File dbInfo = getDbInfo();
if (!dbInfo.exists())
{
Properties properties = new Properties();
properties.setProperty("startingPrime", Integer.toString(startingPrime));
FileOutputStream fOut = new FileOutputStream(dbInfo);
properties.store(fOut, "DB Settings - DO NOT MODIFY UNLESS YOU KNOW WHAT YOU ARE DOING!!!");
fOut.close();
} else
{
Properties properties = getProperties(dbInfo);
try
{
this.startingPrime = Integer.parseInt(properties.getProperty("startingPrime"));
this.startingOffset = PrimeNumber.findIndex(this.startingPrime);
compressed = Boolean.parseBoolean(properties.getProperty("compressed", "false"));
} catch (NumberFormatException ne)
{
ne.printStackTrace();
throw new CorruptDatabaseException();
}
}
}
}
/**
* Returns a empty atomically created file.
*
* @param key the key to use for obtaining the file
* @return file created in databaseS
* @throws IOException if an error occurs
* @throws DuplicateKeyException if a file exists with the specified key
* already
* @throws CreateFileException if unable to create file
* @throws FileInUseException if unable to get a file lock
* @throws CorruptDatabaseException if missing .key.map file
*/
public File createFile(Object key) throws IOException, DuplicateKeyException, CreateFileException, FileInUseException, CorruptDatabaseException
{
return createFile(key, 1);
}
@SuppressWarnings("unchecked")
protected File createFile(Object key, int depth) throws IOException, DuplicateKeyException, CreateFileException, FileInUseException, CorruptDatabaseException
{
if (depth > maxCreateFileAttempts)
{
throw new FileInUseException();
}
byte[] hash = Sha1Util.hashBytes(key.toString().getBytes());
File file = null;
try
{
rwLock.writeLock().lock();
boolean isNew = false;
while (!isNew)
{
file = locateFile(key, startingOffset);
if (file == null)
{
// Houston: we have a problem
throw new IOException("could not acquire file");
}
Map