XmlTextWriter.WriteBase64 メソッド
指定したバイナリ バイトを base64 としてエンコードし、その結果生成されるテキストを書き込みます。
Overrides Public Sub WriteBase64( _
ByVal buffer() As Byte, _ ByVal index As Integer, _ ByVal count As Integer _)
[C#]
public override void WriteBase64(byte[] buffer,intindex,intcount);
[C++]
public: void WriteBase64(unsigned charbuffer __gc[],intindex,intcount);
[JScript]
public override function WriteBase64(
buffer : Byte[],index : int,count : int);
パラメータ
- buffer
エンコードするバイト配列。 - index
書き込むバイトの開始を示すバッファ内の位置。 - count
書き込むバイト数。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | buffer が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | バッファ長から index を差し引いた値が count より小さい値です。 |
ArgumentOutOfRangeException | index または count が 0 未満です。 |
InvalidOperationException | WriteState が Closed です。 |
使用例
[Visual Basic, C#, C++] WriteBase64 を使用して入力ファイルをエンコードし、一時 XML ファイルを生成する例を次に示します。一時 XML ファイルは、 ReadBase64 メソッドを使用してデコードされ、元のファイルと比較されます。
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
class TestBase64
private const bufferSize as integer = 4096
public shared sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
Dim myTestBase64 as TestBase64 = new TestBase64()
'Check that the usage string is correct.
if (args.Length < 3 )
myTestBase64.Usage()
return
end if
Dim fileOld as FileStream = new FileStream(args(1), FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
myTestBase64.EncodeXmlFile("temp.xml", fileOld)
Dim fileNew as FileStream = new FileStream(args(2), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
myTestBase64.DecodeOrignalObject("temp.xml", fileNew)
'Compare the two files.
if (myTestBase64.CompareResult(fileOld, fileNew))
Console.WriteLine("The recreated binary file {0} is the same as {1}", args(2), args(1))
else
Console.WriteLine("The recreated binary file {0} is not the same as {1}", args(2), args(1))
end if
fileOld.Flush()
fileNew.Flush()
fileOld.Close()
fileNew.Close()
end sub
'Use the WriteBase64 method to create an XML document. The object
'passed by the user is encoded and included in the document.
public shared sub EncodeXmlFile(xmlFileName as String, fileOld as FileStream)
Dim buffer(bufferSize) as byte
Dim readByte as integer=0
Dim xw as XmlTextWriter = new XmlTextWriter(xmlFileName, Encoding.UTF8)
xw.WriteStartDocument()
xw.WriteStartElement("root")
' Create a Char writer.
Dim br as BinaryReader = new BinaryReader(fileOld)
' Set the file pointer to the end.
try
do
readByte=br.Read(buffer, 0, bufferSize)
xw.WriteBase64(buffer, 0, readByte)
loop while (bufferSize <= readByte )
catch ex as Exception
Dim ex1 as EndOfStreamException = new EndOfStreamException()
if (ex1.Equals(ex))
Console.WriteLine("We are at end of file")
else
Console.WriteLine(ex)
end if
end try
xw.WriteEndElement()
xw.WriteEndDocument()
xw.Flush()
xw.Close()
end sub
'Use the ReadBase64 method to decode the new XML document
'and generate the original object.
public shared sub DecodeOrignalObject(xmlFileName as String, fileNew as FileStream)
Dim buffer(bufferSize) as byte
Dim readByte as integer =0
'Create a file to write the bmp back.
Dim bw as BinaryWriter = new BinaryWriter(fileNew)
Dim tr as XmlTextReader = new XmlTextReader(xmlFileName)
tr.MoveToContent()
Console.WriteLine(tr.Name)
do
readByte=tr.ReadBase64(buffer, 0, bufferSize)
bw.Write(buffer, 0, readByte)
loop while(readByte>=bufferSize)
bw.Flush()
end sub
'Compare the two files.
public function CompareResult(fileOld as FileStream, fileNew as FileStream) as boolean
Dim readByteOld as integer=0
Dim readByteNew as integer=0
Dim count as integer
Dim readByte as integer=0
Dim bufferOld(bufferSize) as byte
Dim bufferNew(bufferSize) as byte
Dim binaryReaderOld as BinaryReader = new BinaryReader(fileOld)
Dim binaryReaderNew as BinaryReader = new BinaryReader(fileNew)
binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin)
binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin)
do
readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize)
readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize)
if not (readByteOld=readByteNew)
return false
end if
for count=0 to bufferSize-1
if not (bufferOld(count)=bufferNew(count))
return false
end if
next
loop while (count<readByte)
return true
end function
'Display the usage statement.
public shared sub Usage()
Console.WriteLine("TestBase64 sourceFile, targetFile")
Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp")
end sub
end class
[C#]
using System;
using System.IO;
using System.Xml;
using System.Text;
class TestBase64 {
private const int bufferSize=4096;
public static void Main(String[] args) {
TestBase64 testBase64=new TestBase64();
//Check that the usage string is correct.
if (args.Length < 2 ) {
testBase64.Usage();
return;
}
FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
testBase64.EncodeXmlFile("temp.xml", fileOld);
FileStream fileNew = new FileStream(args[1], FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
testBase64.DecodeOrignalObject("temp.xml", fileNew);
//Compare the two files.
if (testBase64.CompareResult( fileOld, fileNew)) {
Console.WriteLine("The recreated binary file {0} is the same as {1}", args[1], args[0] );
} else {
Console.WriteLine("The recreated binary file {0} is not the same as {1}", args[1], args[0] );
}
fileOld.Flush();
fileNew.Flush();
fileOld.Close();
fileNew.Close();
}
//Use the WriteBase64 method to create an XML document. The object
//passed by the user is encoded and included in the document.
public void EncodeXmlFile(String xmlFileName, FileStream fileOld) {
byte[] buffer = new byte[bufferSize];
int readByte=0;
XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.UTF8);
xw.WriteStartDocument();
xw.WriteStartElement("root");
// Create a Char writer.
BinaryReader br = new BinaryReader(fileOld);
// Set the file pointer to the end.
try {
do {
readByte=br.Read(buffer, 0, bufferSize);
xw.WriteBase64(buffer, 0, readByte);
} while (bufferSize <= readByte );
} catch (Exception ex) {
EndOfStreamException ex1= new EndOfStreamException();
if (ex1.Equals(ex)) {
Console.WriteLine("We are at end of file");
} else {
Console.WriteLine(ex);
}
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
}
//Use the ReadBase64 method to decode the new XML document
//and generate the original object.
public void DecodeOrignalObject(String xmlFileName, FileStream fileNew) {
byte[] buffer = new byte[bufferSize];
int readByte=0;
//Create a file to write the bmp back.
BinaryWriter bw = new BinaryWriter(fileNew);
XmlTextReader tr = new XmlTextReader(xmlFileName);
tr.MoveToContent();
Console.WriteLine(tr.Name);
do {
readByte=tr.ReadBase64(buffer, 0, bufferSize);
bw.Write(buffer, 0, readByte);
} while(readByte>=bufferSize);
bw.Flush();
}
//Compare the two files.
public bool CompareResult(FileStream fileOld, FileStream fileNew) {
int readByteOld=0;
int readByteNew=0;
int count, readByte=0;
byte[] bufferOld = new byte[bufferSize];
byte[] bufferNew = new byte[bufferSize];
BinaryReader binaryReaderOld = new BinaryReader(fileOld);
BinaryReader binaryReaderNew = new BinaryReader(fileNew);
binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin);
binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin);
do {
readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize);
readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize);
if (readByteOld!=readByteNew) {
return false;
}
for (count=0; count <bufferSize; ++count) {
if (bufferOld[count]!=bufferNew[count]) {
return false;
}
}
} while (count<readByte );
return true;
}
//Display the usage statement.
public void Usage() {
Console.WriteLine("TestBase64 sourceFile, targetFile \n");
Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp\n");
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Text;
__gc class TestBase64
{
private:
static int bufferSize = 4096;
public:
// Use the WriteBase64 method to create an XML document. The object
// passed by the user is encoded and included in the document.
void EncodeXmlFile(String * xmlFileName, FileStream *fileOld)
{
Byte buffer[] = new Byte[bufferSize];
int readByte = 0;
XmlTextWriter* xw = new XmlTextWriter(xmlFileName, Encoding::UTF8);
xw -> WriteStartDocument();
xw -> WriteStartElement(S"root");
// Create a Char writer.
BinaryReader* br = new BinaryReader(fileOld);
// Set the file pointer to the end.
try
{
do
{
readByte = br -> Read(buffer, 0, bufferSize);
xw -> WriteBase64(buffer, 0, readByte);
} while (bufferSize <= readByte);
}
catch (Exception* ex)
{
EndOfStreamException* ex1 = new EndOfStreamException();
if (ex1 -> Equals(ex))
Console::WriteLine(S"We are at end of file");
else
Console::WriteLine(ex);
}
xw -> WriteEndElement();
xw -> WriteEndDocument();
xw -> Flush();
xw -> Close();
}
// Use the ReadBase64 method to decode the new XML document
// and generate the original object.
void DecodeOrignalObject(String * xmlFileName, FileStream * fileNew)
{
Byte buffer[] = new Byte[bufferSize];
int readByte = 0;
// Create a file to write the bmp back.
BinaryWriter* bw = new BinaryWriter(fileNew);
XmlTextReader* tr = new XmlTextReader(xmlFileName);
tr -> MoveToContent();
Console::WriteLine(tr -> Name);
do
{
readByte = tr -> ReadBase64(buffer, 0, bufferSize);
bw -> Write(buffer, 0, readByte);
} while(readByte >= bufferSize);
bw -> Flush();
}
// Compare the two files.
bool CompareResult(FileStream * fileOld, FileStream * fileNew)
{
int readByteOld = 0;
int readByteNew = 0;
int count, readByte = 0;
Byte bufferOld[] = new Byte[bufferSize];
Byte bufferNew[] = new Byte[bufferSize];
BinaryReader* binaryReaderOld = new BinaryReader(fileOld);
BinaryReader* binaryReaderNew = new BinaryReader(fileNew);
binaryReaderOld -> BaseStream -> Seek(0, SeekOrigin::Begin);
binaryReaderNew -> BaseStream -> Seek(0, SeekOrigin::Begin);
do
{
readByteOld = binaryReaderOld -> Read(bufferOld, 0, bufferSize);
readByteNew = binaryReaderNew -> Read(bufferNew, 0, bufferSize);
if (readByteOld != readByteNew)
return false;
for (count = 0; count < bufferSize; ++count)
if (bufferOld -> Item[count] != bufferNew -> Item[count])
return false;
} while (count<readByte);
return true;
}
// Display the usage statement.
void Usage()
{
Console::WriteLine(S"TestBase64 sourceFile, targetFile \n");
Console::WriteLine(S"For example: TestBase64 winlogon.bmp, target.bmp\n");
}
};
int main()
{
String* args[] = Environment::GetCommandLineArgs();
TestBase64* testBase64 = new TestBase64();
// Check that the usage is correct.
if (args -> Length < 3)
{
testBase64 -> Usage();
return 1;
}
FileStream* fileOld = new FileStream(args[1], FileMode::OpenOrCreate, FileAccess::Read, FileShare::Read);
testBase64 -> EncodeXmlFile(S"temp.xml", fileOld);
FileStream* fileNew = new FileStream(args[2], FileMode::Create, FileAccess::ReadWrite, FileShare::ReadWrite);
testBase64 -> DecodeOrignalObject(S"temp.xml", fileNew);
// Compare the two files.
if (testBase64 -> CompareResult(fileOld, fileNew))
Console::WriteLine(S"The recreated binary file {0} is the same as {1}", args[2], args[1]);
else
Console::WriteLine(S"The recreated binary file {0} is not the same as {1}", args[2], args[1]);
fileOld -> Flush();
fileNew -> Flush();
fileOld -> Close();
fileNew -> Close();
return 0;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard