本文整理汇总了Java中com.helger.commons.io.stream.StreamHelper.getAllBytesAsString方法的典型用法代码示例。如果您正苦于以下问题:Java StreamHelper.getAllBytesAsString方法的具体用法?Java StreamHelper.getAllBytesAsString怎么用?Java StreamHelper.getAllBytesAsString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.helger.commons.io.stream.StreamHelper
的用法示例。
在下文中一共展示了StreamHelper.getAllBytesAsString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExternalEntityExpansion
import com.helger.commons.io.stream.StreamHelper; //导入方法依赖的package包/类
@Test
public void testExternalEntityExpansion () throws SAXException
{
// Include a dummy file
final File aFile = new File ("src/test/resources/test1.txt");
assertTrue (aFile.exists ());
final String sFileContent = StreamHelper.getAllBytesAsString (new FileSystemResource (aFile),
StandardCharsets.ISO_8859_1);
// The XML with XXE problem
final String sXML = "<?xml version='1.0' encoding='utf-8'?>" +
"<!DOCTYPE root [" +
" <!ELEMENT root ANY >" +
" <!ENTITY xxe SYSTEM \"" +
FileHelper.getAsURLString (aFile) +
"\" >]>" +
"<root>&xxe;</root>";
final DOMReaderSettings aDRS = new DOMReaderSettings ().setEntityResolver ( (publicId,
systemId) -> InputSourceFactory.create (new URLResource (systemId)));
// Read successful - entity expansion!
final Document aDoc = DOMReader.readXMLDOM (sXML, aDRS);
assertNotNull (aDoc);
assertEquals (sFileContent, aDoc.getDocumentElement ().getTextContent ());
// Should fail because inline DTD is present
try
{
DOMReader.readXMLDOM (sXML, aDRS.getClone ().setFeatureValues (EXMLParserFeature.AVOID_XXE_SETTINGS));
fail ();
}
catch (final SAXParseException ex)
{
// Expected
assertTrue (ex.getMessage ().contains ("http://apache.org/xml/features/disallow-doctype-decl"));
}
}
示例2: testIssue
import com.helger.commons.io.stream.StreamHelper; //导入方法依赖的package包/类
@Test
public void testIssue ()
{
final String css = StreamHelper.getAllBytesAsString (new FileSystemResource ("src/test/resources/testfiles/css30/good/issue35.css"),
StandardCharsets.UTF_8);
final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
.setBrowserCompliantMode (false);
final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
assertNotNull (cascadingStyleSheet);
final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
assertNotNull (writer.getCSSAsString (cascadingStyleSheet));
}
示例3: processAS4UserMessage
import com.helger.commons.io.stream.StreamHelper; //导入方法依赖的package包/类
@Nonnull
public AS4MessageProcessorResult processAS4UserMessage (@Nonnull final Ebms3UserMessage aUserMessage,
@Nonnull final IPMode aPMode,
@Nullable final Node aPayload,
@Nullable final ICommonsList <WSS4JAttachment> aIncomingAttachments,
@Nonnull final IAS4MessageState aState)
{
// Needed for AS4_TA13 because we want to force a decompression failure and
// for that to happen the stream has to be read
{
s_aLogger.info ("Received AS4 message:");
s_aLogger.info (" UserMessage: " + aUserMessage);
s_aLogger.info (" Payload: " +
(aPayload == null ? "null" : true ? "present" : XMLWriter.getNodeAsString (aPayload)));
if (aIncomingAttachments != null)
{
s_aLogger.info (" Attachments: " + aIncomingAttachments.size ());
for (final WSS4JAttachment x : aIncomingAttachments)
{
s_aLogger.info (" Attachment Content Type: " + x.getMimeType ());
if (x.getMimeType ().startsWith ("text") || x.getMimeType ().endsWith ("/xml"))
{
try
{
final InputStream aIS = x.getSourceStream ();
s_aLogger.info (" Attachment Stream Class: " + aIS.getClass ().getName ());
final String sContent = StreamHelper.getAllBytesAsString (x.getSourceStream (), x.getCharset ());
s_aLogger.info (" Attachment Content: " + sContent.length () + " chars");
}
catch (final IllegalStateException ex)
{
s_aLogger.warn (" Attachment Content: CANNOT BE READ", ex);
}
}
}
}
}
// To test returning with a failure works as intended
if (aUserMessage.getCollaborationInfo ().getAction ().equals (ACTION_FAILURE))
{
return AS4MessageProcessorResult.createFailure (ACTION_FAILURE);
}
return AS4MessageProcessorResult.createSuccess ();
}
示例4: testBasePath
import com.helger.commons.io.stream.StreamHelper; //导入方法依赖的package包/类
@Test
public void testBasePath () throws IOException
{
final IFileRelativeIO aIO = FileRelativeIO.createForCurrentDir ();
final String sTestFile = "testfile";
final String sTestContent = "This is the test file content";
final String sTestFile2 = "testfile2";
// may not exist
assertFalse (aIO.existsFile (sTestFile));
try
{
// write file
final OutputStream aOS = aIO.getOutputStream (sTestFile);
assertNotNull (aOS);
aOS.write (sTestContent.getBytes (StandardCharsets.ISO_8859_1));
assertTrue (StreamHelper.close (aOS).isSuccess ());
// rename a to b
assertTrue (aIO.existsFile (sTestFile));
assertTrue (aIO.renameFile (sTestFile, sTestFile2).isSuccess ());
// ensure only b is present
assertFalse (aIO.existsFile (sTestFile));
assertTrue (aIO.existsFile (sTestFile2));
// rename back from b to a
assertTrue (aIO.renameFile (sTestFile2, sTestFile).isSuccess ());
// ensure only a is present
assertTrue (aIO.existsFile (sTestFile));
assertFalse (aIO.existsFile (sTestFile2));
// read file
try (final InputStream aIS = aIO.getResource (sTestFile).getInputStream ())
{
assertNotNull (aIS);
final String sReadContent = StreamHelper.getAllBytesAsString (aIS, StandardCharsets.ISO_8859_1);
assertEquals (sTestContent, sReadContent);
}
}
finally
{
// ensure all files are gone :)
if (aIO.existsFile (sTestFile))
assertTrue (aIO.deleteFile (sTestFile).isSuccess ());
if (aIO.existsFile (sTestFile2))
assertTrue (aIO.deleteFile (sTestFile2).isSuccess ());
assertFalse (aIO.existsFile (sTestFile));
}
}
示例5: getFileAsString
import com.helger.commons.io.stream.StreamHelper; //导入方法依赖的package包/类
/**
* Get the content of the passed file as a string using the system line
* separator. Note: the last line does not end with the passed line separator.
*
* @param aFile
* The file to read. May be <code>null</code>.
* @param aCharset
* The character set to use. May not be <code>null</code>.
* @return <code>null</code> if the file does not exist, the content
* otherwise.
*/
@Nullable
public static String getFileAsString (@Nullable final File aFile, @Nonnull final Charset aCharset)
{
return aFile == null ? null : StreamHelper.getAllBytesAsString (FileHelper.getInputStream (aFile), aCharset);
}