T-CREATOR

Node.js バッファ(Buffer)でバイナリデータを扱う方法

Node.js バッファ(Buffer)でバイナリデータを扱う方法

Node.js で Web アプリケーションや API を開発していると、「なぜ画像ファイルが正しく処理されないのか?」「ネットワーク通信でデータが文字化けしてしまう」といった問題に遭遇することがありますよね。これらの多くは、バイナリデータの扱い方が原因となっています。

テキストデータとは異なり、バイナリデータは人間が直接読むことができない形式で保存されており、適切な処理方法を理解していないと予期しない動作を引き起こしてしまいます。特に、ファイルアップロード機能、画像処理、ネットワーク通信、暗号化処理などを実装する際には、バイナリデータの正しい扱い方が不可欠です。

本記事では、Node.js の Buffer クラスを使ったバイナリデータ処理の基本から実用的な活用方法まで、具体的なコード例とともに詳しく解説いたします。初心者の方でも理解しやすいよう、基本概念から始めて、実際の開発現場で役立つ実践的なテクニックまで段階的にご紹介していきます。

Buffer の基本概念

バイナリデータとテキストデータの違い

プログラミングにおいて、データは大きく「テキストデータ」と「バイナリデータ」に分類されます。この違いを理解することが、Buffer を適切に使用する第一歩となります。

項目テキストデータバイナリデータ
可読性人間が直接読める人間が直接読めない
文字エンコーディングUTF-8、ASCII などエンコーディングに依存しない
データ例JSON、HTML、CSV画像、音声、実行ファイル
処理方法文字列として操作バイト配列として操作
javascript// テキストデータの例
const textData = 'こんにちは、世界!';
console.log(textData); // そのまま読める

// バイナリデータの例(同じ文字列をバイナリとして表現)
const binaryData = Buffer.from(
  'こんにちは、世界!',
  'utf8'
);
console.log(binaryData);
// <Buffer e3 81 93 e3 81 93 e3 81 ab e3 81 a1 e3 81 af e3 80 81 e4 b8 96 e7 95 8c ef bc 81>

このように、同じ内容でもバイナリデータとして扱うと、バイト列(16 進数)として表現されます。これが Buffer の基本的な役割です。

Node.js での Buffer の役割と特徴

Buffer は Node.js が提供するグローバルクラスで、バイナリデータを効率的に操作するために設計されています。JavaScript の標準的な文字列処理では対応できない、低レベルなデータ操作を可能にします。

javascript// Buffer の基本的な特徴を確認
const buffer = Buffer.alloc(10); // 10バイトのBufferを作成

console.log('Buffer サイズ:', buffer.length); // 10
console.log('Buffer 内容:', buffer); // <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log('型確認:', buffer instanceof Buffer); // true
console.log('配列的アクセス:', buffer[0]); // 0(初期値)

// Buffer は配列のようにアクセス可能
buffer[0] = 255;
buffer[1] = 128;
console.log('変更後:', buffer); // <Buffer ff 80 00 00 00 00 00 00 00 00>

Buffer の主な特徴:

  1. 固定サイズ: 作成時にサイズが決定され、後から変更できません
  2. バイト単位操作: 各要素は 0-255 の値を持つ 1 バイト
  3. メモリ効率: C++ レベルで実装されており、高速なメモリ操作が可能
  4. 型安全性: TypedArray の一種で、型安全なバイナリデータ操作を提供

メモリ上でのデータ表現

Buffer がメモリ上でどのようにデータを表現するかを理解することで、より効率的な処理が可能になります。

javascript// 異なるデータ型のメモリ表現を確認
const examples = {
  // 整数の表現
  integer: (() => {
    const buf = Buffer.alloc(4);
    buf.writeInt32BE(0x12345678, 0); // Big Endian で書き込み
    return {
      buffer: buf,
      hex: buf.toString('hex'),
      decimal: buf.readInt32BE(0),
    };
  })(),

  // 浮動小数点数の表現
  float: (() => {
    const buf = Buffer.alloc(4);
    buf.writeFloatBE(3.14159, 0);
    return {
      buffer: buf,
      hex: buf.toString('hex'),
      decimal: buf.readFloatBE(0),
    };
  })(),

  // 文字列の UTF-8 表現
  string: (() => {
    const buf = Buffer.from('Hello世界', 'utf8');
    return {
      buffer: buf,
      hex: buf.toString('hex'),
      original: buf.toString('utf8'),
    };
  })(),
};

console.log('データ型別メモリ表現:');
Object.entries(examples).forEach(([type, data]) => {
  console.log(`${type}:`, data);
});

この実行結果を見ると、同じデータでも型によってメモリ上の表現が大きく異なることが分かります。Buffer を使用することで、これらの低レベルな表現を直接操作できるようになります。

javascript// エンディアンの違いを実際に確認
const number = 0x12345678;
const bigEndianBuf = Buffer.alloc(4);
const littleEndianBuf = Buffer.alloc(4);

// Big Endian(ネットワークバイトオーダー)
bigEndianBuf.writeInt32BE(number, 0);
console.log('Big Endian:', bigEndianBuf.toString('hex')); // 12345678

// Little Endian(Intel x86 アーキテクチャ)
littleEndianBuf.writeInt32LE(number, 0);
console.log(
  'Little Endian:',
  littleEndianBuf.toString('hex')
); // 78563412

// 読み取り時も対応するメソッドを使用
console.log('BE 読み取り:', bigEndianBuf.readInt32BE(0)); // 305419896
console.log('LE 読み取り:', littleEndianBuf.readInt32LE(0)); // 305419896

このように、Buffer を使用することで、メモリレベルでのデータ操作が可能になり、ネットワーク通信やファイル処理において正確なバイナリデータの処理を実現できます。

Buffer の作成と基本操作

Buffer.alloc()、Buffer.allocUnsafe()、Buffer.from() の使い分け

Buffer を作成する方法は主に 3 つありますが、それぞれ異なる特徴と用途があります。適切な方法を選択することで、安全性とパフォーマンスの両方を確保できます。

javascript// 1. Buffer.alloc() - 安全な初期化済みBuffer
const safeBuffer = Buffer.alloc(10); // 10バイト、全て0で初期化
console.log('alloc():', safeBuffer);
// <Buffer 00 00 00 00 00 00 00 00 00 00>

const safeBufferWithFill = Buffer.alloc(5, 'A'); // 'A'で埋められた5バイト
console.log('alloc() with fill:', safeBufferWithFill);
// <Buffer 41 41 41 41 41>

// 2. Buffer.allocUnsafe() - 高速だが初期化されていない
const unsafeBuffer = Buffer.allocUnsafe(10);
console.log('allocUnsafe():', unsafeBuffer);
// <Buffer ?? ?? ?? ?? ?? ?? ?? ?? ?? ??> (ランダムなメモリ内容)

// 安全のため、使用前に必ず初期化
unsafeBuffer.fill(0);
console.log('初期化後:', unsafeBuffer);
// <Buffer 00 00 00 00 00 00 00 00 00 00>

// 3. Buffer.from() - 既存データからBufferを作成
const fromString = Buffer.from('Hello, 世界!', 'utf8');
console.log('from string:', fromString);

const fromArray = Buffer.from([72, 101, 108, 108, 111]); // "Hello"のASCIIコード
console.log('from array:', fromArray);
console.log('配列から文字列:', fromArray.toString('ascii'));

const fromBuffer = Buffer.from(fromString); // 既存Bufferのコピー
console.log('from buffer:', fromBuffer);

使い分けの指針

メソッド用途セキュリティパフォーマンス
Buffer.alloc()一般的な用途、初期値が重要高(自動初期化)中(初期化コスト)
Buffer.allocUnsafe()高頻度作成、すぐに上書き低(要手動初期化)高(初期化なし)
Buffer.from()既存データの変換高(データ依存)中(変換コスト)
javascript// 実用的な使い分け例

// ケース1: ファイル読み込み用バッファ(安全性重視)
function createFileBuffer(size) {
  return Buffer.alloc(size); // 確実に初期化されたバッファ
}

// ケース2: 高頻度データ処理(パフォーマンス重視)
function processHighFrequencyData(dataArray) {
  const buffer = Buffer.allocUnsafe(dataArray.length);

  // すぐに実データで上書きするため、allocUnsafeが適切
  for (let i = 0; i < dataArray.length; i++) {
    buffer[i] = dataArray[i];
  }

  return buffer;
}

// ケース3: データ変換(既存データ活用)
function convertToBuffer(input) {
  if (typeof input === 'string') {
    return Buffer.from(input, 'utf8');
  } else if (Array.isArray(input)) {
    return Buffer.from(input);
  } else if (Buffer.isBuffer(input)) {
    return Buffer.from(input); // コピーを作成
  }

  throw new Error('Unsupported input type');
}

// 使用例
const fileBuffer = createFileBuffer(1024);
const processedData = processHighFrequencyData([
  1, 2, 3, 4, 5,
]);
const convertedBuffer = convertToBuffer('テストデータ');

console.log('ファイルバッファサイズ:', fileBuffer.length);
console.log('処理済みデータ:', processedData);
console.log(
  '変換されたバッファ:',
  convertedBuffer.toString()
);

サイズ指定と初期化方法

Buffer のサイズ管理と初期化は、メモリ効率と処理性能に直接影響します。適切なサイズ指定により、メモリリークを防ぎ、最適なパフォーマンスを実現できます。

javascript// サイズ指定の基本パターン
class BufferManager {
  constructor() {
    this.buffers = new Map();
  }

  // 固定サイズバッファの作成
  createFixedBuffer(name, size, fillValue = 0) {
    const buffer = Buffer.alloc(size, fillValue);
    this.buffers.set(name, buffer);

    console.log(`${name} バッファ作成: ${size}バイト`);
    return buffer;
  }

  // 動的サイズバッファの作成
  createDynamicBuffer(name, data) {
    let buffer;

    if (typeof data === 'string') {
      // 文字列の場合、UTF-8エンコード後のサイズを計算
      buffer = Buffer.from(data, 'utf8');
    } else if (Array.isArray(data)) {
      // 配列の場合、要素数がサイズ
      buffer = Buffer.from(data);
    } else if (typeof data === 'number') {
      // 数値の場合、その値をサイズとして使用
      buffer = Buffer.alloc(data);
    }

    this.buffers.set(name, buffer);
    console.log(
      `${name} バッファ作成: ${buffer.length}バイト`
    );
    return buffer;
  }

  // バッファの初期化パターン
  initializeBuffer(name, pattern) {
    const buffer = this.buffers.get(name);
    if (!buffer) {
      throw new Error(`Buffer ${name} not found`);
    }

    switch (pattern.type) {
      case 'zero':
        buffer.fill(0);
        break;
      case 'sequence':
        for (let i = 0; i < buffer.length; i++) {
          buffer[i] = i % 256;
        }
        break;
      case 'random':
        for (let i = 0; i < buffer.length; i++) {
          buffer[i] = Math.floor(Math.random() * 256);
        }
        break;
      case 'pattern':
        const patternBytes = Buffer.from(pattern.data);
        for (let i = 0; i < buffer.length; i++) {
          buffer[i] = patternBytes[i % patternBytes.length];
        }
        break;
    }

    console.log(`${name} バッファ初期化完了`);
    return buffer;
  }

  // バッファ情報の表示
  showBufferInfo(name) {
    const buffer = this.buffers.get(name);
    if (!buffer) return;

    console.log(`\n=== ${name} バッファ情報 ===`);
    console.log(`サイズ: ${buffer.length} バイト`);
    console.log(
      `最初の8バイト: ${buffer
        .subarray(0, 8)
        .toString('hex')}`
    );
    console.log(
      `最後の8バイト: ${buffer
        .subarray(-8)
        .toString('hex')}`
    );
  }

  // メモリ使用量の計算
  getTotalMemoryUsage() {
    let totalBytes = 0;
    for (const [name, buffer] of this.buffers) {
      totalBytes += buffer.length;
    }
    return {
      totalBytes,
      totalKB: (totalBytes / 1024).toFixed(2),
      totalMB: (totalBytes / (1024 * 1024)).toFixed(2),
      bufferCount: this.buffers.size,
    };
  }
}

// 使用例
const manager = new BufferManager();

// 各種バッファの作成と初期化
manager.createFixedBuffer('header', 64, 0xff);
manager.createDynamicBuffer(
  'payload',
  'Hello, Binary World! こんにちは!'
);
manager.createFixedBuffer('footer', 32);

// 初期化パターンの適用
manager.initializeBuffer('footer', { type: 'sequence' });

// バッファ情報の表示
manager.showBufferInfo('header');
manager.showBufferInfo('payload');
manager.showBufferInfo('footer');

// メモリ使用量の確認
const memoryUsage = manager.getTotalMemoryUsage();
console.log('\n=== メモリ使用量 ===');
console.log(`総バッファ数: ${memoryUsage.bufferCount}`);
console.log(
  `総使用量: ${memoryUsage.totalBytes} バイト (${memoryUsage.totalKB} KB)`
);

安全な Buffer 作成のベストプラクティス

Buffer を安全に使用するためには、セキュリティリスクを理解し、適切な対策を講じることが重要です。

javascript// 安全なBuffer操作のためのユーティリティクラス
class SecureBufferUtils {
  // 最大バッファサイズの制限
  static MAX_BUFFER_SIZE = 100 * 1024 * 1024; // 100MB

  // 安全なBuffer作成
  static createSafeBuffer(size, options = {}) {
    // サイズ検証
    if (typeof size !== 'number' || size < 0) {
      throw new Error(
        'Buffer size must be a non-negative number'
      );
    }

    if (size > this.MAX_BUFFER_SIZE) {
      throw new Error(
        `Buffer size exceeds maximum allowed size (${this.MAX_BUFFER_SIZE} bytes)`
      );
    }

    // 安全な作成方法を選択
    if (options.unsafe && options.immediateOverwrite) {
      const buffer = Buffer.allocUnsafe(size);
      // セキュリティのため、機密データの痕跡を除去
      buffer.fill(0);
      return buffer;
    } else {
      return Buffer.alloc(size, options.fillValue || 0);
    }
  }

  // 入力データの検証付きBuffer作成
  static createFromValidatedInput(
    input,
    encoding = 'utf8'
  ) {
    try {
      // 入力データの型チェック
      if (input === null || input === undefined) {
        throw new Error(
          'Input cannot be null or undefined'
        );
      }

      // 文字列の場合の処理
      if (typeof input === 'string') {
        // 空文字列チェック
        if (input.length === 0) {
          return Buffer.alloc(0);
        }

        // エンコーディング検証
        const validEncodings = [
          'ascii',
          'utf8',
          'utf16le',
          'base64',
          'hex',
        ];
        if (!validEncodings.includes(encoding)) {
          throw new Error(`Invalid encoding: ${encoding}`);
        }

        return Buffer.from(input, encoding);
      }

      // 配列の場合の処理
      if (Array.isArray(input)) {
        // 配列要素の検証
        for (let i = 0; i < input.length; i++) {
          const value = input[i];
          if (
            typeof value !== 'number' ||
            value < 0 ||
            value > 255
          ) {
            throw new Error(
              `Invalid byte value at index ${i}: ${value}`
            );
          }
        }
        return Buffer.from(input);
      }

      // Buffer の場合の処理
      if (Buffer.isBuffer(input)) {
        return Buffer.from(input); // 安全なコピーを作成
      }

      throw new Error('Unsupported input type');
    } catch (error) {
      console.error(
        'Buffer creation failed:',
        error.message
      );
      throw error;
    }
  }

  // メモリリーク防止のためのBuffer解放
  static secureDispose(buffer) {
    if (!Buffer.isBuffer(buffer)) {
      return;
    }

    // 機密データを上書きして完全に消去
    buffer.fill(0);

    // 追加のセキュリティ対策:ランダムデータで上書き
    for (let i = 0; i < buffer.length; i++) {
      buffer[i] = Math.floor(Math.random() * 256);
    }

    // 最終的に0で埋める
    buffer.fill(0);

    console.log(
      `Buffer (${buffer.length} bytes) securely disposed`
    );
  }

  // Buffer操作の監査ログ
  static createAuditedBuffer(size, purpose = 'general') {
    const timestamp = new Date().toISOString();
    const buffer = this.createSafeBuffer(size);

    // 監査ログの記録
    console.log(
      `[AUDIT] ${timestamp}: Buffer created - Size: ${size}, Purpose: ${purpose}`
    );

    return {
      buffer,
      metadata: {
        created: timestamp,
        size,
        purpose,
        id: Math.random().toString(36).substr(2, 9),
      },
    };
  }
}

// 使用例とベストプラクティスの実演
console.log('=== 安全なBuffer作成の実例 ===\n');

try {
  // 1. 基本的な安全なBuffer作成
  const safeBuffer1 =
    SecureBufferUtils.createSafeBuffer(1024);
  console.log(
    '安全なBuffer作成成功:',
    safeBuffer1.length,
    'bytes'
  );

  // 2. 検証付きBuffer作成
  const validatedBuffer =
    SecureBufferUtils.createFromValidatedInput(
      'Hello, 安全な世界!'
    );
  console.log(
    '検証済みBuffer:',
    validatedBuffer.toString()
  );

  // 3. 監査付きBuffer作成
  const auditedResult =
    SecureBufferUtils.createAuditedBuffer(
      512,
      'encryption'
    );
  console.log(
    '監査付きBuffer ID:',
    auditedResult.metadata.id
  );

  // 4. 安全な解放
  SecureBufferUtils.secureDispose(safeBuffer1);
  SecureBufferUtils.secureDispose(validatedBuffer);
  SecureBufferUtils.secureDispose(auditedResult.buffer);
} catch (error) {
  console.error('エラーが発生しました:', error.message);
}

// エラーケースのテスト
console.log('\n=== エラーケースのテスト ===');

const errorCases = [
  {
    desc: '負のサイズ',
    test: () => SecureBufferUtils.createSafeBuffer(-1),
  },
  {
    desc: '過大なサイズ',
    test: () =>
      SecureBufferUtils.createSafeBuffer(200 * 1024 * 1024),
  },
  {
    desc: '無効な入力',
    test: () =>
      SecureBufferUtils.createFromValidatedInput(null),
  },
  {
    desc: '無効なエンコーディング',
    test: () =>
      SecureBufferUtils.createFromValidatedInput(
        'test',
        'invalid'
      ),
  },
];

errorCases.forEach(({ desc, test }) => {
  try {
    test();
    console.log(`${desc}: 予期しない成功`);
  } catch (error) {
    console.log(
      `${desc}: 正常にエラーを検出 - ${error.message}`
    );
  }
});

これらのベストプラクティスを実装することで、Buffer を使用したバイナリデータ処理において、セキュリティリスクを最小限に抑え、安定したアプリケーションを構築できます。次のセクションでは、Buffer を使った具体的なデータ読み書き操作について詳しく解説いたします。

データの読み書き操作

readInt、writeInt 系メソッドの使い方

Buffer における数値データの読み書きは、データ型とエンディアンを正しく指定することが重要です。Node.js では様々な数値型に対応したメソッドが用意されており、用途に応じて適切に選択する必要があります。

javascript// 数値型別の読み書き操作デモンストレーション
class NumberBufferDemo {
  constructor() {
    // 各数値型のテストデータ
    this.testData = {
      int8: [-128, 0, 127],
      uint8: [0, 128, 255],
      int16: [-32768, 0, 32767],
      uint16: [0, 32768, 65535],
      int32: [-2147483648, 0, 2147483647],
      uint32: [0, 2147483648, 4294967295],
      float: [3.14159, -1.23456, 0.0],
      double: [Math.PI, Math.E, Number.MAX_SAFE_INTEGER],
    };
  }

  // 8ビット整数の操作
  demonstrateInt8Operations() {
    console.log('=== 8ビット整数操作 ===');
    const buffer = Buffer.alloc(6); // 3つのint8 + 3つのuint8用

    // Int8 (符号付き8ビット: -128 to 127)
    this.testData.int8.forEach((value, index) => {
      buffer.writeInt8(value, index);
    });

    // UInt8 (符号なし8ビット: 0 to 255)
    this.testData.uint8.forEach((value, index) => {
      buffer.writeUInt8(value, index + 3);
    });

    console.log('Buffer内容:', buffer.toString('hex'));

    // 読み取り
    console.log('Int8 読み取り:');
    for (let i = 0; i < 3; i++) {
      const value = buffer.readInt8(i);
      console.log(`  位置${i}: ${value}`);
    }

    console.log('UInt8 読み取り:');
    for (let i = 0; i < 3; i++) {
      const value = buffer.readUInt8(i + 3);
      console.log(`  位置${i + 3}: ${value}`);
    }
  }

  // 16ビット整数の操作(エンディアン対応)
  demonstrateInt16Operations() {
    console.log('\n=== 16ビット整数操作 ===');
    const buffer = Buffer.alloc(12); // 各値をBE/LEで格納

    let offset = 0;
    this.testData.int16.forEach((value, index) => {
      // Big Endian で書き込み
      buffer.writeInt16BE(value, offset);
      offset += 2;

      // Little Endian で書き込み
      buffer.writeInt16LE(value, offset);
      offset += 2;
    });

    console.log('Buffer内容:', buffer.toString('hex'));

    // エンディアン別読み取り比較
    console.log('Big Endian vs Little Endian 比較:');
    offset = 0;
    this.testData.int16.forEach((originalValue, index) => {
      const beByte = buffer.readInt16BE(offset);
      const leByte = buffer.readInt16LE(offset + 2);

      console.log(`  値${originalValue}:`);
      console.log(
        `    BE: ${beByte} (${buffer
          .subarray(offset, offset + 2)
          .toString('hex')})`
      );
      console.log(
        `    LE: ${leByte} (${buffer
          .subarray(offset + 2, offset + 4)
          .toString('hex')})`
      );

      offset += 4;
    });
  }

  // 32ビット整数の操作
  demonstrateInt32Operations() {
    console.log('\n=== 32ビット整数操作 ===');
    const buffer = Buffer.alloc(24); // 3つの値 × 8バイト(BE/LE)

    let offset = 0;
    this.testData.int32.forEach((value) => {
      buffer.writeInt32BE(value, offset);
      buffer.writeInt32LE(value, offset + 4);
      offset += 8;
    });

    console.log('32ビット整数の格納結果:');
    offset = 0;
    this.testData.int32.forEach((originalValue, index) => {
      const beValue = buffer.readInt32BE(offset);
      const leValue = buffer.readInt32LE(offset + 4);

      console.log(`  元の値: ${originalValue}`);
      console.log(`    BE読み取り: ${beValue}`);
      console.log(`    LE読み取り: ${leValue}`);
      console.log(
        `    BEバイト: ${buffer
          .subarray(offset, offset + 4)
          .toString('hex')}`
      );
      console.log(
        `    LEバイト: ${buffer
          .subarray(offset + 4, offset + 8)
          .toString('hex')}`
      );

      offset += 8;
    });
  }

  // 浮動小数点数の操作
  demonstrateFloatOperations() {
    console.log('\n=== 浮動小数点数操作 ===');

    // 32ビット浮動小数点(float)
    const floatBuffer = Buffer.alloc(12); // 3つのfloat値
    this.testData.float.forEach((value, index) => {
      floatBuffer.writeFloatBE(value, index * 4);
    });

    console.log('Float値の格納と読み取り:');
    this.testData.float.forEach((originalValue, index) => {
      const readValue = floatBuffer.readFloatBE(index * 4);
      const bytes = floatBuffer.subarray(
        index * 4,
        (index + 1) * 4
      );

      console.log(`  元の値: ${originalValue}`);
      console.log(`  読み取り値: ${readValue}`);
      console.log(`  バイト表現: ${bytes.toString('hex')}`);
      console.log(
        `  精度差: ${Math.abs(originalValue - readValue)}`
      );
    });

    // 64ビット浮動小数点(double)
    const doubleBuffer = Buffer.alloc(24); // 3つのdouble値
    this.testData.double.forEach((value, index) => {
      doubleBuffer.writeDoubleBE(value, index * 8);
    });

    console.log('\nDouble値の格納と読み取り:');
    this.testData.double.forEach((originalValue, index) => {
      const readValue = doubleBuffer.readDoubleBE(
        index * 8
      );
      const bytes = doubleBuffer.subarray(
        index * 8,
        (index + 1) * 8
      );

      console.log(`  元の値: ${originalValue}`);
      console.log(`  読み取り値: ${readValue}`);
      console.log(`  バイト表現: ${bytes.toString('hex')}`);
      console.log(
        `  精度差: ${Math.abs(originalValue - readValue)}`
      );
    });
  }

  // 全デモンストレーションの実行
  runAllDemonstrations() {
    this.demonstrateInt8Operations();
    this.demonstrateInt16Operations();
    this.demonstrateInt32Operations();
    this.demonstrateFloatOperations();
  }
}

// デモンストレーションの実行
const demo = new NumberBufferDemo();
demo.runAllDemonstrations();

エンディアン(Big Endian / Little Endian)の処理

エンディアンは、マルチバイトデータをメモリに格納する際のバイト順序を決定する重要な概念です。ネットワーク通信やクロスプラットフォーム対応では、エンディアンの正しい理解が不可欠です。

javascript// エンディアン処理の実践的な例
class EndianHandler {
  constructor() {
    // システムのエンディアンを検出
    this.systemEndian = this.detectSystemEndian();
    console.log(
      `システムエンディアン: ${this.systemEndian}`
    );
  }

  // システムエンディアンの検出
  detectSystemEndian() {
    const buffer = Buffer.alloc(4);
    buffer.writeInt32LE(0x12345678, 0);

    // 最初のバイトを確認
    return buffer[0] === 0x78
      ? 'Little Endian'
      : 'Big Endian';
  }

  // ネットワークバイトオーダー(Big Endian)での通信例
  createNetworkPacket(packetId, dataLength, payload) {
    // パケット構造: [PacketID(2bytes)] [Length(4bytes)] [Payload(variable)]
    const headerSize = 6; // 2 + 4 bytes
    const totalSize = headerSize + payload.length;
    const packet = Buffer.alloc(totalSize);

    let offset = 0;

    // パケットID(16ビット、Big Endian)
    packet.writeUInt16BE(packetId, offset);
    offset += 2;

    // データ長(32ビット、Big Endian)
    packet.writeUInt32BE(dataLength, offset);
    offset += 4;

    // ペイロード
    payload.copy(packet, offset);

    return packet;
  }

  // ネットワークパケットの解析
  parseNetworkPacket(packet) {
    if (packet.length < 6) {
      throw new Error('Packet too short');
    }

    let offset = 0;

    // パケットID読み取り
    const packetId = packet.readUInt16BE(offset);
    offset += 2;

    // データ長読み取り
    const dataLength = packet.readUInt32BE(offset);
    offset += 4;

    // ペイロード読み取り
    const payload = packet.subarray(offset);

    // データ長の整合性チェック
    if (payload.length !== dataLength) {
      throw new Error(
        `Data length mismatch: expected ${dataLength}, got ${payload.length}`
      );
    }

    return {
      packetId,
      dataLength,
      payload,
      totalSize: packet.length,
    };
  }

  // エンディアン変換ユーティリティ
  convertEndian(buffer, dataType = 'int32') {
    const result = Buffer.alloc(buffer.length);

    switch (dataType) {
      case 'int16':
        for (let i = 0; i < buffer.length; i += 2) {
          const value = buffer.readInt16LE(i);
          result.writeInt16BE(value, i);
        }
        break;

      case 'int32':
        for (let i = 0; i < buffer.length; i += 4) {
          const value = buffer.readInt32LE(i);
          result.writeInt32BE(value, i);
        }
        break;

      case 'float':
        for (let i = 0; i < buffer.length; i += 4) {
          const value = buffer.readFloatLE(i);
          result.writeFloatBE(value, i);
        }
        break;

      case 'double':
        for (let i = 0; i < buffer.length; i += 8) {
          const value = buffer.readDoubleLE(i);
          result.writeDoubleBE(value, i);
        }
        break;

      default:
        throw new Error(
          `Unsupported data type: ${dataType}`
        );
    }

    return result;
  }

  // クロスプラットフォーム対応のデータ保存
  saveDataCrossPlatform(data, filename) {
    // 常にBig Endianで保存(プラットフォーム非依存)
    const buffer = Buffer.alloc(data.length * 4);

    data.forEach((value, index) => {
      buffer.writeInt32BE(value, index * 4);
    });

    // ヘッダー情報を追加
    const header = Buffer.alloc(8);
    header.writeUInt32BE(0x12345678, 0); // マジックナンバー
    header.writeUInt32BE(data.length, 4); // データ個数

    const result = Buffer.concat([header, buffer]);

    console.log(`データ保存: ${filename}`);
    console.log(`  ヘッダー: ${header.toString('hex')}`);
    console.log(`  データサイズ: ${buffer.length} bytes`);
    console.log(`  総サイズ: ${result.length} bytes`);

    return result;
  }

  // クロスプラットフォームデータの読み込み
  loadDataCrossPlatform(buffer) {
    if (buffer.length < 8) {
      throw new Error('Invalid file format');
    }

    // ヘッダー検証
    const magicNumber = buffer.readUInt32BE(0);
    if (magicNumber !== 0x12345678) {
      throw new Error('Invalid magic number');
    }

    const dataCount = buffer.readUInt32BE(4);
    const expectedDataSize = dataCount * 4;
    const actualDataSize = buffer.length - 8;

    if (actualDataSize !== expectedDataSize) {
      throw new Error(
        `Data size mismatch: expected ${expectedDataSize}, got ${actualDataSize}`
      );
    }

    // データ読み込み
    const data = [];
    for (let i = 0; i < dataCount; i++) {
      const value = buffer.readInt32BE(8 + i * 4);
      data.push(value);
    }

    return data;
  }
}

// エンディアン処理の実例
console.log('\n=== エンディアン処理の実例 ===');

const endianHandler = new EndianHandler();

// 1. ネットワーク通信の例
const payload = Buffer.from('Hello, Network!', 'utf8');
const packet = endianHandler.createNetworkPacket(
  0x1234,
  payload.length,
  payload
);

console.log('\nネットワークパケット作成:');
console.log('パケット:', packet.toString('hex'));

const parsedPacket =
  endianHandler.parseNetworkPacket(packet);
console.log('解析結果:', {
  packetId: `0x${parsedPacket.packetId.toString(16)}`,
  dataLength: parsedPacket.dataLength,
  payload: parsedPacket.payload.toString('utf8'),
});

// 2. エンディアン変換の例
const originalData = Buffer.alloc(8);
originalData.writeInt32LE(0x12345678, 0);
originalData.writeInt32LE(0x9abcdef0, 4);

console.log('\nエンディアン変換:');
console.log('元データ (LE):', originalData.toString('hex'));

const convertedData = endianHandler.convertEndian(
  originalData,
  'int32'
);
console.log('変換後 (BE):', convertedData.toString('hex'));

// 3. クロスプラットフォームデータ保存/読み込み
const testData = [0x12345678, 0x9abcdef0, 0x11223344];
const savedBuffer = endianHandler.saveDataCrossPlatform(
  testData,
  'test.dat'
);
const loadedData =
  endianHandler.loadDataCrossPlatform(savedBuffer);

