public boolean WriteBytesToStream( XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes) { // get output stream of substream XOutputStream xOutput = xStream.getOutputStream(); if (xOutput == null) { Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!"); return false; } // get XTrucate implementation from output stream XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput); if (xTruncate == null) { Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!"); return false; } // write requested byte sequence try { xTruncate.truncate(); xOutput.writeBytes(pBytes); } catch (Exception e) { Error("Can't write to stream '" + sStreamName + "', exception: " + e); return false; } // get access to the XPropertySet interface XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStream); if (xPropSet == null) { Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!"); return false; } // set properties to the stream try { xPropSet.setPropertyValue("MediaType", sMediaType); xPropSet.setPropertyValue("Compressed", new Boolean(bCompressed)); } catch (Exception e) { Error("Can't set properties to substream '" + sStreamName + "', exception: " + e); return false; } // check size property of the stream try { long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size")); if (nSize != pBytes.length) { Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!"); return false; } } catch (Exception e) { Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e); return false; } return true; }
/** * Test calls the method and checks return value and that no exceptions were thrown. * <b>XSimpleFileAccess2.txt</b> file used. * * <p>Has <b> OK </b> status if the method returns not <code>null</code> value and no exceptions * were thrown. * * <p> */ public void _openFileReadWrite() { try { String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()); String copiedFile = dirnameTo + "XSimpleFileAccess2.txt"; if (oObj.exists(copiedFile)) oObj.kill(copiedFile); com.sun.star.io.XStream aStream = oObj.openFileReadWrite(copiedFile); aStream.getInputStream().closeInput(); aStream.getOutputStream().closeOutput(); oObj.kill(copiedFile); tRes.tested("openFileReadWrite()", true); } catch (com.sun.star.uno.Exception ex) { log.println("Exception occurred while testing 'openFileReadWrite()'"); ex.printStackTrace(log); tRes.tested("openFileReadWrite()", false); } } // EOF openFileReadWrite()
public boolean closeOutput(XStream xStream) { XOutputStream xOutTemp = null; try { xOutTemp = xStream.getOutputStream(); if (xOutTemp == null) { Error("Can't get the output part of a stream!"); return false; } } catch (Exception e) { Error("Can't get the output part of a stream, exception :" + e); return false; } try { xOutTemp.closeOutput(); } catch (Exception e) { Error("Can't close output part of a stream, exception :" + e); return false; } return true; }
public String CreateTempFile(XMultiServiceFactory xMSF) { String sResult = null; // try to get temporary file representation XPropertySet xTempFileProps = null; try { Object oTempFile = xMSF.createInstance("com.sun.star.io.TempFile"); xTempFileProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oTempFile); } catch (Exception e) { } if (xTempFileProps != null) { try { xTempFileProps.setPropertyValue("RemoveFile", new Boolean(false)); sResult = AnyConverter.toString(xTempFileProps.getPropertyValue("Uri")); } catch (Exception e) { Error("Can't control TempFile properties, exception: " + e); } } else { Error("Can't create temporary file representation!"); } // close temporary file explicitly try { XStream xStream = (XStream) UnoRuntime.queryInterface(XStream.class, xTempFileProps); if (xStream != null) { XOutputStream xOut = xStream.getOutputStream(); if (xOut != null) xOut.closeOutput(); XInputStream xIn = xStream.getInputStream(); if (xIn != null) xIn.closeInput(); } else Error("Can't close TempFile!"); } catch (Exception e) { Error("Can't close TempFile, exception: " + e); } return sResult; }
public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage, String sStreamName, String sMediaType, byte[] pBytes) { // open substream element XStream xSubStream = null; try { Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE); xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream); if (xSubStream == null) { Error("Can't create substream '" + sStreamName + "'!"); return false; } } catch (Exception e) { Error("Can't create substream '" + sStreamName + "', exception : " + e + "!"); return false; } // get output stream of substream XOutputStream xOutput = xSubStream.getOutputStream(); if (xOutput == null) { Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!"); return false; } // get XTrucate implementation from output stream XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput); if (xTruncate == null) { Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!"); return false; } // write requested byte sequence try { xTruncate.truncate(); xOutput.writeBytes(pBytes); } catch (Exception e) { Error("Can't write to stream '" + sStreamName + "', exception: " + e); return false; } // get access to the XPropertySet interface XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream); if (xPropSet == null) { Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!"); return false; } // set properties to the stream // do not set the compressed property try { xPropSet.setPropertyValue("MediaType", sMediaType); } catch (Exception e) { Error("Can't set properties to substream '" + sStreamName + "', exception: " + e); return false; } // check size property of the stream try { long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size")); if (nSize != pBytes.length) { Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!"); return false; } } catch (Exception e) { Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e); return false; } // free the stream resources, garbage collector may remove the object too late if (!disposeStream(xSubStream, sStreamName)) return false; return true; }