mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-29 17:52:06 +01:00
Move file related methods to FileUtils
This commit is contained in:
parent
4a366007d6
commit
1bf57cb6a1
2 changed files with 67 additions and 50 deletions
|
@ -18,11 +18,7 @@ package org.jivesoftware.smack;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
@ -31,6 +27,7 @@ import java.util.List;
|
|||
import org.jivesoftware.smack.packet.RosterPacket;
|
||||
import org.jivesoftware.smack.packet.RosterPacket.Item;
|
||||
import org.jivesoftware.smack.util.Base32Encoder;
|
||||
import org.jivesoftware.smack.util.FileUtils;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.xmlpull.v1.XmlPullParserFactory;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
@ -100,7 +97,7 @@ public class DefaultRosterStore implements RosterStore {
|
|||
*/
|
||||
public static DefaultRosterStore open(final File 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")) {
|
||||
return store;
|
||||
}
|
||||
|
@ -137,7 +134,7 @@ public class DefaultRosterStore implements RosterStore {
|
|||
|
||||
@Override
|
||||
public String getRosterVersion() {
|
||||
String s = readFile(getVersionFile());
|
||||
String s = FileUtils.readFile(getVersionFile());
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -149,7 +146,7 @@ public class DefaultRosterStore implements RosterStore {
|
|||
}
|
||||
|
||||
private boolean setRosterVersion(String version) {
|
||||
return writeFile(getVersionFile(), STORE_ID + "\n" + version);
|
||||
return FileUtils.writeFile(getVersionFile(), STORE_ID + "\n" + version);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -187,7 +184,7 @@ public class DefaultRosterStore implements RosterStore {
|
|||
}
|
||||
|
||||
private Item readEntry(File file) {
|
||||
String s = readFile(file);
|
||||
String s = FileUtils.readFile(file);
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -297,7 +294,7 @@ public class DefaultRosterStore implements RosterStore {
|
|||
s.append(" />");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
System.err.println(error);
|
||||
}
|
||||
|
|
|
@ -17,17 +17,26 @@
|
|||
package org.jivesoftware.smack.util;
|
||||
|
||||
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.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
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 {
|
||||
URI fileUri = URI.create(url);
|
||||
|
||||
|
@ -82,4 +91,56 @@ public final class FileUtils {
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue