public static byte[] escapeEasternUnicodeByteStream(byte[] origBytes,String origString){
  if (origBytes == null) {
    return null;
  }
  if (origBytes.length == 0) {
    return new byte[0];
  }
  int bytesLen=origBytes.length;
  int bufIndex=0;
  int strIndex=0;
  ByteArrayOutputStream bytesOut=new ByteArrayOutputStream(bytesLen);
  while (true) {
    if (origString.charAt(strIndex) == '\\') {
      bytesOut.write(origBytes[bufIndex++]);
    }
 else {
      int loByte=origBytes[bufIndex];
      if (loByte < 0) {
        loByte+=256;
      }
      bytesOut.write(loByte);
      if (loByte >= 0x80) {
        if (bufIndex < (bytesLen - 1)) {
          int hiByte=origBytes[bufIndex + 1];
          if (hiByte < 0) {
            hiByte+=256;
          }
          bytesOut.write(hiByte);
          bufIndex++;
          if (hiByte == 0x5C) {
            bytesOut.write(hiByte);
          }
        }
      }
 else       if (loByte == 0x5c) {
        if (bufIndex < (bytesLen - 1)) {
          int hiByte=origBytes[bufIndex + 1];
          if (hiByte < 0) {
            hiByte+=256;
          }
          if (hiByte == 0x62) {
            bytesOut.write(0x5c);
            bytesOut.write(0x62);
            bufIndex++;
          }
        }
      }
      bufIndex++;
    }
    if (bufIndex >= bytesLen) {
      break;
    }
    strIndex++;
  }
  return bytesOut.toByteArray();
}

public static byte [ ] escapeEasternUnicodeByteStream ( byte [ ] origBytes , String origString ) { if ( origBytes == null ) { return null ; } if ( origBytes . length == 0 ) { return new byte [ 0 ] ; } int bytesLen = origBytes . length ; int bufIndex = 0 ; int strIndex = 0 ; ByteArrayOutputStream bytesOut = new ByteArrayOutputStream ( bytesLen ) ; while ( true ) { if ( origString . charAt ( strIndex ) == '\\' ) { bytesOut . write ( origBytes [ bufIndex ++ ] ) ; } else { int loByte = origBytes [ bufIndex ] ; if ( loByte < 0 ) { loByte += 256 ; } bytesOut . write ( loByte ) ; if ( loByte >= 0x80 ) { if ( bufIndex < ( bytesLen - 1 ) ) { int hiByte = origBytes [ bufIndex + 1 ] ; if ( hiByte < 0 ) { hiByte += 256 ; } bytesOut . write ( hiByte ) ; bufIndex ++ ; if ( hiByte == 0x5C ) { bytesOut . write ( hiByte ) ; } } } else if ( loByte == 0x5c ) { if ( bufIndex < ( bytesLen - 1 ) ) { int hiByte = origBytes [ bufIndex + 1 ] ; if ( hiByte < 0 ) { hiByte += 256 ; } if ( hiByte == 0x62 ) { bytesOut . write ( 0x5c ) ; bytesOut . write ( 0x62 ) ; bufIndex ++ ; } } } bufIndex ++ ; } if ( bufIndex >= bytesLen ) { break ; } strIndex ++ ; } return bytesOut . toByteArray ( ) ; }
unfortunately , sjis has 0x5c as a high byte in some of its double - byte characters , so we need to escape it .
