アプリ版:「スタンプのみでお礼する」機能のリリースについて

教えて下さい。
あるファイルの1バイトづつローテートを行いファイルの書き出しを行っています。(ビット演算による暗号化)
しかし以下の処理の場合、サイズの大きいファイルを扱った場合に多くの処理時間がかかってしまいます。
もっと処理を高速化する事は可能でしょうか?


-----------------------------------------------------------------
' 読み込みファイル
Private Const READ_FILE As String = "c:\temp\date.Text"
' 書き込みファイル
Private WRIT_FILE As String = "c:\temp\date.dat"

' オブジェクト作成
Dim br As New System.IO.FileStream(READ_FILE, IO.FileMode.Open, IO.FileAccess.Read)
Dim bw As New System.IO.StreamWriter(WRIT_FILE, False, System.Text.Encoding.GetEncoding("iso-8859-1"))
Dim m As Integer = 3 ' 3ビット左回転
Dim n As Integer

' 1バイトづつ処理
For I As Integer = 0 To CType(br.Length, Integer) - 1

' 16進数を10進数へ変換
n = ("&h" & String.Format("{0:X2}", br.ReadByte))

' mビット左へローテート処理を行いファイルへ書き込み
bw.Write(ChrW("&h" & Hex(Int((n / 2 ^ (8 - m)) + (n * 2 ^ m And 255)))))

Next

' 閉じる
bw.Close()
br.Close()
-----------------------------------------------------------------


よろしくお願いします。

A 回答 (2件)

> ' 16進数を10進数へ変換


> n = ("&h" & String.Format("{0:X2}", br.ReadByte))
コメントとやっていることが合っていません。
Byte型を &h75 の様な文字列に変換し、
その後暗黙の型変換でInteger型に変換するという
(無駄な)処理がここでは行われています。


Byte型は数値なのでそのまま演算処理すれば良いです。

素直にこんな感じに書いてみては?
===================================
' インデントに全角空白を使っています。要削除。
Imports System
Imports System.IO

Class BRotate
 Shared Sub main(ByVal args As String())

  Dim srcName As String = args(0) ' コピー元のファイル名
  Dim destName As String = args(1) ' コピー先のファイル名

  Dim BUFSIZE As Integer = 2048 ' 1度に処理するサイズ
  Dim buf(BUFSIZE) As Byte ' 読み込み用バッファ
  Dim m As Integer = 3 ' 3ビット左回転

  Dim readSize As Integer ' Readメソッドで読み込んだバイト数

  Using src As New FileStream( _
    srcName, FileMode.Open, FileAccess.Read)
   Using dest As New FileStream( _
     destName, FileMode.Create, FileAccess.Write)

    While True
     readSize = src.Read(buf, 0, BUFSIZE) ' 読み込み

     If readSize = 0 Then
      Exit While ' 完了
     End If

     For i As Integer = 0 To readSize - 1
       buf(i) = (buf(i) << m) Or (buf(i) >> (8-m)) ' ビット回転
     Next

     dest.Write(buf, 0, readSize) ' 書き込み

    End While
   End Using
  End Using
 End Sub
End Class

' 参考にしたページ:以下のページのファイルコピープログラムをベースにした
' バイナリ・ファイルを読み書きするには?[C#、VB] - @IT
' http://www.atmarkit.co.jp/fdotnet/dotnettips/669 …
    • good
    • 0
この回答へのお礼

ありがとうございました!
信じられないくらい高速化する事が出来ました!
(一瞬で処理が終わる!)

まじありがとおおおおおおおおおおw

お礼日時:2014/05/21 09:36

C#しか書けないけど、こんな感じ




//変換表を作成
byte[] convList = new byte[256];
for (byte i = 0; i <= 255; i++) {
convList[i] = (byte)((i << 3) & 255 | (i >> 5));
}

using (FileStream reader = new FileStream(~))
using (FileStream writer = new FileStream(~)) {
while (true) {
int c = reader.ReadByte();
if (c == -1)
break;
writer.WriteByte(convList[c]);
}
}
    • good
    • 0
この回答へのお礼

ありがとうございました。

お礼日時:2014/05/21 11:04

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています