Changeset 6421 in josm
- Timestamp:
- 2013-11-28T23:33:10+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Version.java
r6317 r6421 236 236 } 237 237 String result = "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+")"; 238 if (includeOsDetails ) {238 if (includeOsDetails && Main.platform != null) { 239 239 result += " " + Main.platform.getOSDescription(); 240 240 } -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r6410 r6421 13 13 import java.awt.datatransfer.Transferable; 14 14 import java.awt.datatransfer.UnsupportedFlavorException; 15 import java.io.BufferedInputStream; 15 16 import java.io.BufferedReader; 16 17 import java.io.Closeable; … … 37 38 import java.util.Iterator; 38 39 import java.util.List; 40 import java.util.zip.GZIPInputStream; 39 41 import java.util.zip.ZipFile; 40 42 43 import org.apache.tools.bzip2.CBZip2InputStream; 41 44 import org.openstreetmap.josm.Main; 42 45 import org.openstreetmap.josm.data.Version; 46 import org.openstreetmap.josm.io.FileImporter; 43 47 44 48 /** … … 638 642 */ 639 643 public static InputStream openURL(URL url) throws IOException { 640 return setupURLConnection(url.openConnection()).getInputStream(); 644 return openURLAndDecompress(url, false); 645 } 646 647 /** 648 * Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary. 649 * @param url The url to open 650 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream} 651 * if the {@code Content-Type} header is set accordingly. 652 * @return An stream for the given URL 653 * @throws IOException if an I/O exception occurs. 654 * @since 6421 655 */ 656 public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException { 657 final URLConnection connection = setupURLConnection(url.openConnection()); 658 if (decompress && "application/x-gzip".equals(connection.getHeaderField("Content-Type"))) { 659 return new GZIPInputStream(connection.getInputStream()); 660 } else if (decompress && "application/x-bzip2".equals(connection.getHeaderField("Content-Type"))) { 661 return FileImporter.getBZip2InputStream(new BufferedInputStream(connection.getInputStream())); 662 } else { 663 return connection.getInputStream(); 664 } 641 665 } 642 666 … … 664 688 */ 665 689 public static BufferedReader openURLReader(URL url) throws IOException { 666 return new BufferedReader(new InputStreamReader(openURL(url), "utf-8")); 690 return openURLReaderAndDecompress(url, false); 691 } 692 693 /** 694 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one. 695 * @param url The url to open 696 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream} 697 * if the {@code Content-Type} header is set accordingly. 698 * @return An buffered stream reader for the given URL (using UTF-8) 699 * @throws IOException if an I/O exception occurs. 700 * @since 6421 701 */ 702 public static BufferedReader openURLReaderAndDecompress(final URL url, final boolean decompress) throws IOException { 703 return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), "utf-8")); 667 704 } 668 705 … … 670 707 * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive. 671 708 * @param httpURL The HTTP url to open (must use http:// or https://) 672 * @param keepAlive 709 * @param keepAlive whether not to set header {@code Connection=close} 673 710 * @return An open HTTP connection to the given URL 674 711 * @throws IOException if an I/O exception occurs. -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r6410 r6421 4 4 import org.junit.Assert; 5 5 import org.junit.Test; 6 import org.openstreetmap.josm.Main; 7 import org.openstreetmap.josm.data.Preferences; 8 9 import java.io.BufferedReader; 10 import java.net.URL; 6 11 7 12 /** … … 66 71 Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc})); 67 72 } 73 74 @Test 75 public void testOpenUrlGzip() throws Exception { 76 Main.pref = new Preferences(); 77 final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("http://www.openstreetmap.org/trace/1613906/data"), true); 78 Assert.assertTrue(x.readLine().startsWith("<?xml version=")); 79 x.close(); 80 } 81 82 @Test 83 public void testOpenUrlBzip() throws Exception { 84 Main.pref = new Preferences(); 85 final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("http://www.openstreetmap.org/trace/785544/data"), true); 86 Assert.assertTrue(x.readLine().startsWith("<?xml version=")); 87 x.close(); 88 } 68 89 }
Note:
See TracChangeset
for help on using the changeset viewer.