📄 ref.java
字号:
/*
* Created on Nov 12, 2004
*
* @author Fabio Zadrozny
*/
package org.python.pydev.core;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.internal.resources.ResourceException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.python.pydev.core.log.Log;
/**
* @author Fabio Zadrozny
*/
public class REF {
public static boolean hasAttr(Object o, String attr){
try {
o.getClass().getDeclaredField(attr);
} catch (SecurityException e) {
return false;
} catch (NoSuchFieldException e) {
return false;
}
return true;
}
public static Field getAttrFromClass(Class c, String attr){
try {
return c.getDeclaredField(attr);
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
}
return null;
}
public static Field getAttr(Object o, String attr){
try {
return o.getClass().getDeclaredField(attr);
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
}
return null;
}
public static Object getAttrObj(Object o, String attr){
try {
Field field = REF.getAttr(o, attr);
if(field != null){
Object obj = field.get(o);
return obj;
}
}catch (Exception e) {
//ignore
}
return null;
}
/**
* @param file the file we want to read
* @return the contents of the fil as a string
*/
public static String getFileContents(File file) {
FileInputStream stream = null;
try {
stream = new FileInputStream(file);
return getFileContents(stream, null, null);
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
try { if(stream != null) stream.close(); } catch (Exception e) {Log.log(e);}
}
}
/**
* @param stream
* @return
* @throws IOException
*/
private static String getFileContents(InputStream contentStream, String encoding, IProgressMonitor monitor) throws IOException {
Reader in= null;
try{
final int DEFAULT_FILE_SIZE= 15 * 1024;
if (encoding == null){
in= new BufferedReader(new InputStreamReader(contentStream), DEFAULT_FILE_SIZE);
}else{
try {
in = new BufferedReader(new InputStreamReader(contentStream, encoding), DEFAULT_FILE_SIZE);
} catch (UnsupportedEncodingException e) {
Log.log(e);
//keep going without the encoding
in= new BufferedReader(new InputStreamReader(contentStream), DEFAULT_FILE_SIZE);
}
}
StringBuffer buffer= new StringBuffer(DEFAULT_FILE_SIZE);
char[] readBuffer= new char[2048];
int n= in.read(readBuffer);
while (n > 0) {
if (monitor != null && monitor.isCanceled())
return null;
buffer.append(readBuffer, 0, n);
n= in.read(readBuffer);
}
return buffer.toString();
}finally{
try { if(in != null) in.close(); } catch (Exception e) {Log.log(e);}
}
}
/**
* @param o the object we want as a string
* @return the string representing the object as base64
*/
public static String getObjAsStr(Object o) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ObjectOutputStream stream = new ObjectOutputStream(out);
stream.writeObject(o);
stream.close();
} catch (Exception e) {
Log.log(e);
throw new RuntimeException(e);
}
return new String(encodeBase64(out));
}
public static byte[] encodeBase64(ByteArrayOutputStream out) {
byte[] byteArray = out.toByteArray();
return encodeBase64(byteArray);
}
public static byte[] encodeBase64(byte[] byteArray) {
return Base64.encodeBase64(byteArray);
}
/**
*
* @param persisted the base64 string that should be converted to an object.
* @param readFromFileMethod should be the calback from the plugin that is calling this function
*
*
* The callback should be something as:
new ICallback<Object, ObjectInputStream>(){
public Object call(ObjectInputStream arg) {
try {
return arg.readObject();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}};
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object getStrAsObj(String persisted, ICallback<Object, ObjectInputStream> readFromFileMethod) throws IOException, ClassNotFoundException {
InputStream input = new ByteArrayInputStream(decodeBase64(persisted));
Object o = readFromInputStreamAndCloseIt(readFromFileMethod, input);
return o;
}
/**
* @param readFromFileMethod
* @param input
* @return
* @throws IOException
*/
public static Object readFromInputStreamAndCloseIt(ICallback<Object, ObjectInputStream> readFromFileMethod, InputStream input) {
ObjectInputStream in = null;
Object o = null;
try {
try {
in = new ObjectInputStream(input);
o = readFromFileMethod.call(in);
} finally {
if(in!=null){
in.close();
}
input.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return o;
}
public static byte[] decodeBase64(String persisted) {
return Base64.decodeBase64(persisted.getBytes());
}
public static void writeStrToFile(String str, String file) {
writeStrToFile(str, new File(file));
}
public static void appendStrToFile(String str, String file) {
try {
FileOutputStream stream = new FileOutputStream(file, true);
try {
stream.write(str.getBytes());
} finally{
stream.close();
}
} catch (FileNotFoundException e) {
Log.log(e);
} catch (IOException e) {
Log.log(e);
}
}
public static void writeStrToFile(String str, File file) {
try {
FileOutputStream stream = new FileOutputStream(file);
try {
stream.write(str.getBytes());
} finally{
stream.close();
}
} catch (FileNotFoundException e) {
Log.log(e);
} catch (IOException e) {
Log.log(e);
}
}
/**
* @param file
* @param astManager
*/
public static void writeToFile(Object o, File file) {
try {
OutputStream out = new FileOutputStream(file);
writeToStreamAndCloseIt(o, out);
} catch (Exception e) {
Log.log(e);
}
}
/**
* @param o the object to be written to some stream
* @param out the output stream to be used
*
* @throws IOException
*/
public static void writeToStreamAndCloseIt(Object o, OutputStream out) throws IOException {
//change: checks if we have a buffered output stream (if we don't, one will be provided)
OutputStream b = null;
if (out instanceof BufferedOutputStream || out instanceof ByteArrayOutputStream){
b = (BufferedOutputStream) out;
}else{
b = new BufferedOutputStream(out);
}
try {
ObjectOutputStream stream = new ObjectOutputStream(b);
stream.writeObject(o);
stream.close();
} catch (Exception e) {
Log.log(e);
throw new RuntimeException(e);
} finally{
b.close();
}
}
/**
* Reads some object from a file
* @param file the file from where we should read
* @return the object that was read (or null if some error happened while reading)
*/
public static Object readFromFile(File file){
try {
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
ObjectInputStream stream = new ObjectInputStream(in);
try {
Object o = stream.readObject();
return o;
} finally {
stream.close();
}
} finally {
in.close();
}
} catch (Exception e) {
Log.log(e);
return null;
}
}
public static String getFileAbsolutePath(String f) {
return getFileAbsolutePath(new File(f));
}
/**
* @param f the file we're interested in
* @return the absolute (canonical) path to the file
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -