Changeset 30738 in osm for applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CacheControl.java
- Timestamp:
- 2014-10-19T01:27:04+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CacheControl.java
r30737 r30738 1 1 // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others 2 2 package cadastre_fr; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.io.BufferedOutputStream; 7 import java.io.File; 8 import java.io.FileInputStream; 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 import java.io.ObjectInputStream; 12 import java.io.ObjectOutputStream; 13 import java.io.OutputStream; 14 import java.util.ArrayList; 15 import java.util.concurrent.locks.Lock; 16 import java.util.concurrent.locks.ReentrantLock; 17 18 import javax.swing.JDialog; 19 import javax.swing.JOptionPane; 20 21 import org.openstreetmap.josm.Main; 22 3 23 /** 4 24 * This class handles the WMS layer cache mechanism. The design is oriented for a good performance (no … … 10 30 * is inserted before each append and an exception is raised at objects read). 11 31 */ 12 13 import static org.openstreetmap.josm.tools.I18n.tr;14 15 import java.io.*;16 import java.util.ArrayList;17 import java.util.concurrent.locks.Lock;18 import java.util.concurrent.locks.ReentrantLock;19 20 import javax.swing.JDialog;21 import javax.swing.JOptionPane;22 import org.openstreetmap.josm.Main;23 24 32 public class CacheControl implements Runnable { 25 33 … … 32 40 super(out); 33 41 } 42 @Override 34 43 protected void writeStreamHeader() throws IOException { 35 44 reset(); … … 40 49 41 50 public static int cacheSize = 500; 42 43 51 44 52 public WMSLayer wmsLayer = null; … … 141 149 public boolean loadCache(File file, int currentLambertZone) { 142 150 boolean successfulRead = false; 143 FileInputStream fis = null; 144 ObjectInputStream ois = null; 145 try { 146 fis = new FileInputStream(file); 147 ois = new ObjectInputStream(fis); 151 try ( 152 FileInputStream fis = new FileInputStream(file); 153 ObjectInputStream ois = new ObjectInputStream(fis); 154 ) { 148 155 successfulRead = wmsLayer.read(file, ois, currentLambertZone); 149 156 } catch (Exception ex) { … … 151 158 JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE); 152 159 return false; 153 } finally {154 try {155 ois.close();156 fis.close();157 } catch (Exception e) {158 e.printStackTrace();159 }160 160 } 161 161 if (successfulRead && wmsLayer.isRaster()) { … … 165 165 return successfulRead; 166 166 } 167 168 167 169 168 public synchronized void saveCache(GeorefImage image) { … … 177 176 * Thread saving the grabbed images in background. 178 177 */ 178 @Override 179 179 public synchronized void run() { 180 180 for (;;) { … … 186 186 try { 187 187 if (file.exists()) { 188 ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend( 189 new BufferedOutputStream(new FileOutputStream(file, true))); 190 for (int i=0; i < size; i++) { 191 oos.writeObject(imagesToSave.get(i)); 188 try (ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend( 189 new BufferedOutputStream(new FileOutputStream(file, true)))) { 190 for (int i=0; i < size; i++) { 191 oos.writeObject(imagesToSave.get(i)); 192 } 192 193 } 193 oos.close();194 194 } else { 195 ObjectOutputStream oos = new ObjectOutputStream( 196 new BufferedOutputStream(new FileOutputStream(file))); 197 wmsLayer.write(file, oos); 198 for (int i=0; i < size; i++) { 199 oos.writeObject(imagesToSave.get(i)); 195 try (ObjectOutputStream oos = new ObjectOutputStream( 196 new BufferedOutputStream(new FileOutputStream(file)))) { 197 wmsLayer.write(file, oos); 198 for (int i=0; i < size; i++) { 199 oos.writeObject(imagesToSave.get(i)); 200 } 200 201 } 201 oos.close();202 202 } 203 203 } catch (IOException e) { 204 e.printStackTrace(System.out);204 Main.error(e); 205 205 } 206 206 imagesLock.lock();
Note:
See TracChangeset
for help on using the changeset viewer.