URLPath.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package gnu.kawa.io;
  2. import java.io.*;
  3. import java.net.*;
  4. import gnu.mapping.WrappedException; // FIXME - move to gnu.kawa.util
  5. /** A Path that wraps a URL. */
  6. public class URLPath extends URIPath
  7. {
  8. final URL url;
  9. URLPath (URL url)
  10. {
  11. /* #ifdef use:java.net.URI */
  12. super(toUri(url));;
  13. /* #else */
  14. // super(url.toString());
  15. /* #endif */
  16. this.url = url;
  17. }
  18. public static URLPath valueOf (URL url)
  19. {
  20. return new URLPath(url);
  21. }
  22. public boolean isAbsolute ()
  23. {
  24. return true;
  25. }
  26. public long getLastModified ()
  27. {
  28. return getLastModified(url);
  29. }
  30. public static long getLastModified (URL url)
  31. {
  32. try
  33. {
  34. return url.openConnection().getLastModified();
  35. }
  36. catch (Exception ex)
  37. {
  38. return 0;
  39. }
  40. }
  41. public long getContentLength ()
  42. {
  43. return getContentLength(url);
  44. }
  45. public static int getContentLength (URL url)
  46. {
  47. try
  48. {
  49. return url.openConnection().getContentLength();
  50. }
  51. catch (Exception ex)
  52. {
  53. return -1;
  54. }
  55. }
  56. public URL toURL ()
  57. {
  58. return url;
  59. }
  60. /* #ifdef use:java.net.URI */
  61. public static URI toUri (URL url)
  62. {
  63. try
  64. {
  65. /* #ifdef JAVA5 */
  66. return url.toURI();
  67. /* #else */
  68. // return new URI(url.toString());
  69. /* #endif */
  70. }
  71. catch (Exception ex)
  72. {
  73. throw WrappedException.wrapIfNeeded(ex);
  74. }
  75. }
  76. public URI toUri () { return toUri(url); }
  77. public String toURIString () { return url.toString(); }
  78. /* #else */
  79. // public String toUri () { return url.toString(); }
  80. // public String toURIString () { return uri.toString(); }
  81. /* #endif */
  82. public Path resolve (String relative)
  83. {
  84. try {
  85. return valueOf(resolveToUri(relative).toURL());
  86. } catch (MalformedURLException ex) {
  87. throw new RuntimeException(ex);
  88. }
  89. }
  90. public static InputStream openInputStream (URL url) throws IOException
  91. {
  92. return url.openConnection().getInputStream();
  93. }
  94. public InputStream openInputStream () throws IOException
  95. {
  96. return openInputStream(url);
  97. }
  98. public static OutputStream openOutputStream (URL url) throws IOException
  99. {
  100. String str = url.toString();
  101. // Note JDK (upto 1.5.0, at least) throws an UnknownServiceException
  102. // "protocol doesn't support output" if you do getOutputStream on
  103. // a "file:" URL. That seems lame, but let's avoid that!
  104. if (str.startsWith("file:"))
  105. {
  106. /* #ifdef use:java.net.URI */
  107. try { return new FileOutputStream(new File(new URI(str))); }
  108. catch (Exception ex) { }
  109. /* #else */
  110. // return new FileOutputStream(new File(str.substring(5)));
  111. /* #endif */
  112. }
  113. URLConnection conn = url.openConnection();
  114. conn.setDoInput(false);
  115. conn.setDoOutput(true);
  116. return conn.getOutputStream();
  117. }
  118. public OutputStream openOutputStream () throws IOException
  119. {
  120. return openOutputStream(url);
  121. }
  122. }