addressed codacy issues and review comment

This commit is contained in:
Kelly Kelly 2019-11-26 13:33:45 -05:00
parent 56d89fb4a2
commit bfc86b8c76
2 changed files with 51 additions and 29 deletions

View File

@ -161,9 +161,9 @@ final class MBTilesFileConnector {
*/
private final class ConnectionPool {
private final int POOL_SIZE = 5;
private static final int POOL_SIZE = 5;
private final List<Connection> connectionPool;
private final List<Connection> poolConnections;
private final List<Connection> usedConnections;
/**
@ -178,9 +178,9 @@ final class MBTilesFileConnector {
String path = filePath.replaceAll("\\\\", "/");
String url = String.format(DB_URL, path);
connectionPool = new ArrayList<>(POOL_SIZE);
poolConnections = new ArrayList<>(POOL_SIZE);
for (int idx = 0; idx < POOL_SIZE; idx++) {
connectionPool.add(DriverManager.getConnection(url));
poolConnections.add(DriverManager.getConnection(url));
}
}
@ -192,8 +192,8 @@ final class MBTilesFileConnector {
*/
synchronized Connection getConnection() {
Connection connection = null;
if (!connectionPool.isEmpty()) {
connection = connectionPool.remove(connectionPool.size() - 1);
if (!poolConnections.isEmpty()) {
connection = poolConnections.remove(poolConnections.size() - 1);
usedConnections.add(connection);
}
return connection;
@ -211,7 +211,7 @@ final class MBTilesFileConnector {
*/
synchronized boolean releaseConnection(Connection connection) {
if (usedConnections.contains(connection)) {
connectionPool.add(connection);
poolConnections.add(connection);
return usedConnections.remove(connection);
}

View File

@ -20,6 +20,8 @@ package org.sleuthkit.autopsy.geolocation;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Comparator;
@ -52,7 +54,7 @@ final class MBTilesTileFactory extends TileFactory {
private static final Logger logger = Logger.getLogger(MBTilesTileFactory.class.getName());
private volatile int pendingTiles = 0;
private final int threadPoolSize = 4;
private static final int THREAD_POOL_SIZE = 4;
private ExecutorService service;
private final Map<String, Tile> tileMap;
@ -188,7 +190,7 @@ final class MBTilesTileFactory extends TileFactory {
synchronized ExecutorService getService() {
if (service == null) {
// System.out.println("creating an executor service with a threadpool of size " + threadPoolSize);
service = Executors.newFixedThreadPool(threadPoolSize, new ThreadFactory() {
service = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new ThreadFactory() {
private int count = 0;
@Override
@ -294,27 +296,10 @@ final class MBTilesTileFactory extends TileFactory {
URI uri = getURI(tile);
BufferedImage img = cache.get(uri);
if (img == null) {
byte[] bimg = connector.getTileBytes(uri.toString());
if (bimg != null && bimg.length > 0) {
img = ImageIO.read(new ByteArrayInputStream(bimg));
cache.put(uri, bimg, img);
img = cache.get(uri);
img = getImage(uri);
}
}
if (img == null) {
logger.log(Level.INFO, String.format("Failed to load: %s", uri));
} else {
final BufferedImage image = img;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
if (tile instanceof MBTilesTile) {
((MBTilesTile) tile).setImage(image);
}
pendingTiles--;
fireTileLoadedEvent(tile);
}
});
if (img != null) {
addImageToTile(tile, img);
}
} catch (OutOfMemoryError memErr) {
cache.needMoreMemory();
@ -329,4 +314,41 @@ final class MBTilesTileFactory extends TileFactory {
tile.setLoading(false);
}
}
/**
*
* @param uri
* @return
* @throws IOException
* @throws GeoLocationDataException
*/
private BufferedImage getImage(URI uri ) throws IOException, GeoLocationDataException{
BufferedImage img = null;
byte[] bimg = connector.getTileBytes(uri.toString());
if (bimg != null && bimg.length > 0) {
img = ImageIO.read(new ByteArrayInputStream(bimg));
cache.put(uri, bimg, img);
}
return img;
}
/**
*
* @param tile
* @param image
* @throws InterruptedException
* @throws InvocationTargetException
*/
private void addImageToTile(Tile tile, BufferedImage image) throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
if (tile instanceof MBTilesTile) {
((MBTilesTile) tile).setImage(image);
}
pendingTiles--;
fireTileLoadedEvent(tile);
}
});
}
}