console.log('\nクロスプラットフォームデータ:');
console.log(
  '元データ:',
  testData.map((x) => `0x${x.toString(16)}`)
);
console.log(
  '読み込み結果:',
  loadedData.map((x) => `0x${x.toString(16)}`)
);
console.log(
  'データ一致:',
  JSON.stringify(testData) === JSON.stringify(loadedData)
);

浮動小数点数の扱い

浮動小数点数のバイナリ表現は、IEEE 754 標準に基づいており、精度や特殊値の処理に注意が必要です。

javascript// 浮動小数点数の詳細な処理例
class FloatProcessor {
  constructor() {
    // IEEE 754 の特殊値
    this.specialValues = {
      positiveInfinity: Number.POSITIVE_INFINITY,
      negativeInfinity: Number.NEGATIVE_INFINITY,
      nan: Number.NaN,
      positiveZero: 0.0,
      negativeZero: -0.0,
    };
  }

  // 浮動小数点数のバイナリ分析
  analyzeFloat(value) {
    const buffer = Buffer.alloc(4);
    buffer.writeFloatBE(value, 0);

    // バイナリ表現を取得
    const bytes = buffer.toString('hex');
    const binary = parseInt(bytes, 16)
      .toString(2)
      .padStart(32, '0');

    // IEEE 754 の構成要素を分析
    const sign = binary[0];
    const exponent = binary.substring(1, 9);
    const mantissa = binary.substring(9);

    return {
      value,
      bytes,
      binary,
      sign: sign === '0' ? '+' : '-',
      exponent: parseInt(exponent, 2),
      mantissa,
      isSpecial: this.isSpecialValue(value),
    };
  }

  // 特殊値の判定
  isSpecialValue(value) {
    if (Number.isNaN(value)) return 'NaN';
    if (value === Number.POSITIVE_INFINITY)
      return '+Infinity';
    if (value === Number.NEGATIVE_INFINITY)
      return '-Infinity';
    if (Object.is(value, 0.0)) return '+Zero';
    if (Object.is(value, -0.0)) return '-Zero';
    return false;
  }

  // 精度テスト
  testFloatPrecision() {
    console.log('=== 浮動小数点精度テスト ===');

    const testValues = [
      0.1 + 0.2, // 有名な浮動小数点誤差
      Math.PI, // 円周率
      Math.E, // 自然対数の底
      1.23456789012345, // 精度限界テスト
      Number.MAX_VALUE, // 最大値
      Number.MIN_VALUE, // 最小値
    ];

    testValues.forEach((value) => {
      const analysis = this.analyzeFloat(value);

      // Buffer経由での再現精度をテスト
      const buffer = Buffer.alloc(4);
      buffer.writeFloatBE(value, 0);
      const reconstructed = buffer.readFloatBE(0);
      const precisionLoss = Math.abs(value - reconstructed);

      console.log(`\n値: ${value}`);
      console.log(`バイナリ: ${analysis.binary}`);
      console.log(`16進: ${analysis.bytes}`);
      console.log(`再構築値: ${reconstructed}`);
      console.log(`精度損失: ${precisionLoss}`);
      console.log(
        `特殊値: ${analysis.isSpecial || 'なし'}`
      );
    });
  }

  // Double precision との比較
  compareFloatDouble() {
    console.log('\n=== Float vs Double 精度比較 ===');

    const testValue = Math.PI;

    // Float (32bit) 処理
    const floatBuffer = Buffer.alloc(4);
    floatBuffer.writeFloatBE(testValue, 0);
    const floatResult = floatBuffer.readFloatBE(0);

    // Double (64bit) 処理
    const doubleBuffer = Buffer.alloc(8);
    doubleBuffer.writeDoubleBE(testValue, 0);
    const doubleResult = doubleBuffer.readDoubleBE(0);

    console.log(`元の値: ${testValue}`);
    console.log(`Float結果: ${floatResult}`);
    console.log(`Double結果: ${doubleResult}`);
    console.log(
      `Float精度損失: ${Math.abs(testValue - floatResult)}`
    );
    console.log(
      `Double精度損失: ${Math.abs(
        testValue - doubleResult
      )}`
    );
    console.log(
      `Float bytes: ${floatBuffer.toString('hex')}`
    );
    console.log(
      `Double bytes: ${doubleBuffer.toString('hex')}`
    );
  }

  // 配列データの効率的な処理
  processFloatArray(floatArray) {
    console.log('\n=== 浮動小数点配列処理 ===');

    // 配列をBufferに変換
    const buffer = Buffer.alloc(floatArray.length * 4);
    floatArray.forEach((value, index) => {
      buffer.writeFloatBE(value, index * 4);
    });

    console.log(`配列サイズ: ${floatArray.length} 要素`);
    console.log(`Buffer サイズ: ${buffer.length} bytes`);
    console.log(`Buffer 内容: ${buffer.toString('hex')}`);

    // Bufferから配列に復元
    const reconstructedArray = [];
    for (let i = 0; i < floatArray.length; i++) {
      reconstructedArray.push(buffer.readFloatBE(i * 4));
    }

    // 精度検証
    const precisionErrors = floatArray.map(
      (original, index) => {
        return Math.abs(
          original - reconstructedArray[index]
        );
      }
    );

    console.log('元配列:', floatArray);
    console.log('復元配列:', reconstructedArray);
    console.log('精度誤差:', precisionErrors);
    console.log('最大誤差:', Math.max(...precisionErrors));

    return {
      buffer,
      reconstructedArray,
      maxError: Math.max(...precisionErrors),
    };
  }

  // 特殊値の処理テスト
  testSpecialValues() {
    console.log('\n=== 特殊値処理テスト ===');

    Object.entries(this.specialValues).forEach(
      ([name, value]) => {
        const buffer = Buffer.alloc(4);

        try {
          buffer.writeFloatBE(value, 0);
          const readValue = buffer.readFloatBE(0);
          const analysis = this.analyzeFloat(value);

          console.log(`\n${name}:`);
          console.log(`  元の値: ${value}`);
          console.log(`  読み取り値: ${readValue}`);
          console.log(
            `  バイト表現: ${buffer.toString('hex')}`
          );
          console.log(`  バイナリ: ${analysis.binary}`);
          console.log(
            `  等価性: ${Object.is(value, readValue)}`
          );
        } catch (error) {
          console.log(`${name}: エラー - ${error.message}`);
        }
      }
    );
  }
}

// 浮動小数点処理のデモンストレーション
const floatProcessor = new FloatProcessor();

floatProcessor.testFloatPrecision();
floatProcessor.compareFloatDouble();

const testArray = [3.14159, 2.71828, 1.41421, 0.57721];
floatProcessor.processFloatArray(testArray);

floatProcessor.testSpecialValues();

これらの読み書き操作をマスターすることで、バイナリデータの正確な処理が可能になります。次のセクションでは、文字列エンコーディングとの変換について詳しく解説いたします。

文字列エンコーディングとの変換

UTF-8、ASCII、Base64 の相互変換

文字列とバイナリデータの変換は、Web 開発において頻繁に行われる処理です。適切なエンコーディングを選択することで、データの整合性を保ち、国際化対応も実現できます。

javascript// エンコーディング変換の包括的なデモンストレーション
class EncodingConverter {
  constructor() {
    // サポートするエンコーディング一覧
    this.supportedEncodings = [
      'ascii',
      'utf8',
      'utf16le',
      'ucs2',
      'base64',
      'base64url',
      'hex',
      'binary',
    ];

    // テスト用の多言語文字列
    this.testStrings = {
      english: 'Hello, World!',
      japanese: 'こんにちは、世界!',
      emoji: '🌍🚀✨',
      mixed: 'Hello 世界 🌍',
      special: 'Line1\nLine2\tTabbed\r\nWindows',
      numbers: '12345 67890',
      symbols: '!@#$%^&*()_+-=[]{}|;:\'",.<>?',
    };
  }

  // 基本的なエンコーディング変換
  demonstrateBasicEncoding() {
    console.log('=== 基本エンコーディング変換 ===');

    Object.entries(this.testStrings).forEach(
      ([category, text]) => {
        console.log(`\n${category}: "${text}"`);

        // UTF-8 エンコーディング
        const utf8Buffer = Buffer.from(text, 'utf8');
        console.log(
          `  UTF-8: ${utf8Buffer.toString('hex')} (${
            utf8Buffer.length
          } bytes)`
        );

        // Base64 エンコーディング
        const base64String = utf8Buffer.toString('base64');
        console.log(`  Base64: ${base64String}`);

        // Base64 デコーディング
        const decodedBuffer = Buffer.from(
          base64String,
          'base64'
        );
        const decodedText = decodedBuffer.toString('utf8');
        console.log(`  復元: "${decodedText}"`);
        console.log(`  一致: ${text === decodedText}`);

        // ASCII 対応チェック
        try {
          const asciiBuffer = Buffer.from(text, 'ascii');
          const asciiText = asciiBuffer.toString('ascii');
          console.log(
            `  ASCII: ${
              asciiText === text ? '対応' : '非対応'
            }`
          );
        } catch (error) {
          console.log(`  ASCII: エラー - ${error.message}`);
        }
      }
    );
  }

  // エンコーディング比較分析
  compareEncodings(text) {
    console.log(
      `\n=== エンコーディング比較: "${text}" ===`
    );

    const results = {};

    this.supportedEncodings.forEach((encoding) => {
      try {
        const buffer = Buffer.from(text, 'utf8'); // 元データはUTF-8として扱う
        const encoded = buffer.toString(encoding);
        const decoded = Buffer.from(
          encoded,
          encoding
        ).toString('utf8');

        results[encoding] = {
          encoded,
          decoded,
          byteLength: buffer.length,
          encodedLength: encoded.length,
          isReversible: text === decoded,
          efficiency: encoded.length / buffer.length,
        };
      } catch (error) {
        results[encoding] = {
          error: error.message,
        };
      }
    });

    // 結果を表形式で表示
    console.log(
      '\n| エンコーディング | バイト数 | エンコード長 | 効率 | 可逆性 |'
    );
    console.log(
      '|------------------|----------|-------------|------|--------|'
    );

    Object.entries(results).forEach(
      ([encoding, result]) => {
        if (result.error) {
          console.log(
            `| ${encoding.padEnd(
              16
            )} | エラー | - | - | - |`
          );
        } else {
          const efficiency = (
            result.efficiency * 100
          ).toFixed(1);
          const reversible = result.isReversible
            ? '✓'
            : '✗';
          console.log(
            `| ${encoding.padEnd(16)} | ${result.byteLength
              .toString()
              .padEnd(8)} | ${result.encodedLength
              .toString()
              .padEnd(11)} | ${efficiency.padEnd(
              4
            )}% | ${reversible.padEnd(6)} |`
          );
        }
      }
    );

    return results;
  }

