Class ShpFiles
This class has methods for performing actions on the files. Currently mainly for obtaining read and write channels and streams. But in the future a move method may be introduced.
Note: The method that require locks (such as getInputStream()) will automatically acquire locks and the javadocs
should document how to release the lock. Therefore the methods acquireRead(ShpFileType, FileReader)
and
acquireWrite(ShpFileType, FileWriter)
svn
- Author:
- jesse
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSearches for all the files and adds then to the map of files.Searches for all the files and adds then to the map of files.Searches for all the files and adds then to the map of files.Searches for all the files and adds then to the map of files. -
Method Summary
Modifier and TypeMethodDescriptionacquireRead
(ShpFileType type, FileReader requestor) Acquire a URL for read only purposes.acquireReadFile
(ShpFileType type, FileReader requestor) Acquire a File for read only purposes.acquireWrite
(ShpFileType type, FileWriter requestor) Acquire a URL for read and write purposes.acquireWriteFile
(ShpFileType type, FileWriter requestor) Acquire a File for read and write purposes.boolean
delete()
Delete all the shapefile files.void
dispose()
boolean
exists
(ShpFileType fileType) Returns true if the file exists.protected void
finalize()
This verifies that this class has been closed correctly (nothing locking)get
(ShpFileType type) Returns the string form of the url that identifies the file indicated by the type parameter or null if it is known that the file does not exist.Returns the URLs (in string form) of all the files for the shapefile datastore.getInputStream
(ShpFileType type, FileReader requestor) Opens a input stream for the indicated file.getOutputStream
(ShpFileType type, FileWriter requestor) Opens a output stream for the indicated file.getReadChannel
(ShpFileType type, FileReader requestor) Obtain a ReadableByteChannel from the given URL.getStorageFile
(ShpFileType type) Obtains a Storage file for the type indicated.getWriteChannel
(ShpFileType type, FileWriter requestor) Obtain a WritableByteChannel from the given URL.boolean
isGz()
boolean
isLocal()
Determine if the location of this shapefile is local or remote.boolean
Returns the status of the memory map cache.boolean
Returns true if the files are writablevoid
logCurrentLockers
(Level logLevel) Writes to the log all the lockers and when they were constructed.int
Returns the number of locks on the current set of shapefile files.void
setMemoryMapCacheEnabled
(boolean memoryMapCacheEnabled) Enables the memory map cache.tryAcquireRead
(ShpFileType type, FileReader requestor) Tries to acquire a URL for read only purposes.tryAcquireWrite
(ShpFileType type, FileWriter requestor) Tries to acquire a URL for read/write purposes.void
unlockRead
(File file, FileReader requestor) Unlocks a read lock.void
unlockRead
(URL url, FileReader requestor) Unlocks a read lock.void
unlockWrite
(File file, FileWriter requestor) Unlocks a write lock.void
unlockWrite
(URL url, FileWriter requestor) Unlocks a write lock.
-
Field Details
-
DEFAULT_SKIP_SCAN
public static final boolean DEFAULT_SKIP_SCAN- See Also:
-
-
Constructor Details
-
ShpFiles
- Throws:
MalformedURLException
-
ShpFiles
Searches for all the files and adds then to the map of files.- Parameters:
fileName
- the filename or url of any one of the shapefile filesskipScan
- skip the scan for alternative shapefile extensions (i.e. .SHP, .shp.XML, ...)- Throws:
MalformedURLException
- if it isn't possible to create a URL from string. It will be used to create a file and create a URL from that if both fail this exception is thrown
-
ShpFiles
Searches for all the files and adds then to the map of files.- Parameters:
file
- any one of the shapefile files- Throws:
FileNotFoundException
- if the shapefile associated with file is not foundMalformedURLException
-
ShpFiles
- Throws:
MalformedURLException
-
ShpFiles
Searches for all the files and adds then to the map of files.- Parameters:
url
- any one of the shapefile files- Throws:
IllegalArgumentException
-
ShpFiles
Searches for all the files and adds then to the map of files.- Parameters:
url
- any one of the shapefile filesskipScan
- avoid searching for other extensions when missing.- Throws:
IllegalArgumentException
-
-
Method Details
-
finalize
This verifies that this class has been closed correctly (nothing locking) -
dispose
public void dispose() -
logCurrentLockers
Writes to the log all the lockers and when they were constructed.- Parameters:
logLevel
- the level at which to log.
-
getFileNames
Returns the URLs (in string form) of all the files for the shapefile datastore.- Returns:
- the URLs (in string form) of all the files for the shapefile datastore.
-
get
Returns the string form of the url that identifies the file indicated by the type parameter or null if it is known that the file does not exist.Note: a URL should NOT be constructed from the string instead the URL should be obtained through calling one of the aquireLock methods.
- Parameters:
type
- indicates the type of file the caller is interested in.- Returns:
- the string form of the url that identifies the file indicated by the type parameter or null if it is known that the file does not exist.
-
numberOfLocks
public int numberOfLocks()Returns the number of locks on the current set of shapefile files. This is not thread safe so do not count on it to have a completely accurate picture but it can be useful debugging- Returns:
- the number of locks on the current set of shapefile files.
-
acquireReadFile
Acquire a File for read only purposes. It is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the File. The same object must release the lock and is also used for debugging.- Returns:
- the File type requested
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
acquireRead
Acquire a URL for read only purposes. It is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the URL. The same object must release the lock and is also used for debugging.- Returns:
- the URL to the file of the type requested
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
tryAcquireRead
Tries to acquire a URL for read only purposes. Returns null if the acquire failed or if the file does not.It is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.
- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the URL. The same object must release the lock and is also used for debugging.- Returns:
- A result object containing the URL or the reason for the failure.
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
unlockRead
Unlocks a read lock. The file and requestor must be the the same as the one of the lockers.- Parameters:
file
- file that was lockedrequestor
- the class that requested the file
-
unlockRead
Unlocks a read lock. The url and requestor must be the the same as the one of the lockers.- Parameters:
url
- url that was lockedrequestor
- the class that requested the url
-
acquireWriteFile
Acquire a File for read and write purposes.It is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.
- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the File. The same object must release the lock and is also used for debugging.- Returns:
- the File to the file of the type requested
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
acquireWrite
Acquire a URL for read and write purposes.It is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.
- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the URL. The same object must release the lock and is also used for debugging.- Returns:
- the URL to the file of the type requested
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
tryAcquireWrite
Tries to acquire a URL for read/write purposes. Returns null if the acquire failed or if the file does not existIt is recommended that get*Stream or get*Channel methods are used when reading or writing to the file is desired.
- Parameters:
type
- the type of the file desired.requestor
- the object that is requesting the URL. The same object must release the lock and is also used for debugging.- Returns:
- A result object containing the URL or the reason for the failure.
- See Also:
-
getInputStream(ShpFileType, FileReader)
getReadChannel(ShpFileType, FileReader)
#getWriteChannel(ShpFileType, FileReader)
-
unlockWrite
Unlocks a write lock. The file and requestor must be the the same as the one of the lockers.- Parameters:
file
- file that was lockedrequestor
- the class that requested the file
-
unlockWrite
Unlocks a write lock. The requestor must be have previously obtained a lock for the url.- Parameters:
url
- url that was lockedrequestor
- the class that requested the url
-
isLocal
public boolean isLocal()Determine if the location of this shapefile is local or remote.- Returns:
- true if local, false if remote
-
isGz
public boolean isGz()- Returns:
- whether the files are gzip-ped.
-
isWritable
public boolean isWritable()Returns true if the files are writable -
delete
public boolean delete()Delete all the shapefile files. If the files are not local or the one files cannot be deleted return false. -
getInputStream
Opens a input stream for the indicated file. A read lock is requested at the method call and released on close.- Parameters:
type
- the type of file to open the stream to.requestor
- the object requesting the stream- Returns:
- an input stream
- Throws:
IOException
- if a problem occurred opening the stream.
-
getOutputStream
Opens a output stream for the indicated file. A write lock is requested at the method call and released on close.- Parameters:
type
- the type of file to open the stream to.requestor
- the object requesting the stream- Returns:
- an output stream
- Throws:
IOException
- if a problem occurred opening the stream.
-
getReadChannel
public ReadableByteChannel getReadChannel(ShpFileType type, FileReader requestor) throws IOException Obtain a ReadableByteChannel from the given URL. If the url protocol is file, a FileChannel will be returned. Otherwise a generic channel will be obtained from the urls input stream.A read lock is obtained when this method is called and released when the channel is closed.
- Parameters:
type
- the type of file to open the channel to.requestor
- the object requesting the channel- Throws:
IOException
-
getWriteChannel
public WritableByteChannel getWriteChannel(ShpFileType type, FileWriter requestor) throws IOException Obtain a WritableByteChannel from the given URL. If the url protocol is file, a FileChannel will be returned. Currently, this method will return a generic channel for remote urls, however both shape and dbf writing can only occur with a local FileChannel channel.A write lock is obtained when this method is called and released when the channel is closed.
- Parameters:
type
- the type of file to open the stream to.requestor
- the object requesting the stream- Returns:
- a WritableByteChannel for the provided file type
- Throws:
IOException
- if there is an error opening the stream
-
getStorageFile
Obtains a Storage file for the type indicated. An id is provided so that the same file can be obtained at a later time with just the id- Parameters:
type
- the type of file to create and return- Returns:
- StorageFile
- Throws:
IOException
- if temporary files cannot be created
-
getTypeName
-
isMemoryMapCacheEnabled
public boolean isMemoryMapCacheEnabled()Returns the status of the memory map cache. When enabled the memory mapped portions of the files are cached and shared (giving each thread a clone of it) -
setMemoryMapCacheEnabled
public void setMemoryMapCacheEnabled(boolean memoryMapCacheEnabled) Enables the memory map cache. When enabled the memory mapped portions of the files are cached and shared (giving each thread a clone of it) -
exists
Returns true if the file exists. Throws an exception if the file is not local.- Parameters:
fileType
- the type of file to check existance for.- Returns:
- true if the file exists.
- Throws:
IllegalArgumentException
- if the files are not local.
-