Changeset 83
- Timestamp:
- 08/17/07 22:58:12 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
bbtracker/trunk/src/org/bbtracker/mobile/FileTrackStore.java
r75 r83 66 66 public void saveTrack(final Track track) throws TrackStoreException { 67 67 final String dir = getTrackDirectory(); 68 final String filename = getFileName(track);69 68 FileConnection connection = null; 70 69 DataOutputStream dout = null; 71 70 try { 72 connection = (FileConnection) Connector.open("file:///" + dir + "/" + filename + EXTENSION, 73 Connector.READ_WRITE); 74 int i = 1; 75 while (connection.exists()) { 76 connection.close(); 77 connection = (FileConnection) Connector.open("file:///" + dir + "/" + filename + "_" + (i++) + 78 EXTENSION, Connector.WRITE); 79 } 80 connection.create(); 71 connection = createTrackFile(dir, track.getName()); 81 72 dout = connection.openDataOutputStream(); 82 73 track.writeToStream(dout); … … 104 95 } 105 96 97 private FileConnection createTrackFile(final String dir, final String trackName) throws IOException, 98 TrackStoreException { 99 final String filename = getFileName(trackName); 100 FileConnection connection = null; 101 102 int i = 0; 103 do { 104 if (connection != null) { 105 connection.close(); 106 } 107 108 String fullName; 109 if (i == 0) { 110 fullName = "file:///" + dir + filename + EXTENSION; 111 } else { 112 fullName = "file:///" + dir + filename + "_" + i + EXTENSION; 113 } 114 115 try { 116 connection = (FileConnection) Connector.open(fullName, Connector.READ_WRITE); 117 } catch (final IllegalArgumentException e) { 118 // some file system don't like file names that are longer than 8.3 (thanks CP/M and DOS!) 119 if (i == 0) { 120 if (filename.length() <= 8) { 121 throw new TrackStoreException("Filesystem doesn't accept filename <" + fullName + ">: " + 122 e.getMessage()); 123 } 124 fullName = "file:///" + dir + filename.substring(0, 8) + EXTENSION; 125 } else { 126 final String postfix = "_" + i; 127 final int l = 8 - postfix.length(); 128 fullName = "file:///" + dir + filename.substring(0, l) + postfix + EXTENSION; 129 } 130 try { 131 connection = (FileConnection) Connector.open(fullName, Connector.READ_WRITE); 132 } catch (final IllegalArgumentException e2) { 133 throw new TrackStoreException("Filesystem doesn't accept filename <" + fullName + ">: " + 134 e2.getMessage()); 135 } 136 } 137 i++; 138 } while (connection.exists()); 139 connection.create(); 140 return connection; 141 } 142 106 143 private String getTrackDirectory() throws TrackStoreException { 107 144 final String dir = Preferences.getInstance().getTrackDirectory(); … … 112 149 } 113 150 114 private String getFileName(final Track track) { 115 final String name = track.getName(); 116 final char[] chars = name.toCharArray(); 151 private String getFileName(final String trackName) { 152 final char[] chars = trackName.toCharArray(); 117 153 for (int i = 0; i < chars.length; i++) { 118 154 final char c = chars[i];
Note: See TracChangeset
for help on using the changeset viewer.