  // URL安全なBase64エンコーディング
  demonstrateUrlSafeBase64() {
    console.log(
      '\n=== URL安全なBase64エンコーディング ==='
    );

    const testData =
      'Hello, World! 🌍 This is a test string with special characters: +/=';
    const buffer = Buffer.from(testData, 'utf8');

    // 標準Base64
    const standardBase64 = buffer.toString('base64');
    console.log('標準Base64:', standardBase64);

    // URL安全Base64
    const urlSafeBase64 = buffer.toString('base64url');
    console.log('URL安全Base64:', urlSafeBase64);

    // 手動でURL安全変換
    const manualUrlSafe = standardBase64
      .replace(/\+/g, '-')
      .replace(/\//g, '_')
      .replace(/=/g, '');
    console.log('手動変換:', manualUrlSafe);

    // デコード検証
    const decodedStandard = Buffer.from(
      standardBase64,
      'base64'
    ).toString('utf8');
    const decodedUrlSafe = Buffer.from(
      urlSafeBase64,
      'base64url'
    ).toString('utf8');

    console.log('デコード結果:');
    console.log('  標準:', decodedStandard);
    console.log('  URL安全:', decodedUrlSafe);
    console.log(
      '  一致:',
      decodedStandard === decodedUrlSafe &&
        decodedStandard === testData
    );
  }

  // 16進数エンコーディングの活用
  demonstrateHexEncoding() {
    console.log('\n=== 16進数エンコーディング ===');

    const binaryData = Buffer.from([
      0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0xe4, 0xb8, 0x96,
      0xe7, 0x95, 0x8c,
    ]);

    console.log(
      'バイナリデータ:',
      Array.from(binaryData)
        .map((b) => `0x${b.toString(16).padStart(2, '0')}`)
        .join(' ')
    );

    // 16進数文字列に変換
    const hexString = binaryData.toString('hex');
    console.log('16進数文字列:', hexString);

    // 大文字での表示
    const hexUpperCase = hexString.toUpperCase();
    console.log('16進数(大文字):', hexUpperCase);

    // スペース区切りでの表示
    const hexSpaced = hexString
      .replace(/(..)/g, '$1 ')
      .trim();
    console.log('16進数(スペース区切り):', hexSpaced);

    // 16進数からBufferに復元
    const restoredBuffer = Buffer.from(hexString, 'hex');
    const restoredText = restoredBuffer.toString('utf8');
    console.log('復元されたテキスト:', restoredText);

    // バイト単位での処理
    console.log('\nバイト単位分析:');
    for (let i = 0; i < binaryData.length; i++) {
      const byte = binaryData[i];
      const hex = byte.toString(16).padStart(2, '0');
      const binary = byte.toString(2).padStart(8, '0');
      const ascii =
        byte >= 32 && byte <= 126
          ? String.fromCharCode(byte)
          : '.';

      console.log(
        `  位置${i}: 0x${hex} (${binary}) '${ascii}'`
      );
    }
  }
}

// エンコーディング変換のデモンストレーション
const converter = new EncodingConverter();

converter.demonstrateBasicEncoding();
converter.compareEncodings('Hello 世界! 🌍');
converter.demonstrateUrlSafeBase64();
converter.demonstrateHexEncoding();

toString()、Buffer.from() での文字コード指定

Buffer と文字列の相互変換では、文字コードの指定が重要です。適切な指定により、データの損失を防ぎ、正確な変換を実現できます。

javascript// 文字コード指定の詳細な処理例
class CharacterCodeHandler {
  constructor() {
    // 各エンコーディングの特徴
    this.encodingInfo = {
      ascii: {
        description: '7ビットASCII文字のみ',
        range: '0-127',
        bytePerChar: 1,
        supports: 'English only',
      },
      utf8: {
        description: '可変長Unicode(1-4バイト)',
        range: 'All Unicode',
        bytePerChar: '1-4',
        supports: 'All languages',
      },
      utf16le: {
        description: '16ビットUnicode(Little Endian)',
        range: 'All Unicode',
        bytePerChar: '2-4',
        supports: 'All languages',
      },
      ucs2: {
        description: 'UTF-16LEの別名',
        range: 'All Unicode',
        bytePerChar: '2-4',
        supports: 'All languages',
      },
      base64: {
        description: 'Base64エンコーディング',
        range: 'A-Z, a-z, 0-9, +, /',
        bytePerChar: '4/3',
        supports: 'Binary data',
      },
      hex: {
        description: '16進数表現',
        range: '0-9, a-f',
        bytePerChar: 2,
        supports: 'Binary data',
      },
    };
  }

  // toString() メソッドの詳細な使用法
  demonstrateToString() {
    console.log('=== toString() メソッドの活用 ===');

    // 多言語テストデータ
    const multilingualText =
      'Hello こんにちは 안녕하세요 🌍';
    const sourceBuffer = Buffer.from(
      multilingualText,
      'utf8'
    );

    console.log(`元のテキスト: "${multilingualText}"`);
    console.log(`UTF-8バイト数: ${sourceBuffer.length}`);
    console.log(
      `UTF-8バイト: ${sourceBuffer.toString('hex')}`
    );

    // 各エンコーディングでの toString() 結果
    Object.keys(this.encodingInfo).forEach((encoding) => {
      try {
        const result = sourceBuffer.toString(encoding);
        console.log(`\n${encoding}:`);
        console.log(`  結果: "${result}"`);
        console.log(`  長さ: ${result.length}`);

        // 逆変換での検証
        try {
          const backConverted = Buffer.from(
            result,
            encoding
          );
          const backToUtf8 = backConverted.toString('utf8');
          const isReversible =
            backToUtf8 === multilingualText;
          console.log(
            `  可逆性: ${isReversible ? '✓' : '✗'}`
          );

          if (!isReversible) {
            console.log(`  逆変換結果: "${backToUtf8}"`);
          }
        } catch (reverseError) {
          console.log(
            `  可逆性: エラー - ${reverseError.message}`
          );
        }
      } catch (error) {
        console.log(
          `\n${encoding}: エラー - ${error.message}`
        );
      }
    });
  }

  // Buffer.from() の高度な使用法
  demonstrateBufferFrom() {
    console.log('\n=== Buffer.from() の高度な使用法 ===');

    // 1. 文字列からの変換(エンコーディング指定)
    const testString = 'Hello 世界! 🚀';

    console.log('1. 文字列からの変換:');
    ['utf8', 'utf16le', 'ascii'].forEach((encoding) => {
      try {
        const buffer = Buffer.from(testString, encoding);
        console.log(
          `  ${encoding}: ${
            buffer.length
          }バイト - ${buffer.toString('hex')}`
        );
      } catch (error) {
        console.log(
          `  ${encoding}: エラー - ${error.message}`
        );
      }
    });

    // 2. 配列からの変換
    console.log('\n2. 配列からの変換:');
    const byteArray = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
    const arrayBuffer = Buffer.from(byteArray);
    console.log(`  配列: [${byteArray.join(', ')}]`);
    console.log(`  Buffer: ${arrayBuffer.toString('hex')}`);
    console.log(
      `  文字列: "${arrayBuffer.toString('ascii')}"`
    );

    // 3. 既存Bufferからのコピー
    console.log('\n3. Bufferからのコピー:');
    const originalBuffer = Buffer.from(
      'Original Data',
      'utf8'
    );
    const copiedBuffer = Buffer.from(originalBuffer);

    console.log(
      `  元Buffer: ${originalBuffer.toString('hex')}`
    );
    console.log(
      `  コピー: ${copiedBuffer.toString('hex')}`
    );
    console.log(
      `  同一参照: ${originalBuffer === copiedBuffer}`
    );
    console.log(
      `  内容一致: ${originalBuffer.equals(copiedBuffer)}`
    );

    // 4. ArrayBufferからの変換
    console.log('\n4. ArrayBufferからの変換:');
    const arrayBuffer2 = new ArrayBuffer(8);
    const uint8Array = new Uint8Array(arrayBuffer2);
    uint8Array.set([
      0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    ]);

    const bufferFromArrayBuffer = Buffer.from(arrayBuffer2);
    console.log(
      `  ArrayBuffer: ${Array.from(uint8Array)
        .map((b) => `0x${b.toString(16)}`)
        .join(' ')}`
    );
    console.log(
      `  Buffer: ${bufferFromArrayBuffer.toString('hex')}`
    );
    console.log(
      `  文字列: "${bufferFromArrayBuffer.toString(
        'ascii'
      )}"`
    );
  }

  // 部分的な文字列変換
  demonstratePartialConversion() {
    console.log('\n=== 部分的な文字列変換 ===');

    const longText =
      'This is a very long text that we want to process in chunks. これは非常に長いテキストで、チャンクに分けて処理したいものです。';
    const buffer = Buffer.from(longText, 'utf8');

    console.log(`元のテキスト長: ${longText.length} 文字`);
    console.log(`Buffer長: ${buffer.length} バイト`);

    // 部分的な変換(start, end パラメータ使用)
    const chunkSize = 20;
    console.log(`\n${chunkSize}バイトずつ処理:`);

    for (
      let start = 0;
      start < buffer.length;
      start += chunkSize
    ) {
      const end = Math.min(
        start + chunkSize,
        buffer.length
      );

      try {
        const chunk = buffer.toString('utf8', start, end);
        console.log(`  バイト${start}-${end}: "${chunk}"`);
      } catch (error) {
        // UTF-8の文字境界でない場合のエラー処理
        console.log(
          `  バイト${start}-${end}: エラー - ${error.message}`
        );

        // 安全な境界を見つけて再試行
        let safeEnd = end;
        while (
          safeEnd > start &&
          (buffer[safeEnd] & 0x80) !== 0
        ) {
          safeEnd--;
        }

        if (safeEnd > start) {
          const safeChunk = buffer.toString(
            'utf8',
            start,
            safeEnd
          );
          console.log(
            `  修正後 バイト${start}-${safeEnd}: "${safeChunk}"`
          );
        }
      }
    }
  }

