1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 08:12:05 +01:00

Move file related methods to FileUtils

This commit is contained in:
Florian Schmaus 2014-03-20 13:26:57 +01:00
parent 4a366007d6
commit 1bf57cb6a1
2 changed files with 67 additions and 50 deletions

View file

@ -18,11 +18,7 @@ package org.jivesoftware.smack;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -31,6 +27,7 @@ import java.util.List;
import org.jivesoftware.smack.packet.RosterPacket; import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item; import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.util.Base32Encoder; import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -100,7 +97,7 @@ public class DefaultRosterStore implements RosterStore {
*/ */
public static DefaultRosterStore open(final File baseDir) { public static DefaultRosterStore open(final File baseDir) {
DefaultRosterStore store = new DefaultRosterStore(baseDir); DefaultRosterStore store = new DefaultRosterStore(baseDir);
String s = store.readFile(store.getVersionFile()); String s = FileUtils.readFile(store.getVersionFile());
if (s != null && s.startsWith(STORE_ID + "\n")) { if (s != null && s.startsWith(STORE_ID + "\n")) {
return store; return store;
} }
@ -137,7 +134,7 @@ public class DefaultRosterStore implements RosterStore {
@Override @Override
public String getRosterVersion() { public String getRosterVersion() {
String s = readFile(getVersionFile()); String s = FileUtils.readFile(getVersionFile());
if (s == null) { if (s == null) {
return null; return null;
} }
@ -149,7 +146,7 @@ public class DefaultRosterStore implements RosterStore {
} }
private boolean setRosterVersion(String version) { private boolean setRosterVersion(String version) {
return writeFile(getVersionFile(), STORE_ID + "\n" + version); return FileUtils.writeFile(getVersionFile(), STORE_ID + "\n" + version);
} }
@Override @Override
@ -187,7 +184,7 @@ public class DefaultRosterStore implements RosterStore {
} }
private Item readEntry(File file) { private Item readEntry(File file) {
String s = readFile(file); String s = FileUtils.readFile(file);
if (s == null) { if (s == null) {
return null; return null;
} }
@ -297,7 +294,7 @@ public class DefaultRosterStore implements RosterStore {
s.append(" />"); s.append(" />");
} }
s.append("</item>"); s.append("</item>");
return writeFile(getBareJidFile(item.getUser()), s.toString()); return FileUtils.writeFile(getBareJidFile(item.getUser()), s.toString());
} }
@ -306,47 +303,6 @@ public class DefaultRosterStore implements RosterStore {
return new File(fileDir, ENTRY_PREFIX + encodedJid); return new File(fileDir, ENTRY_PREFIX + encodedJid);
} }
private String readFile(File file) {
try {
Reader reader = null;
try {
char buf[] = new char[8192];
int len;
StringBuilder s = new StringBuilder();
reader = new FileReader(file);
while ((len = reader.read(buf)) >= 0) {
s.append(buf, 0, len);
}
return s.toString();
}
finally {
if (reader != null) {
reader.close();
}
}
}
catch (FileNotFoundException e) {
return null;
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private boolean writeFile(File file, String content) {
try {
FileWriter writer = new FileWriter(file, false);
writer.write(content);
writer.close();
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void log(String error) { private void log(String error) {
System.err.println(error); System.err.println(error);
} }

View file

@ -17,17 +17,26 @@
package org.jivesoftware.smack.util; package org.jivesoftware.smack.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class FileUtils { public final class FileUtils {
private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());
public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException { public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException {
URI fileUri = URI.create(url); URI fileUri = URI.create(url);
@ -82,4 +91,56 @@ public final class FileUtils {
} }
return true; return true;
} }
/**
* Reads the contents of a File
*
* @param file
* @return the content of file or null in case of an error
* @throws IOException
*/
public static String readFileOrThrow(File file) throws FileNotFoundException, IOException {
Reader reader = null;
try {
reader = new FileReader(file);
char buf[] = new char[8192];
int len;
StringBuilder s = new StringBuilder();
while ((len = reader.read(buf)) >= 0) {
s.append(buf, 0, len);
}
return s.toString();
}
finally {
if (reader != null) {
reader.close();
}
}
}
public static String readFile(File file) {
try {
return readFileOrThrow(file);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "readFile", e);
}
return null;
}
public static void writeFileOrThrow(File file, String content) throws IOException {
FileWriter writer = new FileWriter(file, false);
writer.write(content);
writer.close();
}
public static boolean writeFile(File file, String content) {
try {
writeFileOrThrow(file, content);
return true;
}
catch (IOException e) {
LOGGER.log(Level.WARNING, "writeFile", e);
return false;
}
}
} }