1 16 17 package org.springframework.core.io; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 40 public class InputStreamResource extends AbstractResource { 41 42 private final InputStream inputStream; 43 44 private final String description; 45 46 private boolean read = false; 47 48 49 53 public InputStreamResource(InputStream inputStream) { 54 this(inputStream, "resource loaded through InputStream"); 55 } 56 57 62 public InputStreamResource(InputStream inputStream, String description) { 63 if (inputStream == null) { 64 throw new IllegalArgumentException ("InputStream must not be null"); 65 } 66 this.inputStream = inputStream; 67 this.description = (description != null ? description : ""); 68 } 69 70 71 74 public boolean exists() { 75 return true; 76 } 77 78 81 public boolean isOpen() { 82 return true; 83 } 84 85 89 public InputStream getInputStream() throws IOException , IllegalStateException { 90 if (this.read) { 91 throw new IllegalStateException ("InputStream has already been read - " + 92 "do not use InputStreamResource if a stream needs to be read multiple times"); 93 } 94 this.read = true; 95 return this.inputStream; 96 } 97 98 101 public String getDescription() { 102 return this.description; 103 } 104 105 106 109 public boolean equals(Object obj) { 110 return (obj == this || 111 (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream))); 112 } 113 114 117 public int hashCode() { 118 return this.inputStream.hashCode(); 119 } 120 121 } 122 | Popular Tags |