  // エンコーディング検出と自動変換
  detectAndConvert(buffer) {
    console.log('\n=== エンコーディング検出と変換 ===');

    // 簡単なエンコーディング推定
    const detectionResults = this.detectEncoding(buffer);

    console.log('エンコーディング推定結果:');
    detectionResults.forEach((result) => {
      console.log(
        `  ${result.encoding}: 信頼度 ${result.confidence}% - ${result.reason}`
      );
    });

    // 最も可能性の高いエンコーディングで変換
    const bestMatch = detectionResults[0];
    if (bestMatch.confidence > 50) {
      try {
        const convertedText = buffer.toString(
          bestMatch.encoding
        );
        console.log(
          `\n推定エンコーディング(${bestMatch.encoding})での変換:`
        );
        console.log(`"${convertedText}"`);
      } catch (error) {
        console.log(`変換エラー: ${error.message}`);
      }
    }
  }

  // 簡単なエンコーディング検出ロジック
  detectEncoding(buffer) {
    const results = [];

    // ASCII判定
    const isAscii = buffer.every((byte) => byte < 128);
    if (isAscii) {
      results.push({
        encoding: 'ascii',
        confidence: 90,
        reason: 'All bytes < 128',
      });
    }

    // UTF-8判定
    try {
      const utf8Text = buffer.toString('utf8');
      const backToBuffer = Buffer.from(utf8Text, 'utf8');
      if (buffer.equals(backToBuffer)) {
        results.push({
          encoding: 'utf8',
          confidence: 85,
          reason: 'Valid UTF-8 sequence',
        });
      }
    } catch (error) {
      // UTF-8ではない
    }

    // Base64判定
    const base64Pattern = /^[A-Za-z0-9+/]*={0,2}$/;
    const bufferAsString = buffer.toString('ascii');
    if (
      base64Pattern.test(bufferAsString) &&
      bufferAsString.length % 4 === 0
    ) {
      results.push({
        encoding: 'base64',
        confidence: 70,
        reason: 'Valid Base64 pattern',
      });
    }

    // 16進数判定
    const hexPattern = /^[0-9a-fA-F]+$/;
    if (
      hexPattern.test(bufferAsString) &&
      bufferAsString.length % 2 === 0
    ) {
      results.push({
        encoding: 'hex',
        confidence: 60,
        reason: 'Valid hex pattern',
      });
    }

    // 信頼度順にソート
    return results.sort(
      (a, b) => b.confidence - a.confidence
    );
  }
}

// 文字コード処理のデモンストレーション
const charHandler = new CharacterCodeHandler();

charHandler.demonstrateToString();
charHandler.demonstrateBufferFrom();
charHandler.demonstratePartialConversion();

// エンコーディング検出のテスト
const testBuffers = [
  Buffer.from('Hello World', 'ascii'),
  Buffer.from('こんにちは', 'utf8'),
  Buffer.from('SGVsbG8gV29ybGQ=', 'base64'),
  Buffer.from('48656c6c6f20576f726c64', 'hex'),
];

testBuffers.forEach((buffer, index) => {
  console.log(`\nテストBuffer ${index + 1}:`);
  console.log(`バイト: ${buffer.toString('hex')}`);
  charHandler.detectAndConvert(buffer);
});

日本語文字の適切な処理方法

日本語を含むマルチバイト文字の処理では、文字境界やエンコーディングの違いに特に注意が必要です。

javascript// 日本語文字処理の専門クラス
class JapaneseTextProcessor {
  constructor() {
    // 日本語文字の分類
    this.characterSets = {
      hiragana: /[\u3040-\u309F]/,
      katakana: /[\u30A0-\u30FF]/,
      kanji: /[\u4E00-\u9FAF]/,
      fullWidthNumbers: /[\uFF10-\uFF19]/,
      fullWidthAlphabets: /[\uFF21-\uFF3A\uFF41-\uFF5A]/,
      halfWidthKatakana: /[\uFF61-\uFF9F]/,
      japaneseSymbols: /[\u3000-\u303F]/,
    };

    // テスト用日本語文字列
    this.testTexts = {
      mixed: 'こんにちはHello世界123!',
      hiragana: 'ひらがなテスト',
      katakana: 'カタカナテスト',
      kanji: '漢字テスト',
      fullWidth: '123ABC',
      halfWidth: 'アイウエオ',
      symbols: '「」、。・',
      complex: '私の名前は田中太郎です。I am 25 years old.',
    };
  }

  // 日本語文字の分析
  analyzeJapaneseText(text) {
    console.log(`=== 日本語文字分析: "${text}" ===`);

    const buffer = Buffer.from(text, 'utf8');
    console.log(`文字数: ${text.length}`);
    console.log(`バイト数: ${buffer.length}`);
    console.log(`UTF-8バイト: ${buffer.toString('hex')}`);

    // 文字種別の統計
    const stats = {};
    Object.keys(this.characterSets).forEach((key) => {
      stats[key] = 0;
    });
    stats.ascii = 0;
    stats.other = 0;

    // 各文字を分析
    console.log('\n文字別分析:');
    for (let i = 0; i < text.length; i++) {
      const char = text[i];
      const codePoint = char.codePointAt(0);
      const charBuffer = Buffer.from(char, 'utf8');

      // 文字種別を判定
      let charType = 'other';
      if (char.match(/[a-zA-Z0-9]/)) {
        charType = 'ascii';
        stats.ascii++;
      } else {
        for (const [type, pattern] of Object.entries(
          this.characterSets
        )) {
          if (char.match(pattern)) {
            charType = type;
            stats[type]++;
            break;
          }
        }
        if (charType === 'other') {
          stats.other++;
        }
      }

      console.log(
        `  "${char}": U+${codePoint
          .toString(16)
          .padStart(4, '0')} (${charType}) ${
          charBuffer.length
        }バイト [${charBuffer.toString('hex')}]`
      );
    }

    // 統計情報の表示
    console.log('\n文字種別統計:');
    Object.entries(stats).forEach(([type, count]) => {
      if (count > 0) {
        console.log(`  ${type}: ${count}文字`);
      }
    });

    return { buffer, stats };
  }

  // UTF-8文字境界の安全な処理
  demonstrateSafeUtf8Handling() {
    console.log('\n=== UTF-8文字境界の安全な処理 ===');

    const text = 'こんにちは世界!Hello World!';
    const buffer = Buffer.from(text, 'utf8');

    console.log(`元のテキスト: "${text}"`);
    console.log(`バイト数: ${buffer.length}`);

    // 危険な分割(文字境界を考慮しない)
    console.log('\n危険な分割例:');
    for (let i = 5; i <= 15; i += 5) {
      try {
        const chunk = buffer
          .subarray(0, i)
          .toString('utf8');
        console.log(`  ${i}バイト: "${chunk}"`);
      } catch (error) {
        console.log(
          `  ${i}バイト: エラー - ${error.message}`
        );
      }
    }

    // 安全な分割(文字境界を考慮)
    console.log('\n安全な分割例:');
    for (let i = 5; i <= 15; i += 5) {
      const safeEnd = this.findSafeUtf8Boundary(buffer, i);
      const chunk = buffer
        .subarray(0, safeEnd)
        .toString('utf8');
      console.log(
        `  ${i}バイト要求 → ${safeEnd}バイト使用: "${chunk}"`
      );
    }
  }

  // UTF-8文字境界の検出
  findSafeUtf8Boundary(buffer, maxBytes) {
    if (maxBytes >= buffer.length) {
      return buffer.length;
    }

    // UTF-8の文字境界を逆方向に検索
    for (let i = maxBytes; i >= 0; i--) {
      const byte = buffer[i];

      // ASCII文字(0xxxxxxx)または UTF-8の開始バイト(11xxxxxx)
      if ((byte & 0x80) === 0 || (byte & 0xc0) === 0xc0) {
        return i;
      }
    }

    return 0; // 安全な境界が見つからない場合
  }

  // 文字エンコーディングの変換と検証
  demonstrateEncodingConversion() {
    console.log('\n=== 日本語エンコーディング変換 ===');

    const japaneseText = '日本語のテスト文字列です。';
    console.log(`元のテキスト: "${japaneseText}"`);

    // UTF-8 エンコーディング
    const utf8Buffer = Buffer.from(japaneseText, 'utf8');
    console.log(`UTF-8: ${utf8Buffer.length}バイト`);
    console.log(`  バイト: ${utf8Buffer.toString('hex')}`);

    // UTF-16LE エンコーディング
    const utf16Buffer = Buffer.from(
      japaneseText,
      'utf16le'
    );
    console.log(`UTF-16LE: ${utf16Buffer.length}バイト`);
    console.log(`  バイト: ${utf16Buffer.toString('hex')}`);

    // Base64 エンコーディング(UTF-8ベース)
    const base64String = utf8Buffer.toString('base64');
    console.log(`Base64: ${base64String}`);

    // 各エンコーディングからの復元
    console.log('\n復元テスト:');
    const restoredUtf8 = utf8Buffer.toString('utf8');
    const restoredUtf16 = utf16Buffer.toString('utf16le');
    const restoredBase64 = Buffer.from(
      base64String,
      'base64'
    ).toString('utf8');

    console.log(
      `UTF-8復元: "${restoredUtf8}" (${
        restoredUtf8 === japaneseText
      })`
    );
    console.log(
      `UTF-16復元: "${restoredUtf16}" (${
        restoredUtf16 === japaneseText
      })`
    );
    console.log(
      `Base64復元: "${restoredBase64}" (${
        restoredBase64 === japaneseText
      })`
    );
  }

  // 日本語文字列の正規化処理
  demonstrateNormalization() {
    console.log('\n=== 日本語文字列の正規化 ===');

    // 異なる表現の同じ文字
    const variations = [
      'が', // ひらがな「が」
      'ガ', // カタカナ「ガ」
      'ガ', // 半角カタカナ「カ」+ 濁点「゙」
    ];

    variations.forEach((char, index) => {
      const buffer = Buffer.from(char, 'utf8');
      const normalized = char.normalize('NFC');
      const normalizedBuffer = Buffer.from(
        normalized,
        'utf8'
      );

      console.log(`変化${index + 1}: "${char}"`);
      console.log(
        `  元のバイト: ${buffer.toString('hex')} (${
          buffer.length
        }バイト)`
      );
      console.log(`  正規化後: "${normalized}"`);
      console.log(
        `  正規化バイト: ${normalizedBuffer.toString(
          'hex'
        )} (${normalizedBuffer.length}バイト)`
      );
      console.log(
        `  Unicode: ${Array.from(char)
          .map(
            (c) =>
              `U+${c
                .codePointAt(0)
                .toString(16)
                .padStart(4, '0')}`
          )
          .join(' ')}`
      );
    });

    // 全角・半角変換
    console.log('\n全角・半角変換:');
    const fullWidthText = '123ABC';
    const halfWidthText = '123ABC';

    console.log(
      `全角: "${fullWidthText}" - ${
        Buffer.from(fullWidthText, 'utf8').length
      }バイト`
    );
    console.log(
      `半角: "${halfWidthText}" - ${
        Buffer.from(halfWidthText, 'utf8').length
      }バイト`
    );

    // 簡単な全角→半角変換
    const converted = fullWidthText.replace(
      /[A-Za-z0-9]/g,
      (char) => {
        return String.fromCharCode(
          char.charCodeAt(0) - 0xfee0
        );
      }
    );
    console.log(
      `変換結果: "${converted}" - ${
        Buffer.from(converted, 'utf8').length
      }バイト`
    );
  }

