1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package uk.ac.rdg.resc.jstyx.server;
30
31 import java.net.URL;
32 import java.net.MalformedURLException;
33
34 import org.apache.mina.common.ByteBuffer;
35
36 import uk.ac.rdg.resc.jstyx.StyxException;
37 import uk.ac.rdg.resc.jstyx.StyxUtils;
38 import uk.ac.rdg.resc.jstyx.types.ULong;
39
40 /***
41 * StyxFile that contains a URL.
42 *
43 * @author Jon Blower
44 * $Revision: 609 $
45 * $Date: 2006-03-31 18:09:42 +0100 (Fri, 31 Mar 2006) $
46 * $Log$
47 * Revision 1.3 2005/11/07 21:14:44 jonblower
48 * Fixed null pointer bug in getLength()
49 *
50 * Revision 1.2 2005/11/07 12:22:25 jonblower
51 * Added getLength() method
52 *
53 * Revision 1.1 2005/11/04 17:32:11 jonblower
54 * Initial import
55 *
56 */
57
58 public class URLFile extends StyxFile
59 {
60 private URL url;
61
62 /***
63 * Creates a new instance of URLFile with read-write permissions for everyone
64 * (0666).
65 */
66 public URLFile(String name) throws StyxException
67 {
68 super(name, 0666);
69 this.url = null;
70 }
71
72 public synchronized void read(StyxFileClient client, long offset, int count,
73 int tag) throws StyxException
74 {
75 String urlStr = "";
76 if (this.url != null)
77 {
78 urlStr = this.url.toString();
79 }
80 this.processAndReplyRead(urlStr, client, offset, count, tag);
81 }
82
83 /***
84 * The new value for the URL must come in a single message (i.e.
85 * the offset must be zero and the incoming ByteBuffer must contain the
86 * entire URL). Must also write with truncation.
87 */
88 public synchronized void write(StyxFileClient client, long offset,
89 int count, ByteBuffer data, boolean truncate, int tag)
90 throws StyxException
91 {
92 if (!truncate)
93 {
94 throw new StyxException("Must write to the URL file with truncation");
95 }
96 if (count == 0)
97 {
98 if (offset != this.getLength().asLong())
99 {
100 throw new StyxException("Can only write EOF to the end of this file");
101 }
102
103 this.replyWrite(client, 0, tag);
104 return;
105 }
106 if (offset != 0)
107 {
108 throw new StyxException("Must write data to the start of the URL file");
109 }
110
111 data.limit(data.position() + count);
112
113 String urlStr = StyxUtils.dataToString(data);
114 if (urlStr.trim().equals(""))
115 {
116 this.url = null;
117 }
118 else
119 {
120 try
121 {
122 this.url = new URL(urlStr);
123 }
124 catch (MalformedURLException mue)
125 {
126 throw new StyxException(urlStr + " is not recognized as a valid URL");
127 }
128 }
129 this.replyWrite(client, count, tag);
130 }
131
132 /***
133 * @return the size of this file in bytes
134 */
135 public ULong getLength()
136 {
137 if (this.url == null)
138 {
139 return ULong.ZERO;
140 }
141 else
142 {
143 String str = this.url.toString();
144 int len = StyxUtils.strToUTF8(str).length;
145 return new ULong(len);
146 }
147 }
148
149 /***
150 * @return the URL contained in this file or null if it hasn't been set
151 */
152 public URL getURL()
153 {
154 return this.url;
155 }
156 }