Kaynağa Gözat

Make the way identities are migrated much more sensible.

Issue 1122

git-svn-id: http://svn.dmdirc.com/trunk@4006 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 yıl önce
ebeveyn
işleme
2f4dcd468f

+ 6
- 21
src/com/dmdirc/config/Identity.java Dosyayı Görüntüle

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

+ 99
- 0
src/com/dmdirc/config/LineReader.java Dosyayı Görüntüle

@@ -0,0 +1,99 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.config;
24
+
25
+import java.io.Reader;
26
+import java.util.List;
27
+
28
+/**
29
+ * A reader implementation that "reads" from a List of Strings, as provided
30
+ * by a DMDirc text file. This is only used to migrate DMDirc config files.
31
+ * 
32
+ * @author chris
33
+ */
34
+public class LineReader extends Reader {
35
+    
36
+    /** The lines to read from. */
37
+    private final List<String> lines;
38
+    
39
+    /** Character list. */
40
+    char[] mybuf;
41
+
42
+    /** Current position. */
43
+    int cur = 0;
44
+    
45
+    /**
46
+     * Creates a new LineReader to read from the specified lines.
47
+     * 
48
+     * @param lines The lines to read from
49
+     */
50
+    public LineReader(final List<String> lines) {
51
+        super();
52
+        
53
+        this.lines = lines;
54
+        
55
+        int size = 0;
56
+        
57
+        for (String line : lines) {
58
+            size += 1 + line.length();
59
+        }
60
+        
61
+        mybuf = new char[size];
62
+        size = 0;
63
+        
64
+        for (String line : lines) {
65
+            System.arraycopy(line.toCharArray(), 0, mybuf, size, line.length());
66
+            mybuf[size + line.length()] = '\n';
67
+            
68
+            size += 1 + line.length();
69
+        }
70
+    }
71
+    
72
+    /**
73
+     * A version number for this class. It should be changed whenever the class
74
+     * structure is changed (or anything else that would prevent serialized
75
+     * objects being unserialized with the new class).
76
+     */
77
+    private static final long serialVersionUID = 1;
78
+    
79
+    /** {@inheritDoc} */
80
+    @Override
81
+    public int read(final char[] cbuf, final int off, final int len) {
82
+        int count = -1;
83
+        
84
+        if (cur < mybuf.length) {
85
+            count = Math.min(len, mybuf.length - cur);
86
+            System.arraycopy(mybuf, cur, cbuf, off, count);
87
+            cur += count;
88
+        }
89
+        
90
+        return count;
91
+    }
92
+
93
+    /** {@inheritDoc} */
94
+    @Override
95
+    public void close() {
96
+        // Do nothing
97
+    }
98
+
99
+}

+ 1
- 2
src/com/dmdirc/ui/themes/Theme.java Dosyayı Görüntüle

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

+ 2
- 4
src/com/dmdirc/ui/themes/ThemeIdentity.java Dosyayı Görüntüle

@@ -50,15 +50,13 @@ public class ThemeIdentity extends Identity {
50 50
      * Creates a new instance of ThemeIdentity.
51 51
      *
52 52
      * @param stream The input stream to read the identity from.
53
-     * @param stream2 A second input stream (for migration)
54 53
      * @param theme The theme that owns this identity
55 54
      * @throws InvalidIdentityFileException Missing required properties
56 55
      * @throws IOException Input/output exception
57 56
      */
58
-    public ThemeIdentity(final InputStream stream, final InputStream stream2,
59
-            final Theme theme) throws IOException,
57
+    public ThemeIdentity(final InputStream stream, final Theme theme) throws IOException,
60 58
             InvalidIdentityFileException {
61
-        super(stream, stream2, true);
59
+        super(stream, true);
62 60
         
63 61
         myTarget.setTheme();
64 62
         this.theme = theme;

+ 2
- 0
src/com/dmdirc/util/ConfigFile.java Dosyayı Görüntüle

@@ -106,6 +106,8 @@ public class ConfigFile extends TextFile {
106 106
         keydomains.clear();
107 107
         flatdomains.clear();
108 108
         domains.clear();
109
+        
110
+        readLines();
109 111
 
110 112
         for (String line : getLines()) {
111 113
             String tline = line;

+ 21
- 6
src/com/dmdirc/util/TextFile.java Dosyayı Görüntüle

@@ -46,6 +46,9 @@ public class TextFile {
46 46
     /** The input stream we're dealing with. */
47 47
     private InputStream is;
48 48
     
49
+    /** The lines we've read from the file. */
50
+    private List<String> lines;
51
+    
49 52
     /**
50 53
      * Creates a new instance of TextFile for the specified file.
51 54
      * 
@@ -74,25 +77,37 @@ public class TextFile {
74 77
     }
75 78
     
76 79
     /**
77
-     * Retrieves the contents of the file as a list of lines.
80
+     * Retrieves the contents of the file as a list of lines. If getLines() or
81
+     * readLines() has previously been called, a cached version is returned.
78 82
      * 
79 83
      * @return A list of lines in the file
80 84
      * @throws IOException if an I/O exception occurs
81 85
      */
82 86
     public List<String> getLines() throws IOException {
87
+        if (lines == null) {
88
+            readLines();
89
+        }
90
+        
91
+        return lines;
92
+    }
93
+    
94
+    /**
95
+     * Reads the contents of the file into this TextFile's line cache.
96
+     * 
97
+     * @throws IOException If an I/O exception occurs
98
+     */
99
+    public void readLines() throws IOException {
83 100
         final BufferedReader reader = new BufferedReader(
84 101
                 file == null ? new InputStreamReader(is) : new FileReader(file));
85
-        final List<String> res = new ArrayList<String>();
102
+        lines = new ArrayList<String>();
86 103
         
87 104
         String line;
88 105
         
89 106
         while ((line = reader.readLine()) != null) {
90
-            res.add(line);
107
+            lines.add(line);
91 108
         }
92 109
         
93
-        //reader.close();
94
-        
95
-        return res;
110
+        reader.close();
96 111
     }
97 112
     
98 113
     /**

+ 3
- 3
test/com/dmdirc/config/IdentityTest.java Dosyayı Görüntüle

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

Loading…
İptal
Kaydet