  // 全テストの実行
  runAllTests() {
    Object.entries(this.testTexts).forEach(
      ([category, text]) => {
        this.analyzeJapaneseText(text);
        console.log(''); // 空行
      }
    );

    this.demonstrateSafeUtf8Handling();
    this.demonstrateEncodingConversion();
    this.demonstrateNormalization();
  }
}

// 日本語文字処理のデモンストレーション
const japaneseProcessor = new JapaneseTextProcessor();
japaneseProcessor.runAllTests();

これらの文字列エンコーディング処理をマスターすることで、国際化対応のアプリケーション開発や、様々な文字コードが混在する環境での安全なデータ処理が可能になります。次のセクションでは、実用的なバイナリデータ処理の具体例をご紹介いたします。

実用的なバイナリデータ処理

画像ファイルのヘッダー解析

画像ファイルの形式判定や基本情報の取得は、ファイルアップロード機能やメディア処理において重要な技術です。Buffer を使用することで、効率的にバイナリデータを解析できます。

javascript// 画像ファイル解析の包括的なクラス
class ImageAnalyzer {
  constructor() {
    // 主要な画像フォーマットのシグネチャ
    this.imageSignatures = {
      jpeg: {
        signature: [0xff, 0xd8, 0xff],
        extension: '.jpg',
        mimeType: 'image/jpeg',
        description: 'JPEG画像',
      },
      png: {
        signature: [
          0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
        ],
        extension: '.png',
        mimeType: 'image/png',
        description: 'PNG画像',
      },
      gif: {
        signature: [0x47, 0x49, 0x46, 0x38],
        extension: '.gif',
        mimeType: 'image/gif',
        description: 'GIF画像',
      },
      webp: {
        signature: [0x52, 0x49, 0x46, 0x46],
        extension: '.webp',
        mimeType: 'image/webp',
        description: 'WebP画像',
        additionalCheck: (buffer) => {
          // RIFF後の8バイト目からWEBPかチェック
          return (
            buffer.length >= 12 &&
            buffer
              .subarray(8, 12)
              .equals(Buffer.from([0x57, 0x45, 0x42, 0x50]))
          );
        },
      },
      bmp: {
        signature: [0x42, 0x4d],
        extension: '.bmp',
        mimeType: 'image/bmp',
        description: 'BMP画像',
      },
      ico: {
        signature: [0x00, 0x00, 0x01, 0x00],
        extension: '.ico',
        mimeType: 'image/x-icon',
        description: 'ICO画像',
      },
    };
  }

  // 画像形式の判定
  detectImageFormat(buffer) {
    console.log('=== 画像形式判定 ===');
    console.log(`ファイルサイズ: ${buffer.length} bytes`);
    console.log(
      `先頭16バイト: ${buffer
        .subarray(0, 16)
        .toString('hex')}`
    );

    for (const [format, info] of Object.entries(
      this.imageSignatures
    )) {
      const signature = Buffer.from(info.signature);

      if (buffer.length >= signature.length) {
        const fileHeader = buffer.subarray(
          0,
          signature.length
        );

        if (fileHeader.equals(signature)) {
          // 追加チェックが必要な場合
          if (
            info.additionalCheck &&
            !info.additionalCheck(buffer)
          ) {
            continue;
          }

          console.log(
            `検出された形式: ${info.description}`
          );
          console.log(`拡張子: ${info.extension}`);
          console.log(`MIMEタイプ: ${info.mimeType}`);

          return {
            format,
            ...info,
            isValid: true,
          };
        }
      }
    }

    console.log('未知の画像形式です');
    return {
      format: 'unknown',
      isValid: false,
    };
  }

  // JPEG画像の詳細解析
  analyzeJpegDetails(buffer) {
    if (
      !buffer
        .subarray(0, 3)
        .equals(Buffer.from([0xff, 0xd8, 0xff]))
    ) {
      throw new Error('JPEG形式ではありません');
    }

    console.log('\n=== JPEG詳細解析 ===');

    let offset = 2; // FF D8 をスキップ
    const segments = [];

    while (offset < buffer.length - 1) {
      // JPEGセグメントマーカーを検索
      if (buffer[offset] !== 0xff) {
        break;
      }

      const marker = buffer[offset + 1];
      let segmentLength = 0;

      // セグメント長を取得(SOI, EOI以外)
      if (marker !== 0xd8 && marker !== 0xd9) {
        if (offset + 3 >= buffer.length) break;
        segmentLength = buffer.readUInt16BE(offset + 2);
      }

      const segment = {
        marker: `0xFF${marker
          .toString(16)
          .padStart(2, '0')
          .toUpperCase()}`,
        offset,
        length: segmentLength,
        description: this.getJpegSegmentDescription(marker),
      };

      segments.push(segment);

      // 特別な処理が必要なセグメント
      if (marker === 0xc0 || marker === 0xc2) {
        // SOF (Start of Frame) - 画像サイズ情報
        if (offset + 9 < buffer.length) {
          const height = buffer.readUInt16BE(offset + 5);
          const width = buffer.readUInt16BE(offset + 7);
          const components = buffer[offset + 9];

          segment.imageInfo = { width, height, components };
          console.log(`画像サイズ: ${width} x ${height}`);
          console.log(`色成分数: ${components}`);
        }
      } else if (marker === 0xe1) {
        // EXIF情報
        segment.hasExif = true;
        console.log('EXIF情報が含まれています');
      }

      console.log(
        `セグメント: ${segment.marker} (${segment.description}) - ${segmentLength}バイト`
      );

      if (marker === 0xd9) break; // EOI (End of Image)

      offset += segmentLength === 0 ? 2 : segmentLength + 2;
    }

    return segments;
  }

  // JPEGセグメント説明の取得
  getJpegSegmentDescription(marker) {
    const descriptions = {
      0xd8: 'SOI (Start of Image)',
      0xd9: 'EOI (End of Image)',
      0xc0: 'SOF0 (Start of Frame - Baseline)',
      0xc2: 'SOF2 (Start of Frame - Progressive)',
      0xc4: 'DHT (Define Huffman Table)',
      0xdb: 'DQT (Define Quantization Table)',
      0xdd: 'DRI (Define Restart Interval)',
      0xda: 'SOS (Start of Scan)',
      0xe0: 'APP0 (JFIF)',
      0xe1: 'APP1 (EXIF)',
      0xe2: 'APP2',
      0xfe: 'COM (Comment)',
    };

    return (
      descriptions[marker] ||
      `Unknown (0x${marker.toString(16)})`
    );
  }

  // PNG画像の詳細解析
  analyzePngDetails(buffer) {
    const pngSignature = Buffer.from([
      0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
    ]);

    if (!buffer.subarray(0, 8).equals(pngSignature)) {
      throw new Error('PNG形式ではありません');
    }

    console.log('\n=== PNG詳細解析 ===');

    let offset = 8; // PNGシグネチャをスキップ
    const chunks = [];

    while (offset < buffer.length) {
      if (offset + 8 > buffer.length) break;

      const length = buffer.readUInt32BE(offset);
      const type = buffer
        .subarray(offset + 4, offset + 8)
        .toString('ascii');

      const chunk = {
        type,
        length,
        offset,
        description: this.getPngChunkDescription(type),
      };

      // IHDR チャンク(画像ヘッダー)の詳細解析
      if (type === 'IHDR' && length >= 13) {
        const dataOffset = offset + 8;
        const width = buffer.readUInt32BE(dataOffset);
        const height = buffer.readUInt32BE(dataOffset + 4);
        const bitDepth = buffer[dataOffset + 8];
        const colorType = buffer[dataOffset + 9];
        const compression = buffer[dataOffset + 10];
        const filter = buffer[dataOffset + 11];
        const interlace = buffer[dataOffset + 12];

        chunk.imageInfo = {
          width,
          height,
          bitDepth,
          colorType:
            this.getPngColorTypeDescription(colorType),
          compression,
          filter,
          interlace: interlace === 1 ? 'Adam7' : 'None',
        };

        console.log(`画像サイズ: ${width} x ${height}`);
        console.log(`ビット深度: ${bitDepth}`);
        console.log(
          `色タイプ: ${chunk.imageInfo.colorType}`
        );
        console.log(
          `インターレース: ${chunk.imageInfo.interlace}`
        );
      }

      chunks.push(chunk);
      console.log(
        `チャンク: ${type} (${chunk.description}) - ${length}バイト`
      );

      offset += 8 + length + 4; // length + type + data + CRC

      if (type === 'IEND') break; // 画像終了
    }

    return chunks;
  }

  // PNGチャンク説明の取得
  getPngChunkDescription(type) {
    const descriptions = {
      IHDR: 'Image Header',
      PLTE: 'Palette',
      IDAT: 'Image Data',
      IEND: 'Image End',
      tRNS: 'Transparency',
      gAMA: 'Gamma',
      cHRM: 'Chromaticity',
      sRGB: 'sRGB',
      iCCP: 'ICC Profile',
      tEXt: 'Text',
      zTXt: 'Compressed Text',
      iTXt: 'International Text',
    };

    return descriptions[type] || 'Unknown';
  }

  // PNG色タイプの説明
  getPngColorTypeDescription(colorType) {
    const types = {
      0: 'Grayscale',
      2: 'RGB',
      3: 'Palette',
      4: 'Grayscale + Alpha',
      6: 'RGB + Alpha',
    };

    return types[colorType] || `Unknown (${colorType})`;
  }

