byte[] getJar(String address) { // System.out.println("getJar: "+address); byte[] data; try { URL url = new URL(address); IJ.showStatus("Connecting to " + IJ.URL); URLConnection uc = url.openConnection(); int len = uc.getContentLength(); if (IJ.debugMode) IJ.log("Updater (url): " + address + " " + len); if (len <= 0) return null; String name = address.contains("wsr") ? "daily build (" : "ij.jar ("; IJ.showStatus("Downloading " + name + IJ.d2s((double) len / 1048576, 1) + "MB)"); InputStream in = uc.getInputStream(); data = new byte[len]; int n = 0; while (n < len) { int count = in.read(data, n, len - n); if (count < 0) throw new EOFException(); n += count; IJ.showProgress(n, len); } in.close(); } catch (IOException e) { if (IJ.debugMode) IJ.log("" + e); return null; } if (IJ.debugMode) IJ.wait(6000); return data; }
/** Opens a stack of images. */ ImagePlus openStack(ColorModel cm, boolean show) { ImageStack stack = new ImageStack(fi.width, fi.height, cm); long skip = fi.getOffset(); Object pixels; try { ImageReader reader = new ImageReader(fi); InputStream is = createInputStream(fi); if (is == null) return null; IJ.resetEscape(); for (int i = 1; i <= fi.nImages; i++) { if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages); if (IJ.escapePressed()) { IJ.beep(); IJ.showProgress(1.0); silentMode = false; return null; } pixels = reader.readPixels(is, skip); if (pixels == null) break; stack.addSlice(null, pixels); skip = fi.gapBetweenImages; if (!silentMode) IJ.showProgress(i, fi.nImages); } is.close(); } catch (Exception e) { IJ.log("" + e); } catch (OutOfMemoryError e) { IJ.outOfMemory(fi.fileName); stack.trim(); } if (!silentMode) IJ.showProgress(1.0); if (stack.getSize() == 0) return null; if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) { for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1); } ImagePlus imp = new ImagePlus(fi.fileName, stack); if (fi.info != null) imp.setProperty("Info", fi.info); if (show) imp.show(); imp.setFileInfo(fi); setCalibration(imp); ImageProcessor ip = imp.getProcessor(); if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank setStackDisplayRange(imp); if (!silentMode) IJ.showProgress(1.0); silentMode = false; return imp; }
public static byte[] download(String urlString, String name) { int maxLength = 52428800; // 50MB URL url = null; boolean unknownLength = false; byte[] data = null; ; int n = 0; try { url = new URL(urlString); if (IJ.debugMode) IJ.log("PluginInstaller: " + urlString + " " + url); if (url == null) return null; URLConnection uc = url.openConnection(); int len = uc.getContentLength(); unknownLength = len < 0; if (unknownLength) len = maxLength; if (name != null) IJ.showStatus("Downloading " + url.getFile()); InputStream in = uc.getInputStream(); data = new byte[len]; int lenk = len / 1024; while (n < len) { int count = in.read(data, n, len - n); if (count < 0) break; n += count; if (name != null) IJ.showStatus("Downloading " + name + " (" + (n / 1024) + "/" + lenk + "k)"); IJ.showProgress(n, len); } in.close(); } catch (Exception e) { String msg = "" + e; if (!msg.contains("://")) msg += "\n " + urlString; IJ.error("Plugin Installer", msg); return null; } finally { IJ.showProgress(1.0); } if (name != null) IJ.showStatus(""); if (unknownLength) { byte[] data2 = data; data = new byte[n]; for (int i = 0; i < n; i++) data[i] = data2[i]; } return data; }
byte[] getJar(String address) { byte[] data; boolean gte133 = version().compareTo("1.33u") >= 0; try { URL url = new URL(address); URLConnection uc = url.openConnection(); int len = uc.getContentLength(); String name = address.endsWith("ij/ij.jar") ? "daily build" : "ij.jar"; IJ.showStatus("Downloading ij.jar (" + IJ.d2s((double) len / 1048576, 1) + "MB)"); InputStream in = uc.getInputStream(); data = new byte[len]; int n = 0; while (n < len) { int count = in.read(data, n, len - n); if (count < 0) throw new EOFException(); n += count; if (gte133) IJ.showProgress(n, len); } in.close(); } catch (IOException e) { return null; } return data; }
private void showProgress(int current, int last) { if (showProgressBar && (System.currentTimeMillis() - startTime) > 500L) IJ.showProgress(current, last); }