本文整理汇总了Java中java.beans.XMLEncoder.close方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEncoder.close方法的具体用法?Java XMLEncoder.close怎么用?Java XMLEncoder.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.beans.XMLEncoder
的用法示例。
在下文中一共展示了XMLEncoder.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public @Override void write(java.io.Writer w, final Object inst) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new ExceptionListener() {
public @Override void exceptionThrown(Exception x) {
Logger.getLogger(XMLBeanConvertor.class.getName()).log(Level.INFO, "Problem writing " + inst, x);
}
});
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
try {
// XXX would inst.getClass().getClassLoader() be more appropriate?
ClassLoader ccl2 = Lookup.getDefault().lookup(ClassLoader.class);
if (ccl2 != null) {
Thread.currentThread().setContextClassLoader(ccl2);
}
e.writeObject(inst);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
e.close();
String data = new String(out.toByteArray(), "UTF-8");
data = data.replaceFirst("<java", "<!DOCTYPE xmlbeans PUBLIC \"-//NetBeans//DTD XML beans 1.0//EN\" \"http://www.netbeans.org/dtds/xml-beans-1_0.dtd\">\n<java");
w.write(data);
}
示例2: objectXmlEncoder
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* 把java的可序列化的对象(实现Serializable接口)序列化保存到XML文件里面,如果想一次保存多个可序列化对象请用集合进行封装
* 保存时将会用现在的对象原来的XML文件内容
* @param obj 要序列化的可序列化的对象
* @param fileName 带完全的保存路径的文件名
* @throws FileNotFoundException 指定位置的文件不存在
* @throws IOException 输出时发生异常
* @throws Exception 其他运行时异常
*/
public static void objectXmlEncoder(Object obj,String fileName)
throws FileNotFoundException,IOException,Exception
{
//创建输出文件
File fo = new File(fileName);
//文件不存在,就创建该文件
if(!fo.exists())
{
//先创建文件的目录
String path = fileName.substring(0,fileName.lastIndexOf('.'));
File pFile = new File(path);
pFile.mkdirs();
}
//创建文件输出流
FileOutputStream fos = new FileOutputStream(fo);
//创建XML文件对象输出类实例
XMLEncoder encoder = new XMLEncoder(fos);
//对象序列化输出到XML文件
encoder.writeObject(obj);
encoder.flush();
//关闭序列化工具
encoder.close();
//关闭输出流
fos.close();
}
示例3: saveAnObjectXML
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static void saveAnObjectXML(Object obj, String path) {
try {
// Serialize object into XML
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(path)));
encoder.writeObject(obj);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
示例4: run
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void run() {
// run thread a few time
// object stays the same but NullPointerException appears randomly
// on dual proccessor a lock is generated
for (int i = 0; i < THREAD_LENGTH; i++) {
// create XMLEncoder to ByteArrayOutputStream
// this is to exclude file locking problems
XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
encoder.setExceptionListener(this);
// write the object
// will see randomly null pointer exceptions
// a bug as object is same through different encode phases
encoder.writeObject(this.object);
//close encoder
encoder.close();
}
System.out.println(Thread.currentThread().getName() + " is finished");
}
示例5: test
import java.beans.XMLEncoder; //导入方法依赖的package包/类
static void test(AbstractTest object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(output);
encoder.setPersistenceDelegate(
object.getClass(),
new DefaultPersistenceDelegate(new String[] {"value"}));
encoder.writeObject(object);
encoder.close();
System.out.print(output);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
XMLDecoder decoder = new XMLDecoder(input);
AbstractTest result = (AbstractTest) decoder.readObject();
decoder.close();
if (object.getValue() != result.getValue())
throw new Error("Should be " + object);
}
示例6: doSave
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* This code abstracted because it relies on java.beans.* which is not always available
* e.g. on Android
*/
@Override
public void
doSave(
OutputStream out,
Map usersMap )
{
UserManagerConfig config = new UserManagerConfig();
List users = new ArrayList( usersMap.values() );
config.setUsers(users);
XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( out ) );
encoder.writeObject(config);
encoder.close();
}
示例7: objectToXml
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static byte[] objectToXml(Object obj,ClassLoader cl){
if(cl == null){ return objectToXml(obj); }
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLEncoder xml = new XMLEncoder(bos);
//HACK
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
if (cl != oldLoader && cl != null){
try{
Thread.currentThread().setContextClassLoader(cl);
xml.writeObject(obj);
}catch(Exception e){e.printStackTrace();}
finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}else{
xml.writeObject(obj);
}
xml.close();
return bos.toByteArray();
}
示例8: writeToDisk
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void writeToDisk() throws IOException {
if (_purged)
return;
XMLEncoder out = null;
try {
out = _um.getXMLEncoder(new SafeFileOutputStream(_um
.getUserFile(getName())));
ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(CommonPluginUtils.getClassLoaderForObject(this));
out.writeObject(this);
Thread.currentThread().setContextClassLoader(prevCL);
logger.debug("Wrote userfile for " + this.getName());
} finally {
if (out != null)
out.close();
}
}
示例9: save
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static void save(File fout, SkeletonBean obj) throws IOException
{
FileOutputStream fos = new FileOutputStream(fout);
try {
XMLEncoder enc = new XMLEncoder(fos);
enc.writeObject(obj);
enc.close();
} catch (Exception ex)
{
throw new IOException(ex.toString());
}
finally
{
fos.close();
}
}
示例10: serializeAndDeserialize
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* @param original
* @return
*/
public T serializeAndDeserialize(T original)
{
java.io.ByteArrayOutputStream out = new DebuggingByteArrayOutputStream(4096);
XMLEncoder encoder = new XMLEncoder(out);
encoder.writeObject(original);
encoder.close();
dumpByteArray(out.toByteArray());
java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(out.toByteArray());
XMLDecoder decoder = new XMLDecoder(in);
T deserialized = (T)decoder.readObject();
decoder.close();
return deserialized;
}
示例11: writeXMLObject
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Writes the objet and CLOSES the stream. Uses the persistence delegate registered in this class.
*
* @param os The stream to write to. Will be closed.
* @param o The object to be serialized.
* @param listener The listener to recieve the exeptions if there are any. If <code>null</code> not used.
*/
void writeXMLObject(final OutputStream os, final Object o, final ExceptionListener listener) {
final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(LayoutUtilCommon.class.getClassLoader());
final XMLEncoder encoder = new XMLEncoder(os);
if (listener != null) {
encoder.setExceptionListener(listener);
}
encoder.writeObject(o);
encoder.close(); // Must be closed to write.
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
示例12: save
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void save() {
try {
Engine.logEngine.debug("(Scheduler Manager) Start jobs saving ...");
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(getFileURL()));
encoder.writeObject(schedulerXML);
encoder.close();
Engine.logEngine.debug("(Scheduler Manager) ... jobs saving finished !");
} catch (FileNotFoundException e) {
Engine.logEngine.error("(Scheduler Manager) ... jobs saving failed !", e);
}
}
示例13: saveTemplates
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Saves all templates as XML files in the current template directory.
*
* @return Whether or not the save was successful.
*/
public synchronized boolean saveTemplates() {
if (templates==null) {
return true;
}
if (directory==null || !directory.isDirectory()) {
return false;
}
// Blow away all old XML files to start anew, as some might be from
// templates we're removed from the template manager.
File[] oldXMLFiles = directory.listFiles(new XMLFileFilter());
if (oldXMLFiles==null) {
return false; // Either an IOException or it isn't a directory.
}
int count = oldXMLFiles.length;
for (int i=0; i<count; i++) {
/*boolean deleted = */oldXMLFiles[i].delete();
}
// Save all current templates as XML.
boolean wasSuccessful = true;
for (CodeTemplate template : templates) {
File xmlFile = new File(directory, template.getID() + ".xml");
try {
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(xmlFile)));
e.writeObject(template);
e.close();
} catch (IOException ioe) {
ioe.printStackTrace();
wasSuccessful = false;
}
}
return wasSuccessful;
}
示例14: serialize
import java.beans.XMLEncoder; //导入方法依赖的package包/类
@Override
public boolean serialize(Object src, OutputStream dst) throws IOException {
XMLEncoder encoder = new XMLEncoder(dst);
encoder.writeObject(src);
encoder.flush();
encoder.close();
return false;
}
示例15: propertyChange
import java.beans.XMLEncoder; //导入方法依赖的package包/类
@Override
public void propertyChange(PropertyChangeEvent evt) {
Runnable runnable = new Runnable() {
public void run() {
Collection<? extends AndroidJavaPlatformProvider8> providers = Lookup.getDefault().lookupAll(AndroidJavaPlatformProvider8.class);
List<String> tmp = new ArrayList<>();
for (AndroidJavaPlatformProvider8 provider : providers) {
if (provider != null) {
JavaPlatform[] installedPlatforms = provider.getInstalledPlatforms();
for (JavaPlatform installedPlatform : installedPlatforms) {
List<ClassPath.Entry> entries = installedPlatform.getSourceFolders().entries();
for (ClassPath.Entry entrie : entries) {
if (!tmp.contains(entrie.getURL().getPath())) {
tmp.add(entrie.getURL().getPath());
}
}
}
}
}
try {
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(LASTPLATFORMS_FILE));
encoder.writeObject(tmp.toArray(new String[tmp.size()]));
encoder.flush();
encoder.close();
} catch (Exception ex) {
}
reference.set(tmp.toArray(new String[tmp.size()]));
}
};
POOL.execute(runnable);
}