Changeset 7119 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2014-05-14T00:51:20+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r7083 r7119 45 45 import java.util.regex.Pattern; 46 46 import java.util.zip.GZIPInputStream; 47 import java.util.zip.ZipEntry; 47 48 import java.util.zip.ZipFile; 49 import java.util.zip.ZipInputStream; 48 50 49 51 import org.apache.tools.bzip2.CBZip2InputStream; 50 52 import org.openstreetmap.josm.Main; 51 53 import org.openstreetmap.josm.data.Version; 52 import org.openstreetmap.josm.io.FileImporter;53 54 54 55 /** … … 665 666 public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException { 666 667 final URLConnection connection = setupURLConnection(url.openConnection()); 667 if (decompress && "application/x-gzip".equals(connection.getHeaderField("Content-Type"))) { 668 return new GZIPInputStream(connection.getInputStream()); 669 } else if (decompress && "application/x-bzip2".equals(connection.getHeaderField("Content-Type"))) { 670 return FileImporter.getBZip2InputStream(new BufferedInputStream(connection.getInputStream())); 671 } else { 672 return connection.getInputStream(); 673 } 668 final InputStream in = connection.getInputStream(); 669 if (decompress) { 670 switch (connection.getHeaderField("Content-Type")) { 671 case "application/zip": 672 return getZipInputStream(in); 673 case "application/x-gzip": 674 return getGZipInputStream(in); 675 case "application/x-bzip2": 676 return getBZip2InputStream(in); 677 } 678 } 679 return in; 680 } 681 682 /** 683 * Returns a Bzip2 input stream wrapping given input stream. 684 * @param in The raw input stream 685 * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null} 686 * @throws IOException if the given input stream does not contain valid BZ2 header 687 * @since 7119 688 */ 689 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException { 690 if (in == null) { 691 return null; 692 } 693 BufferedInputStream bis = new BufferedInputStream(in); 694 int b = bis.read(); 695 if (b != 'B') 696 throw new IOException(tr("Invalid bz2 file.")); 697 b = bis.read(); 698 if (b != 'Z') 699 throw new IOException(tr("Invalid bz2 file.")); 700 return new CBZip2InputStream(bis, /* see #9537 */ true); 701 } 702 703 /** 704 * Returns a Gzip input stream wrapping given input stream. 705 * @param in The raw input stream 706 * @return a Gzip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null} 707 * @throws IOException if an I/O error has occurred 708 * @since 7119 709 */ 710 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException { 711 if (in == null) { 712 return null; 713 } 714 return new GZIPInputStream(in); 715 } 716 717 /** 718 * Returns a Zip input stream wrapping given input stream. 719 * @param in The raw input stream 720 * @return a Zip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null} 721 * @throws IOException if an I/O error has occurred 722 * @since 7119 723 */ 724 public static ZipInputStream getZipInputStream(InputStream in) throws IOException { 725 if (in == null) { 726 return null; 727 } 728 ZipInputStream zis = new ZipInputStream(in, StandardCharsets.UTF_8); 729 // Positions the stream at the beginning of first entry 730 ZipEntry ze = zis.getNextEntry(); 731 if (ze != null && Main.isDebugEnabled()) { 732 Main.debug("Zip entry: "+ze.getName()); 733 } 734 return zis; 674 735 } 675 736
Note:
See TracChangeset
for help on using the changeset viewer.