Browse Source

Identities initialised using streams now require two to be passed, as there doesn't seem to be a reliable way to read one twice...

Fixes issue 1123
Fixes issue 1124
Fixes issue 1125

git-svn-id: http://svn.dmdirc.com/trunk@3878 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
134e1a33fa

+ 14
- 16
src/com/dmdirc/config/Identity.java View File

@@ -134,7 +134,7 @@ public class Identity extends ConfigSource implements Serializable,
134 134
             InvalidIdentityFileException {
135 135
         this.file = new ConfigFile(new TextFile(file));
136 136
         this.file.setAutomake(true);
137
-        initFile(forceDefault, null, file);
137
+        initFile(forceDefault, new FileInputStream(file), new FileInputStream(file));
138 138
         myTarget = getTarget(forceDefault);
139 139
     }
140 140
 
@@ -142,16 +142,18 @@ public class Identity extends ConfigSource implements Serializable,
142 142
      * Creates a new read-only identity.
143 143
      *
144 144
      * @param stream The input stream to read the identity from
145
+     * @param stream2 A second input stream if the first doesn't support marking
145 146
      * @param forceDefault Whether to force this identity to be loaded as default
146 147
      * identity or not
147 148
      * @throws InvalidIdentityFileException Missing required properties
148 149
      * @throws IOException Input/output exception
149 150
      */
150
-    public Identity(final InputStream stream, final boolean forceDefault) throws IOException,
151
+    public Identity(final InputStream stream, final InputStream stream2,
152
+            final boolean forceDefault) throws IOException,
151 153
             InvalidIdentityFileException {
152 154
         this.file = new ConfigFile(new TextFile(stream));
153 155
         this.file.setAutomake(true);
154
-        initFile(forceDefault, stream, null);
156
+        initFile(forceDefault, stream, stream2);
155 157
         myTarget = getTarget(forceDefault);
156 158
     }
157 159
 
@@ -220,34 +222,30 @@ public class Identity extends ConfigSource implements Serializable,
220 222
      * @throws InvalidIdentityFileException if the identity file is invalid
221 223
      * @throws IOException On I/O exception when reading the identity
222 224
      */
223
-    @Precondition("Exactly one of stream and file are specified")
224 225
     private void initFile(final boolean forceDefault, final InputStream stream,
225
-            final File file) throws InvalidIdentityFileException, IOException {
226
-        Logger.assertTrue(file == null ^ stream == null);
226
+            final InputStream newStream) throws InvalidIdentityFileException, IOException {
227 227
         
228
-        if (stream != null && stream.markSupported()) {
228
+        if (stream.markSupported()) {
229 229
             stream.mark(Integer.MAX_VALUE);
230 230
         }
231 231
         
232 232
         try {
233 233
             this.file.read();
234
-        } catch (InvalidConfigFileException ex) {
235
-            InputStream newStream;
234
+        } catch (InvalidConfigFileException ex) {            
235
+            InputStream myStream;
236 236
             
237 237
             if (stream != null && stream.markSupported()) {
238 238
                 // Not a config file
239 239
                 stream.reset();
240
-                newStream = stream;
241
-            } else if (file != null) {
242
-                newStream = new FileInputStream(file);
240
+                myStream = stream;
243 241
             } else {
244
-                throw new IllegalArgumentException("InputStream does not support "
245
-                        + "mark and contents are not a ConfigFile");                
242
+                myStream = newStream;
246 243
             }
247 244
                 
248 245
             final Properties properties = new Properties();
249
-            properties.load(newStream);
250
-            newStream.close();
246
+            properties.load(myStream);
247
+            
248
+            myStream.close();
251 249
             
252 250
             migrateProperties(properties);
253 251
         }

+ 1
- 1
src/com/dmdirc/themes/Theme.java View File

@@ -112,7 +112,7 @@ public class Theme implements Comparable<Theme> {
112 112
 
113 113
         if (stream != null) {
114 114
             try {
115
-                identity = new ThemeIdentity(stream);
115
+                identity = new ThemeIdentity(stream, rm.getResourceInputStream("config"));
116 116
                 IdentityManager.addIdentity(identity);
117 117
             } catch (InvalidIdentityFileException ex) {
118 118
                 Logger.userError(ErrorLevel.MEDIUM, "Error loading theme identity file: "

+ 3
- 2
src/com/dmdirc/themes/ThemeIdentity.java View File

@@ -47,12 +47,13 @@ public class ThemeIdentity extends Identity {
47 47
      * Creates a new instance of ThemeIdentity.
48 48
      *
49 49
      * @param stream The input stream to read the identity from.
50
+     * @param stream2 A second input stream (for migration)
50 51
      * @throws InvalidIdentityFileException Missing required properties
51 52
      * @throws IOException Input/output exception
52 53
      */
53
-    public ThemeIdentity(final InputStream stream) throws IOException,
54
+    public ThemeIdentity(final InputStream stream, final InputStream stream2) throws IOException,
54 55
             InvalidIdentityFileException {
55
-        super(stream, true);
56
+        super(stream, stream2, true);
56 57
         myTarget.setTheme();
57 58
     }
58 59
     

+ 3
- 3
test/com/dmdirc/config/IdentityTest.java View File

@@ -165,17 +165,17 @@ public class IdentityTest {
165 165
     
166 166
     @Test(expected=InvalidIdentityFileException.class)
167 167
     public void testNoName() throws IOException, InvalidIdentityFileException {
168
-        new Identity(getClass().getResourceAsStream("identity1"), false);
168
+        new Identity(getClass().getResourceAsStream("identity1"), null, false);
169 169
     }
170 170
     
171 171
     @Test(expected=InvalidIdentityFileException.class)
172 172
     public void testNoTarget() throws IOException, InvalidIdentityFileException {
173
-        new Identity(getClass().getResourceAsStream("identity2"), false);
173
+        new Identity(getClass().getResourceAsStream("identity2"), null, false);
174 174
     }
175 175
     
176 176
     @Test
177 177
     public void testMigrate() throws IOException, InvalidIdentityFileException {
178
-        final Identity id = new Identity(getClass().getResourceAsStream("identity3"), false);
178
+        final Identity id = new Identity(getClass().getResourceAsStream("identity3"), null, false);
179 179
         
180 180
         assertTrue(id.getFile().isKeyDomain("identity"));
181 181
         assertTrue(id.getFile().isKeyDomain("meep"));

Loading…
Cancel
Save