Changeset 8568 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2015-07-04T22:52:23+02:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
r8540 r8568 2 2 package org.openstreetmap.josm.data.cache; 3 3 4 import java.io.ByteArrayOutputStream;5 4 import java.io.FileNotFoundException; 6 5 import java.io.IOException; 7 import java.io.InputStream;8 6 import java.net.HttpURLConnection; 9 7 import java.net.URL; … … 30 28 import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult; 31 29 import org.openstreetmap.josm.data.preferences.IntegerProperty; 30 import org.openstreetmap.josm.tools.Utils; 32 31 33 32 /** … … 350 349 351 350 attributes.setResponseCode(responseCode(urlConn)); 352 byte[] raw = read(urlConn);351 byte[] raw = Utils.readBytesFromStream(urlConn.getInputStream()); 353 352 354 353 if (isResponseLoadable(urlConn.getHeaderFields(), responseCode(urlConn), raw)) { … … 473 472 } 474 473 475 private static byte[] read(URLConnection urlConn) throws IOException {476 InputStream input = urlConn.getInputStream();477 try {478 ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available());479 byte[] buffer = new byte[2048];480 boolean finished = false;481 do {482 int read = input.read(buffer);483 if (read >= 0) {484 bout.write(buffer, 0, read);485 } else {486 finished = true;487 }488 } while (!finished);489 if (bout.size() == 0)490 return null;491 return bout.toByteArray();492 } finally {493 input.close();494 }495 }496 497 474 /** 498 475 * TODO: move to JobFactory -
trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java
r8526 r8568 56 56 SCANEX("scanex"), 57 57 /** A WMS endpoint entry only stores the WMS server info, without layer, which are chosen later by the user. **/ 58 WMS_ENDPOINT("wms_endpoint"); 58 WMS_ENDPOINT("wms_endpoint"), 59 /** WMTS stores GetCapabilities URL. Does not store any information about the layer **/ 60 WMTS("wmts"); 59 61 60 62 -
trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java
r8533 r8568 8 8 import java.util.List; 9 9 import java.util.Map; 10 import java.util.concurrent.ConcurrentHashMap; 10 11 import java.util.regex.Matcher; 11 12 import java.util.regex.Pattern; … … 35 36 public class CustomProjection extends AbstractProjection { 36 37 38 private final static Map<String, Double> UNITS_TO_METERS = getUnitsToMeters(); 39 private final static double METER_PER_UNIT_DEGREE = 2 * Math.PI * 6370997 / 360; 40 37 41 /** 38 42 * pref String that defines the projection … … 45 49 protected String cacheDir; 46 50 protected Bounds bounds; 51 private double metersPerUnit = METER_PER_UNIT_DEGREE; // default to degrees 47 52 48 53 /** … … 89 94 wktext("wktext", false), // ignored 90 95 /** meters, US survey feet, etc. */ 91 units("units", true), // ignored96 units("units", true), 92 97 /** Don't use the /usr/share/proj/proj_def.dat defaults file */ 93 98 no_defs("no_defs", false), 94 99 init("init", true), 100 to_meter("to_meter", true), 95 101 // JOSM extensions, not present in PROJ.4 96 102 wmssrs("wmssrs", true), … … 103 109 104 110 /** Map of all parameters by key */ 105 static final Map<String, Param> paramsByKey = new HashMap<>();111 static final Map<String, Param> paramsByKey = new ConcurrentHashMap<>(); 106 112 static { 107 113 for (Param p : Param.values()) { … … 198 204 if (s != null) { 199 205 this.code = s; 206 } 207 s = parameters.get(Param.units.key); 208 if (s != null) { 209 this.metersPerUnit = UNITS_TO_METERS.get(s); 210 } 211 s = parameters.get(Param.to_meter.key); 212 if (s != null) { 213 this.metersPerUnit = parseDouble(s, Param.to_meter.key); 200 214 } 201 215 } … … 528 542 return name != null ? name : tr("Custom Projection"); 529 543 } 544 545 @Override 546 public double getMetersPerUnit() { 547 return metersPerUnit; 548 } 549 550 private static Map<String, Double> getUnitsToMeters() { 551 Map<String, Double> ret = new ConcurrentHashMap<>(); 552 ret.put("km", 1000d); 553 ret.put("m", 1d); 554 ret.put("dm", 1d/10); 555 ret.put("cm", 1d/100); 556 ret.put("mm", 1d/1000); 557 ret.put("kmi", 1852.0); 558 ret.put("in", 0.0254); 559 ret.put("ft", 0.3048); 560 ret.put("yd", 0.9144); 561 ret.put("mi", 1609.344); 562 ret.put("fathom", 1.8288); 563 ret.put("chain", 20.1168); 564 ret.put("link", 0.201168); 565 ret.put("us-in", 1d/39.37); 566 ret.put("us-ft", 0.304800609601219); 567 ret.put("us-yd", 0.914401828803658); 568 ret.put("us-ch", 20.11684023368047); 569 ret.put("us-mi", 1609.347218694437); 570 ret.put("ind-yd", 0.91439523); 571 ret.put("ind-ft", 0.30479841); 572 ret.put("ind-ch", 20.11669506); 573 ret.put("degree", METER_PER_UNIT_DEGREE); 574 return ret; 575 } 530 576 } -
trunk/src/org/openstreetmap/josm/data/projection/Projection.java
r6069 r8568 68 68 */ 69 69 Bounds getWorldBoundsLatLon(); 70 71 /** 72 * Get the number of meters per unit of this projection. This more 73 * defines the scale of the map, than real conversion of unit to meters 74 * as this value is more less correct only along great circles. 75 * 76 * Used by WMTS to properly scale tiles 77 * @return meters per unit of projection 78 * 79 */ 80 double getMetersPerUnit(); 70 81 } -
trunk/src/org/openstreetmap/josm/data/projection/proj/Proj.java
r7509 r8568 64 64 */ 65 65 double[] invproject(double east, double north); 66 67 66 } -
trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java
r8542 r8568 31 31 import java.util.Set; 32 32 import java.util.concurrent.ConcurrentSkipListSet; 33 import java.util.concurrent.atomic.AtomicInteger; 33 34 34 35 import javax.swing.AbstractAction; … … 44 45 45 46 import org.openstreetmap.gui.jmapviewer.AttributionSupport; 46 import org.openstreetmap.gui.jmapviewer.Coordinate;47 47 import org.openstreetmap.gui.jmapviewer.MemoryTileCache; 48 48 import org.openstreetmap.gui.jmapviewer.OsmTileLoader; … … 51 51 import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader; 52 52 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 53 import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource; 53 54 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 54 55 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; … … 102 103 /** do set autoload when creating a new layer */ 103 104 public static final BooleanProperty PROP_DEFAULT_AUTOLOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true); 104 /** do s et showerrors when creating a new layer*/105 /** do show errors per default */ 105 106 public static final BooleanProperty PROP_DEFAULT_SHOWERRORS = new BooleanProperty(PREFERENCE_PREFIX + ".default_showerrors", true); 106 107 /** minimum zoom level to show to user */ … … 118 119 119 120 private AttributionSupport attribution = new AttributionSupport(); 120 Tile showMetadataTile;121 121 122 122 // needed public access for session exporter … … 130 130 protected TileCache tileCache; 131 131 protected TileSource tileSource; 132 //protected tileMatrix;133 132 protected TileLoader tileLoader; 134 133 … … 154 153 protected abstract TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException; 155 154 156 protected abstract Map<String, String> getHeaders(TileSource tileSource); 157 158 protected void initTileSource(TileSource tileMatrix) { 159 this.tileSource = tileMatrix; 160 attribution.initialize(tileMatrix); 155 protected Map<String, String> getHeaders(TileSource tileSource) { 156 if (tileSource instanceof TemplatedTileSource) { 157 return ((TemplatedTileSource) tileSource).getHeaders(); 158 } 159 return null; 160 } 161 162 protected void initTileSource(TileSource tileSource) { 163 attribution.initialize(tileSource); 161 164 162 165 currentZoomLevel = getBestZoom(); 163 166 164 Map<String, String> headers = getHeaders(tile Matrix);167 Map<String, String> headers = getHeaders(tileSource); 165 168 166 169 tileLoader = getTileLoaderFactory().makeTileLoader(this, headers); … … 247 250 } 248 251 249 pr ivateint getBestZoom() {252 protected int getBestZoom() { 250 253 double factor = getScaleFactor(1); // check the ratio between area of tilesize at zoom 1 to current view 251 254 double result = Math.log(factor)/Math.log(2)/2+1; … … 273 276 274 277 private final class ShowTileInfoAction extends AbstractAction { 275 private final TileHolder clickedTileHolder;278 private transient final TileHolder clickedTileHolder; 276 279 277 280 private ShowTileInfoAction(TileHolder clickedTileHolder) { … … 349 352 } 350 353 354 @Override 351 355 public Component createMenuComponent() { 352 356 JCheckBoxMenuItem item = new JCheckBoxMenuItem(this); … … 414 418 @Override 415 419 public void actionPerformed(ActionEvent ae) { 416 double new _factor = Math.sqrt(getScaleFactor(currentZoomLevel));417 Main.map.mapView.zoomToFactor(new _factor);420 double newFactor = Math.sqrt(getScaleFactor(currentZoomLevel)); 421 Main.map.mapView.zoomToFactor(newFactor); 418 422 redraw(); 419 423 } … … 451 455 @Override 452 456 public void hookUpMapView() { 453 initTileSource(getTileSource(info));457 this.tileSource = getTileSource(info); 454 458 projectionChanged(null, Main.getProjection()); // check if projection is supported 459 initTileSource(this.tileSource); 455 460 456 461 // keep them final here, so we avoid namespace clutter in the class … … 538 543 @Override 539 544 protected void finish() { 545 // empty - flush is instaneus 540 546 } 541 547 542 548 @Override 543 549 protected void cancel() { 550 // empty - flush is instaneus 544 551 } 545 552 }.run(); … … 642 649 */ 643 650 public static void setMaxZoomLvl(int maxZoomLvl) { 644 maxZoomLvl = checkMaxZoomLvl(maxZoomLvl, null); 645 PROP_MAX_ZOOM_LVL.put(maxZoomLvl); 651 PROP_MAX_ZOOM_LVL.put(checkMaxZoomLvl(maxZoomLvl, null)); 646 652 } 647 653 … … 651 657 */ 652 658 public static void setMinZoomLvl(int minZoomLvl) { 653 minZoomLvl = checkMinZoomLvl(minZoomLvl, null); 654 PROP_MIN_ZOOM_LVL.put(minZoomLvl); 659 PROP_MIN_ZOOM_LVL.put(checkMinZoomLvl(minZoomLvl, null)); 655 660 } 656 661 … … 742 747 */ 743 748 public boolean decreaseZoomLevel() { 744 //int minZoom = this.getMinZoomLvl();745 749 if (zoomDecreaseAllowed()) { 746 750 if (Main.isDebugEnabled()) { … … 750 754 zoomChanged(); 751 755 } else { 752 /*Main.debug("Current zoom level could not be decreased. Min. zoom level "+minZoom+" reached.");*/753 756 return false; 754 757 } … … 786 789 */ 787 790 private Tile getTile(int x, int y, int zoom) { 788 int max = (1 << zoom); 789 if (x < 0 || x >= max || y < 0 || y >= max) 791 if (x < 0 || x >= tileSource.getTileXMax(zoom) || y < 0 || y >= tileSource.getTileYMax(zoom)) 790 792 return null; 791 793 return tileCache.getTile(tileSource, x, y, zoom); … … 1003 1005 } 1004 1006 1005 /*int xCursor = -1;1007 int xCursor = -1; 1006 1008 int yCursor = -1; 1007 if ( PROP_DRAW_DEBUG.get()) {1009 if (Main.isDebugEnabled()) { 1008 1010 if (yCursor < t.getYtile()) { 1009 1011 if (t.getYtile() % 32 == 31) { … … 1026 1028 xCursor = t.getXtile(); 1027 1029 } 1028 } */1030 } 1029 1031 } 1030 1032 … … 1042 1044 } 1043 1045 1044 private Coordinate getShiftedCoord(EastNorth en) { 1045 LatLon ll = getShiftedLatLon(en); 1046 return new Coordinate(ll.lat(), ll.lon()); 1046 private ICoordinate getShiftedCoord(EastNorth en) { 1047 return getShiftedLatLon(en).toCoordinate(); 1047 1048 } 1048 1049 … … 1117 1118 1118 1119 private int size() { 1119 int x _span = x1 - x0 + 1;1120 int y _span = y1 - y0 + 1;1121 return x _span * y_span;1120 int xSpan = x1 - x0 + 1; 1121 int ySpan = y1 - y0 + 1; 1122 return xSpan * ySpan; 1122 1123 } 1123 1124 … … 1355 1356 } 1356 1357 int newzoom = displayZoomLevel + zoomOffset; 1357 if (newzoom < MIN_ZOOM) {1358 if (newzoom < getMinZoomLvl() || newzoom > getMaxZoomLvl()) { 1358 1359 continue; 1359 1360 } … … 1513 1514 public class PrecacheTask implements TileLoaderListener { 1514 1515 private final ProgressMonitor progressMonitor; 1515 private volatileint totalCount;1516 private volatile int processedCount = 0;1517 private TileLoader tileLoader;1516 private int totalCount; 1517 private AtomicInteger processedCount = new AtomicInteger(0); 1518 private final TileLoader tileLoader; 1518 1519 1519 1520 /** … … 1534 1535 */ 1535 1536 public boolean isFinished() { 1536 return processedCount >= totalCount;1537 return processedCount.get() >= totalCount; 1537 1538 } 1538 1539 … … 1556 1557 public void tileLoadingFinished(Tile tile, boolean success) { 1557 1558 if (success) { 1558 this.processedCount++;1559 int processed = this.processedCount.incrementAndGet(); 1559 1560 this.progressMonitor.worked(1); 1560 this.progressMonitor.setCustomText(tr("Downloaded {0}/{1} tiles", processed Count, totalCount));1561 this.progressMonitor.setCustomText(tr("Downloaded {0}/{1} tiles", processed, totalCount)); 1561 1562 } 1562 1563 } -
trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java
r8540 r8568 41 41 import org.openstreetmap.josm.data.ProjectionBounds; 42 42 import org.openstreetmap.josm.data.imagery.ImageryInfo; 43 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;44 43 import org.openstreetmap.josm.data.imagery.OffsetBookmark; 45 44 import org.openstreetmap.josm.data.preferences.ColorProperty; … … 153 152 154 153 public static ImageryLayer create(ImageryInfo info) { 155 ImageryType type = info.getImageryType(); 156 if (type == ImageryType.WMS || type == ImageryType.HTML) 154 switch(info.getImageryType()) { 155 case WMS: 156 case HTML: 157 157 return new WMSLayer(info); 158 else if (type == ImageryType.TMS || type == ImageryType.BING || type == ImageryType.SCANEX) 158 case WMTS: 159 return new WMTSLayer(info); 160 case TMS: 161 case BING: 162 case SCANEX: 159 163 return new TMSLayer(info); 160 else throw new AssertionError(); 164 default: 165 throw new AssertionError(tr("Unsupported imagery type: {0}", info.getImageryType())); 166 } 161 167 } 162 168 -
trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java
r8540 r8568 78 78 } 79 79 80 @Override81 protected Map<String, String> getHeaders(TileSource tileSource) {82 if (tileSource instanceof TemplatedTMSTileSource) {83 return ((TemplatedTMSTileSource) tileSource).getHeaders();84 }85 return null;86 }87 88 80 /** 89 81 * Creates and returns a new TileSource instance depending on the {@link ImageryType} -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddImageryPanel.java
r8513 r8568 16 16 17 17 import org.openstreetmap.josm.data.imagery.ImageryInfo; 18 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 18 19 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 19 20 import org.openstreetmap.josm.gui.widgets.JosmTextField; … … 86 87 } 87 88 89 protected static String sanitize(String s, ImageryType type) { 90 String ret = s; 91 String imageryType = type.getTypeString() + ":"; 92 if (ret.startsWith(imageryType)) { 93 // remove ImageryType from URL 94 ret = ret.substring(imageryType.length()); 95 } 96 return sanitize(ret); 97 } 98 88 99 protected final String getImageryName() { 89 100 return sanitize(name.getText()); -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddTMSLayerPanel.java
r8390 r8568 11 11 12 12 import org.openstreetmap.josm.data.imagery.ImageryInfo; 13 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 13 14 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 14 15 import org.openstreetmap.josm.gui.widgets.JosmTextField; … … 71 72 a.append('[').append(z).append(']'); 72 73 } 73 a.append(':').append( getImageryRawUrl());74 a.append(':').append(sanitize(getImageryRawUrl(), ImageryType.TMS)); 74 75 return a.toString(); 75 76 } -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java
r8426 r8568 178 178 179 179 protected final String getWmsUrl() { 180 return sanitize(wmsUrl.getText() );180 return sanitize(wmsUrl.getText(), ImageryInfo.ImageryType.WMS); 181 181 } 182 182 -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java
r8510 r8568 389 389 activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.WMS)); 390 390 activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.TMS)); 391 activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.WMTS)); 391 392 //activeToolbar.add(edit); TODO 392 393 activeToolbar.add(remove); … … 482 483 putValue(SHORT_DESCRIPTION, tr("Add a new {0} entry by entering the URL", type.toString())); 483 484 String icon = /* ICON(dialogs/) */ "add"; 484 if (ImageryInfo.ImageryType.WMS.equals(type)) 485 switch (type) { 486 case WMS: 485 487 icon = /* ICON(dialogs/) */ "add_wms"; 486 else if (ImageryInfo.ImageryType.TMS.equals(type)) 488 break; 489 case TMS: 487 490 icon = /* ICON(dialogs/) */ "add_tms"; 491 break; 492 case WMTS: 493 icon = /* ICON(dialogs/) */ "add_wmts"; 494 break; 495 default: 496 break; 497 } 488 498 putValue(SMALL_ICON, ImageProvider.get("dialogs", icon)); 489 499 this.type = type; … … 493 503 public void actionPerformed(ActionEvent evt) { 494 504 final AddImageryPanel p; 495 if (ImageryInfo.ImageryType.WMS.equals(type)) { 505 switch (type) { 506 case WMS: 496 507 p = new AddWMSLayerPanel(); 497 } else if (ImageryInfo.ImageryType.TMS.equals(type)) { 508 break; 509 case TMS: 498 510 p = new AddTMSLayerPanel(); 499 } else { 511 break; 512 case WMTS: 513 p = new AddWMTSLayerPanel(); 514 break; 515 default: 500 516 throw new IllegalStateException("Type " + type + " not supported"); 501 517 } -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r8510 r8568 20 20 import java.util.Enumeration; 21 21 import java.util.List; 22 import java.util.Map; 23 import java.util.Map.Entry; 24 import java.util.concurrent.ConcurrentHashMap; 22 25 import java.util.zip.ZipEntry; 23 26 import java.util.zip.ZipFile; … … 72 75 public static final long DAYS = 24*60*60; // factor to get caching time in days 73 76 77 private Map<String, String> httpHeaders = new ConcurrentHashMap<>(); 78 74 79 /** 75 80 * Constructs a CachedFile object from a given filename, URL or internal resource. … … 142 147 public CachedFile setCachingStrategy(CachingStrategy cachingStrategy) { 143 148 this.cachingStrategy = cachingStrategy; 149 return this; 150 } 151 152 /** 153 * Sets the http headers. Only applies to URL pointing to http or https resources 154 * @param headers that should be sent together with request 155 * @return this object 156 */ 157 public CachedFile setHttpHeaders(Map<String, String> headers) { 158 this.httpHeaders.putAll(headers); 144 159 return this; 145 160 } … … 397 412 destDirFile = new File(destDir, localPath + ".tmp"); 398 413 try { 399 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince );414 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince, httpHeaders); 400 415 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 401 416 if (Main.isDebugEnabled()) { … … 464 479 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) 465 480 throws MalformedURLException, IOException { 481 return connectFollowingRedirect(downloadUrl, httpAccept, ifModifiedSince, null); 482 } 483 /** 484 * Opens a connection for downloading a resource. 485 * <p> 486 * Manually follows redirects because 487 * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect 488 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>. 489 * <p> 490 * This can cause problems when downloading from certain GitHub URLs. 491 * 492 * @param downloadUrl The resource URL to download 493 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null} 494 * @param ifModifiedSince The download time of the cache file, optional 495 * @param headers http headers to be sent together with http request 496 * @return The HTTP connection effectively linked to the resource, after all potential redirections 497 * @throws MalformedURLException If a redirected URL is wrong 498 * @throws IOException If any I/O operation goes wrong 499 * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol 500 * @since TODO 501 */ 502 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince, Map<String, String> headers) 503 throws MalformedURLException, IOException { 466 504 CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl"); 467 505 String downloadString = downloadUrl.toExternalForm(); … … 474 512 if (ifModifiedSince != null) { 475 513 con.setIfModifiedSince(ifModifiedSince); 514 } 515 if (headers != null) { 516 for (Entry<String, String> header: headers.entrySet()) { 517 con.setRequestProperty(header.getKey(), header.getValue()); 518 } 476 519 } 477 520 con.setInstanceFollowRedirects(false); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r8567 r8568 14 14 import java.awt.datatransfer.UnsupportedFlavorException; 15 15 import java.io.BufferedReader; 16 import java.io.ByteArrayOutputStream; 16 17 import java.io.Closeable; 17 18 import java.io.File; … … 1337 1338 return hasExtension(file.getName(), extensions); 1338 1339 } 1340 1341 /** 1342 * Reads the input stream and closes the stream at the end of processing (regardless if an exception was thrown) 1343 * 1344 * @param stream 1345 * @return byte array of data in input stream 1346 * @throws IOException 1347 */ 1348 public static byte[] readBytesFromStream(InputStream stream) throws IOException { 1349 try { 1350 ByteArrayOutputStream bout = new ByteArrayOutputStream(stream.available()); 1351 byte[] buffer = new byte[2048]; 1352 boolean finished = false; 1353 do { 1354 int read = stream.read(buffer); 1355 if (read >= 0) { 1356 bout.write(buffer, 0, read); 1357 } else { 1358 finished = true; 1359 } 1360 } while (!finished); 1361 if (bout.size() == 0) 1362 return null; 1363 return bout.toByteArray(); 1364 } finally { 1365 stream.close(); 1366 } 1367 } 1339 1368 }
Note:
See TracChangeset
for help on using the changeset viewer.