  // 画像ファイルの完全解析
  analyzeImageFile(buffer) {
    console.log('=== 画像ファイル完全解析 ===');

    const formatInfo = this.detectImageFormat(buffer);

    if (!formatInfo.isValid) {
      return formatInfo;
    }

    let detailAnalysis = null;

    try {
      switch (formatInfo.format) {
        case 'jpeg':
          detailAnalysis = this.analyzeJpegDetails(buffer);
          break;
        case 'png':
          detailAnalysis = this.analyzePngDetails(buffer);
          break;
        default:
          console.log('詳細解析は未対応の形式です');
      }
    } catch (error) {
      console.error('詳細解析エラー:', error.message);
    }

    return {
      ...formatInfo,
      detailAnalysis,
    };
  }
}

// 画像解析のデモンストレーション
const imageAnalyzer = new ImageAnalyzer();

// サンプル画像データの作成(実際のファイルヘッダーを模擬)
const sampleImages = {
  jpeg: Buffer.from([
    0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49,
    0x46, 0x00, 0x01, 0x01, 0x01, 0x00, 0x48, 0x00, 0x48,
    0x00, 0x00, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x01, 0x90,
    0x01, 0x40, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01,
    0x03, 0x11, 0x01, 0xff, 0xd9,
  ]),

  png: Buffer.from([
    0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
    0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00,
    0x01, 0x40, 0x00, 0x00, 0x01, 0x90, 0x08, 0x02, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
    0x44, 0xae, 0x42, 0x60, 0x82,
  ]),
};

// 各サンプル画像の解析
Object.entries(sampleImages).forEach(([format, buffer]) => {
  console.log(`\n${'='.repeat(50)}`);
  console.log(`${format.toUpperCase()} サンプル解析`);
  console.log(`${'='.repeat(50)}`);

  imageAnalyzer.analyzeImageFile(buffer);
});

ネットワークプロトコルのパケット作成

ネットワーク通信では、バイナリ形式でのデータ交換が一般的です。Buffer を使用して、効率的なプロトコル実装を行うことができます。

javascript// ネットワークプロトコル処理の包括的なクラス
class NetworkProtocolHandler {
  constructor() {
    // プロトコル定義
    this.protocols = {
      // カスタムTCPプロトコル
      customTcp: {
        headerSize: 12,
        fields: {
          magic: { offset: 0, size: 4, type: 'uint32' },
          version: { offset: 4, size: 1, type: 'uint8' },
          messageType: {
            offset: 5,
            size: 1,
            type: 'uint8',
          },
          flags: { offset: 6, size: 2, type: 'uint16' },
          payloadLength: {
            offset: 8,
            size: 4,
            type: 'uint32',
          },
        },
      },

      // HTTPライクプロトコル
      httpLike: {
        headerSize: 16,
        fields: {
          method: { offset: 0, size: 4, type: 'string' },
          statusCode: {
            offset: 4,
            size: 2,
            type: 'uint16',
          },
          contentLength: {
            offset: 6,
            size: 4,
            type: 'uint32',
          },
          timestamp: {
            offset: 10,
            size: 6,
            type: 'timestamp',
          },
        },
      },
    };

    // メッセージタイプ定義
    this.messageTypes = {
      0x01: 'CONNECT',
      0x02: 'DISCONNECT',
      0x03: 'DATA',
      0x04: 'ACK',
      0x05: 'HEARTBEAT',
      0x06: 'ERROR',
    };
  }

  // カスタムTCPパケットの作成
  createCustomTcpPacket(messageType, payload, flags = 0) {
    console.log('=== カスタムTCPパケット作成 ===');

    const protocol = this.protocols.customTcp;
    const payloadBuffer = Buffer.isBuffer(payload)
      ? payload
      : Buffer.from(payload, 'utf8');
    const totalSize =
      protocol.headerSize + payloadBuffer.length;

    const packet = Buffer.alloc(totalSize);

    // ヘッダー情報の設定
    const magic = 0x12345678; // マジックナンバー
    const version = 1;

    let offset = 0;

    // マジックナンバー (4バイト)
    packet.writeUInt32BE(magic, offset);
    offset += 4;

    // バージョン (1バイト)
    packet.writeUInt8(version, offset);
    offset += 1;

    // メッセージタイプ (1バイト)
    packet.writeUInt8(messageType, offset);
    offset += 1;

    // フラグ (2バイト)
    packet.writeUInt16BE(flags, offset);
    offset += 2;

    // ペイロード長 (4バイト)
    packet.writeUInt32BE(payloadBuffer.length, offset);
    offset += 4;

    // ペイロードをコピー
    payloadBuffer.copy(packet, offset);

    console.log(`パケットサイズ: ${totalSize} bytes`);
    console.log(
      `ヘッダー: ${packet
        .subarray(0, protocol.headerSize)
        .toString('hex')}`
    );
    console.log(
      `ペイロード: ${payloadBuffer.length} bytes`
    );
    console.log(
      `メッセージタイプ: ${
        this.messageTypes[messageType] || 'UNKNOWN'
      }`
    );

    return packet;
  }

  // カスタムTCPパケットの解析
  parseCustomTcpPacket(packet) {
    console.log('\n=== カスタムTCPパケット解析 ===');

    const protocol = this.protocols.customTcp;

    if (packet.length < protocol.headerSize) {
      throw new Error('パケットサイズが不正です');
    }

    let offset = 0;

    // ヘッダー情報の読み取り
    const magic = packet.readUInt32BE(offset);
    offset += 4;

    const version = packet.readUInt8(offset);
    offset += 1;

    const messageType = packet.readUInt8(offset);
    offset += 1;

    const flags = packet.readUInt16BE(offset);
    offset += 2;

    const payloadLength = packet.readUInt32BE(offset);
    offset += 4;

    // マジックナンバーの検証
    if (magic !== 0x12345678) {
      throw new Error(
        `無効なマジックナンバー: 0x${magic.toString(16)}`
      );
    }

    // ペイロード長の検証
    const expectedTotalSize =
      protocol.headerSize + payloadLength;
    if (packet.length !== expectedTotalSize) {
      throw new Error(
        `パケットサイズ不整合: 期待値${expectedTotalSize}, 実際${packet.length}`
      );
    }

    // ペイロードの抽出
    const payload = packet.subarray(offset);

    const result = {
      magic: `0x${magic.toString(16)}`,
      version,
      messageType: {
        code: messageType,
        name: this.messageTypes[messageType] || 'UNKNOWN',
      },
      flags: {
        value: flags,
        binary: flags.toString(2).padStart(16, '0'),
      },
      payloadLength,
      payload,
      payloadText: payload.toString('utf8'),
    };

    console.log('解析結果:');
    console.log(`  マジック: ${result.magic}`);
    console.log(`  バージョン: ${result.version}`);
    console.log(
      `  メッセージタイプ: ${
        result.messageType.name
      } (0x${messageType.toString(16)})`
    );
    console.log(
      `  フラグ: 0x${flags.toString(16)} (${
        result.flags.binary
      })`
    );
    console.log(`  ペイロード長: ${result.payloadLength}`);
    console.log(`  ペイロード: "${result.payloadText}"`);

    return result;
  }
}

// ネットワークプロトコル処理のデモンストレーション
const protocolHandler = new NetworkProtocolHandler();

// カスタムTCPプロトコルのテスト
console.log('カスタムTCPプロトコルテスト');
console.log('='.repeat(40));

const tcpPacket = protocolHandler.createCustomTcpPacket(
  0x03, // DATA
  'Hello, Network Protocol!',
  0x0001 // フラグ
);

const parsedTcp =
  protocolHandler.parseCustomTcpPacket(tcpPacket);

これらの実用的なバイナリデータ処理の例を通じて、Buffer を使った高度なデータ操作の技術を習得できます。画像解析、ネットワークプロトコル、暗号化処理など、実際の開発現場で必要となる様々な場面で応用していただけるでしょう。

まとめ

Node.js の Buffer クラスを使ったバイナリデータ処理について、基本概念から実用的な応用例まで詳しく解説してまいりました。Buffer は JavaScript の標準的な文字列処理では対応できない、低レベルなデータ操作を可能にする重要な機能です。

重要なポイント

項目基本概念実用的な応用
Buffer 作成Buffer.alloc(), Buffer.from() の適切な使い分けセキュリティを考慮した安全な作成方法
データ操作数値型・エンディアンを意識した読み書き実際のファイル形式・プロトコルでの活用
文字エンコーディングUTF-8, ASCII, Base64 の相互変換多言語対応と文字境界の安全な処理
実用例画像解析、ネットワーク通信、暗号化処理本格的なアプリケーション開発への応用

開発における実践的な指針

  1. 安全性の確保: Buffer.alloc() を基本とし、Buffer.allocUnsafe() は必要な場合のみ使用
  2. エンディアンの意識: ネットワーク通信では Big Endian、プラットフォーム間でのデータ共有では統一された形式を使用
  3. 文字エンコーディングの明示: 文字列変換時は必ずエンコーディングを指定
  4. エラーハンドリング: バイナリデータ処理では予期しないフォーマットに対する適切な例外処理を実装
  5. パフォーマンス最適化: 大量データ処理では Buffer の再利用とメモリ効率を考慮

活用場面での効果

Buffer を適切に活用することで、以下のような効果を得られます:

ファイル処理の向上

  • 画像・動画ファイルの効率的な解析と変換
  • バイナリ形式での設定ファイルやデータファイルの処理
  • ファイルアップロード機能における形式検証の実装

ネットワーク通信の最適化

  • カスタムプロトコルの実装による通信効率の向上
  • WebSocket でのバイナリメッセージ処理
  • HTTP/2 でのバイナリフレーム操作

セキュリティ機能の実装

  • 暗号化・復号化処理の安全な実装
  • デジタル署名とハッシュ値計算
  • セキュアなデータ保存と転送

国際化対応の実現

  • 多言語文字列の適切な処理
  • 文字エンコーディングの自動検出と変換
  • Unicode 正規化による文字列統一

Node.js での開発において、Buffer を使ったバイナリデータ処理は避けて通れない重要な技術です。本記事で紹介した手法を参考に、より効率的で安全なアプリケーション開発を進めていただければと思います。

特に、ファイルアップロード機能、リアルタイム通信、データ変換処理などを実装する際には、Buffer の特性を理解し、適切に活用することで、パフォーマンスとセキュリティの両面で優れたソリューションを構築できるでしょう。

関連リンク

公式ドキュメント

技術仕様・標準

セキュリティ関連

パフォーマンス・最適化

実装例・ライブラリ

  • Sharp - 高性能画像処理ライブラリ
  • ws - WebSocket 実装
  • node-forge - 暗号化ライブラリ
  • iconv-lite - 文字エンコーディング変換

学習リソース