📄 websession.java
字号:
{ break; } // Write to a string output.append(new String(buffer, 0, amountRead)); } } catch(IOException e) { output = new StringBuffer(HTMLParser.EMPTY_DOC); throw new STAFException( STAFResult.FileReadError, "Error getting current contents.\n" + e.toString()); } catch(OutOfMemoryError e) { output = new StringBuffer(); throw new ContentTooLargeException( "Getting the session content caused an OutOfMemoryError " + "because it is too large. Recommend redirecting the content " + "to a file using the FILE option or, if the content is not " + "needed, you may be able to specify the RETURNNOCONTENT " + "option depending on the request."); } finally { try { if (instream != null) instream.close(); } catch(IOException e) { throw new STAFException( STAFResult.FileReadError, "Error getting current contents while closing " + "stream.\n" + e.toString()); } } return output.toString(); } } /*****************************************************************************//* *//* Method: setCurrentContents *//* Description: Set the current contents for the last interaction by reading *//* from the input stream. If temporary file (e.g. fileName == null)*//* and contents <= 50, store the contents in a string. Otherwise, *//* store the contents in a file. *//* Also, sets the content for the HTML parser. *//* Parameters: instream - input stream containing the contents for the last *//* interaction for this session. *//* *//*****************************************************************************/ public void setCurrentContents(InputStream instream, String fileName, String machine) throws STAFException, IOException { currentContents = ""; currentContentsFileName = ""; if (instream == null) return; String localFileName = httpService.getTempDir() + "session" + id + ".tmp"; // Write the result string as a stream of raw bytes (e.g. // binary mode) BufferedInputStream bis = null; OutputStream outstream = null; StringBuffer output = new StringBuffer(); long totalRead = 0; final byte[] buffer = new byte[BUFF_SIZE]; // Read content from input stream and write content to a string if // fileName is not specified (e.g. is null) or if the content stream // size is small (e.g. <= MAX_STRING_SIZE). Otherwise, write the // content to a file. try { bis = new BufferedInputStream(instream); outstream = new DataOutputStream(new FileOutputStream( localFileName)); while (true) { int amountRead = bis.read(buffer); if (amountRead == -1) { break; } if ((fileName == null) && (totalRead <= MAX_STRING_SIZE)) { // Write to a string output.append(new String(buffer, 0, amountRead)); } // Write to a file outstream.write(buffer, 0, amountRead); totalRead += amountRead; } } catch(IOException e) { // Delete the temporary file before throwing the exception if (outstream != null) outstream.close(); (new File(localFileName)).delete(); throw new STAFException( STAFResult.FileWriteError, "Error setting current contents.\n" + e.toString()); } finally { try { if (bis != null) bis.close(); if (outstream != null) outstream.close(); } catch(IOException e) { (new File(localFileName)).delete(); throw new STAFException( STAFResult.FileWriteError, "Error setting current contents (when closing " + "streams).\n" + e.toString()); } } if ((fileName == null) && (totalRead <= MAX_STRING_SIZE)) { // Contents stored in a string currentContents = output.toString(); // Set current contents in the parser try { parser.setContent(currentContents); } catch(Exception e) { try { parser.setContent(HTMLParser.EMPTY_DOC); } catch (Exception ex) { // Do nothing. EMPTY_DOC is error free } } // Delete local temporary file (new File(localFileName)).delete(); } else { // Contents stored in a file currentContentsFileName = localFileName; output = new StringBuffer(""); // Set current contents in the parser try { parser.setContent(new FileInputStream( currentContentsFileName)); } catch(Exception e) { try { parser.setContent(HTMLParser.EMPTY_DOC); } catch (Exception ex) { // Do nothing. EMPTY_DOC is error free } } } }/*****************************************************************************//* *//* Method: writeContentToFile *//* Description: utility method to write an the current content to a file *//* Parameters: filename - string representation of a file *//* machineName - name of the machine to write the file to *//* Returns: void *//* *//*****************************************************************************/public void writeContentToFile(String toFile, String toMachine) throws IOException, STAFException{ // Store name for file redirect String fName = toFile; if (currentContentsFileName.equals("")) { // Current contents reside in a string. Write contents to a file. String fileName = toFile; if (toMachine != null) { // Create a temporary file name to use to store the content in fileName = httpService.getTempDir() + "file" + httpService.getTempFileCount() + ".tmp"; } File outFile = new File(fileName); FileWriter out = new FileWriter(outFile); out.write(currentContents); out.close(); if (toMachine == null) { // The contents have already been written to the specified file // on the local machine return; } // Copy the temporary file on the local machine to the specified file // on the specified machine. String request = "COPY FILE " + fileName + " TOFILE " + fName + " TOMACHINE " + toMachine; STAFResult res = httpService.getServiceHandle().submit2( "local", "FS", request); // Delete the temporary local file (new File(fileName)).delete(); if (res.rc != 0) { String errMsg = "Error copying the temporary file containing " + "the contents to the specified file.\n" + "STAF local FS " + request + " failed with RC=" + res.rc + " Result=" + res.result; throw new STAFException(res.rc, errMsg); } } else { // Current contents reside in a file on the local machine String request = ""; if (toMachine == null) { // Copy the contents file to a file on the local machine request = "COPY FILE " + currentContentsFileName + " TOFILE " + fName; } else { // Copy the contents file to a file on the specified machine request = "COPY FILE " + currentContentsFileName + " TOFILE " + fName + " TOMACHINE " + toMachine; } STAFResult res = httpService.getServiceHandle().submit2( "local", "FS", request); if (res.rc != 0) { String errMsg = "Error copying the temporary file where the " + "contents are stored to the specified file.\n" + "STAF local FS " + request + " failed with RC=" + res.rc + " Result=" + res.result; throw new STAFException(res.rc, errMsg); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -