из java/c# в Delphi [требует правки]

493
20 марта 2017, 07:54

Привет. Есть необходимость перевести данный ниже код в Delphi. Кто поможет? труд оплачу.

package ru.inteltelecom.cx.crossplatform.data.binary; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.UUID; 
import ru.inteltelecom.cx.crossplatform.exception.CxException; 
import ru.inteltelecom.cx.crossplatform.exception.CxInvalidArgumentException; 
import ru.inteltelecom.cx.crossplatform.exception.CxInvalidOperationException; 
import ru.inteltelecom.cx.crossplatform.utils.BitConverter; 
import ru.inteltelecom.cx.crossplatform.utils.DataUtils; 
import ru.inteltelecom.cx.crossplatform.utils.InternalLog; 
 
public class DataReaderLevel 
{ 
  public static boolean ALLOW_WAIT_BUFFER_COMPLETION = false; 
 
  public static int BYTE_WAITING_SLEEP_ITERATION_DURATION = 150; 
 
  public static int BYTE_WAITING_SLEEP_ITERATIONS_COUNT = 4; 
 
  public static int BYTES_PER_SLEEP_ITERATION = 500; 
 
  public static int SLEEP_ITERATIONS_COUNT_MAX = 100; 
 
  public static int SLEEP_ITERATIONS_COUNT_MIN = 4; 
 
  public static int SLEEP_ITERATION_LENGTH = 150; 
 
  protected int _flagCurrent = -1; 
 
  protected int _flagCurrentByte = -1; 
 
  protected int _flagBitsIndex = 0; 
  private DataReader _reader; 
  private int _lengthLimit = -1; 
 
  protected DataReaderLevel(DataReader owner_) 
  { 
    this._reader = owner_; 
  } 
 
  public DataReader getReader() { 
    return this._reader; 
  } 
 
  public int getNextFlag() throws IOException 
  { 
    if (this._flagBitsIndex == 0) { 
      this._flagCurrentByte = getUByteAsInt(); 
      this._flagBitsIndex += 1; 
 
      this._flagCurrent = (this._flagCurrentByte & 0x3); 
    } 
    else { 
      this._flagBitsIndex = ((this._flagBitsIndex + 1) % 4); 
      this._flagCurrentByte >>= 2; 
 
      this._flagCurrent = (this._flagCurrentByte & 0x3); 
    } 
    return this._flagCurrent; 
  } 
 
  public static boolean isData(int flag) 
  { 
    return flag == 0; 
  } 
 
  public static boolean isShort(int flag) { 
    return flag == 3; 
  } 
 
  public static boolean isZero(int flag) { 
    return flag == 2; 
  } 
 
  public static boolean isNull(int flag) { 
    return flag == 1; 
  } 
 
  public static CxException newInvalidValueFlag(int flag_) { 
    return new CxException("Invalid value flag: " + flag_); 
  } 
 
  public boolean hasLengthLimit() { 
    return this._lengthLimit >= 0; 
  } 
 
  public int getBytesLeft() { 
    return this._lengthLimit; 
  } 
 
  public Object readValue(int typeCode_) 
    throws IOException 
  { 
    switch (typeCode_) { 
    case 0: 
      return null; 
    case 1: 
      return DataUtils.getSByte(readSByte()); 
    case 2: 
      return readNSByte(); 
    case 5: 
      return DataUtils.getInt(readInt()); 
    case 6: 
      return readNInt(); 
    case 7: 
      return DataUtils.getLong(readLong()); 
    case 8: 
      return readNLong(); 
    case 11: 
      return DataUtils.getDouble(readDouble()); 
    case 12: 
      return readNDouble(); 
    case 13: 
      return DataUtils.getBool(readBool()); 
    case 14: 
      return readNBool(); 
    case 15: 
      return readString(); 
    case 16: 
      return readDateTime(); 
    case 17: 
      return readDate(); 
    case 18: 
      return readTime(); 
    case 19: 
      return readUUID(); 
    case 3: 
    case 4: 
    case 9: 
    case 10: } throw new CxInvalidArgumentException("typeCode_", "Type is not supported TypeCode: " + typeCode_); 
  } 
 
  public boolean readBool() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isZero(flag)) { 
      return false; 
    } 
    if (isData(flag)) { 
      return true; 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Boolean readNBool() throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return Boolean.FALSE; 
    } 
    if (isData(flag)) { 
      return Boolean.TRUE; 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public boolean readBoolFromNInt() throws IOException 
  { 
    Integer value = readNInt(); 
    return (value != null) && (value.intValue() == 1); 
  } 
 
  public boolean readBoolFromInt() throws IOException { 
    return readInt() == 1; 
  } 
 
  public byte readSByte() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isZero(flag)) { 
      return 0; 
    } 
    if (isData(flag)) { 
      return getByte(); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public int readInt() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isZero(flag)) { 
      return 0; 
    } 
    if (isShort(flag)) { 
      return getByte(); 
    } 
    if (isData(flag)) { 
      return BitConverter.toInt32(getByte(), getByte(), getByte(), getByte()); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  protected Integer readInnerLevelSize() throws IOException 
  { 
    int flag = getNextFlag(); 
    if (isNull(flag)) 
      return null; 
    if (isZero(flag)) { 
      return null; 
    } 
 
    int innerLevelLength = readUIntInternal(flag); 
    if ((this._lengthLimit > 0) && (innerLevelLength > 0)) { 
      if (innerLevelLength > this._lengthLimit) { 
        throw new CxInvalidOperationException("Reading after the end of the DataReaderLevel", "Inner level length: " + innerLevelLength + " is larger than the length limit: " + this._lengthLimit); 
      } 
 
      this._lengthLimit -= innerLevelLength; 
    } 
 
    return DataUtils.getInt(innerLevelLength); 
  } 
 
  protected int readUIntInternal(int flag_) throws IOException, CxException 
  { 
    if (isZero(flag_)) { 
      return 0; 
    } 
    if (isShort(flag_)) { 
      return BitConverter.toInt32(getByte(), (byte)0, (byte)0, (byte)0); 
    } 
    if (isData(flag_)) { 
      return BitConverter.toInt32(getByte(), getByte(), getByte(), getByte()); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public long readLong() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isZero(flag)) { 
      return 0L; 
    } 
    if (isShort(flag)) { 
      return BitConverter.toInt32(getByte(), getByte(), getByte(), getByte()); 
    } 
    if (isData(flag)) { 
      return BitConverter.toInt64(getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte()); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public double readDouble() throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isZero(flag)) { 
      return 0.0D; 
    } 
    if (isData(flag)) { 
      return BitConverter.toDouble(getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte()); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Byte readNSByte() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return DataUtils.ZERO_NBYTE; 
    } 
    if (isData(flag)) { 
      return DataUtils.getSByte(getByte()); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Integer readNInt() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return DataUtils.ZERO_NINT; 
    } 
    if (isShort(flag)) { 
      return DataUtils.getInt(BitConverter.toInt32(getByte(), (byte)0, (byte)0, (byte)0)); 
    } 
    if (isData(flag)) { 
      return DataUtils.getInt(BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Long readNLong() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return DataUtils.ZERO_NLONG; 
    } 
    if (isShort(flag)) { 
      return DataUtils.getLong(BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())); 
    } 
    if (isData(flag)) { 
      return DataUtils.getLong(BitConverter.toInt64(getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte())); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Double readNDouble() throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return DataUtils.ZERO_NDOUBLE; 
    } 
    if (isData(flag)) { 
      return DataUtils.getDouble(BitConverter.toDouble(getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte(), getByte())); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public String readString() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return ""; 
    } 
 
    int length = readUIntInternal(flag); 
    boolean isLimited = this._lengthLimit >= 0; 
 
    if ((isLimited) &&  
      (this._lengthLimit - length < 0)) { 
      throw new CxEndOfLevelException(this._lengthLimit, length); 
    } 
 
    if (length > 0) { 
      byte[] buffer = new byte[length]; 
      int resultLength = readBuffer(buffer, length); 
 
      if (isLimited) { 
        this._lengthLimit -= Math.min(length, resultLength); 
      } 
      if (resultLength != length) { 
        throw new CxException("Invalid buffer length (Expected: " + length + ", Actual: " + resultLength + ")"); 
      } 
      return DataUtils.getString(buffer); 
    } 
 
    return ""; 
  } 
 
  public byte[] readByteArray() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) { 
      return null; 
    } 
    if (isZero(flag)) { 
      return new byte[0]; 
    } 
 
    int length = readUIntInternal(flag); 
    boolean isLimited = this._lengthLimit >= 0; 
 
    if ((isLimited) &&  
      (this._lengthLimit - length < 0)) { 
      throw new CxEndOfLevelException(this._lengthLimit, length); 
    } 
 
    if (length > 0) { 
      byte[] buffer = new byte[length]; 
      int resultLength = readBuffer(buffer, length); 
 
      if (isLimited) { 
        this._lengthLimit -= Math.min(length, resultLength); 
      } 
      if (resultLength != length) { 
        throw new CxException("Invalid buffer length (Expected: " + length + ", Actual: " + resultLength + ")"); 
      } 
      return buffer; 
    } 
 
    return new byte[0]; 
  } 
 
  public Date readDateTime() 
    throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) 
      return null; 
    if (isData(flag)) 
    { 
      Calendar cal = DataUtils.setTimeFromInt(null, BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())); 
      DataUtils.setDateFromInt(cal, BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())); 
      return cal.getTime(); 
    } 
    throw NewInvalidValueFlag(); 
  } 
 
  public Date readDate() throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) 
      return null; 
    if (isData(flag)) 
    { 
      return DataUtils.setDateFromInt(null, BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())).getTime(); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public Date readTime() throws IOException 
  { 
    int flag = getNextFlag(); 
 
    if (isNull(flag)) 
      return null; 
    if (isData(flag)) 
    { 
      return DataUtils.setTimeFromInt(null, BitConverter.toInt32(getByte(), getByte(), getByte(), getByte())).getTime(); 
    } 
 
    throw NewInvalidValueFlag(); 
  } 
 
  public UUID readUUID() 
    throws IOException 
  { 
    String strValue = readString(); 
    if (strValue == null) { 
      return null; 
    } 
 
    return UUID.fromString(strValue); 
  } 
 
  public void release() 
  { 
    this._reader.releaseLevel(this); 
  } 
 
  protected void reset() throws IOException { 
    try { 
      if (this._lengthLimit > 0) { 
        int resultLength = readBuffer(new byte[this._lengthLimit], this._lengthLimit); 
 
        if (resultLength != this._lengthLimit) { 
          throw new CxException("Error while skipping inner level data. Invalid buffer length (Expected: " + this._lengthLimit + ", Actual: " + resultLength + ")"); 
        } 
      } 
    } 
    finally 
    { 
      this._flagCurrent = -1; 
      this._flagCurrentByte = -1; 
      this._flagBitsIndex = 0; 
      this._lengthLimit = -1; 
    } 
  } 
 
  protected InputStream getStream() 
  { 
    return this._reader.getStream(); 
  } 
 
  private byte getByte() throws IOException 
  { 
    return (byte)getUByteAsInt(); 
  } 
 
  protected int readBuffer(byte[] buffer, int length) throws IOException { 
    InputStream stream = getStream(); 
    int resultLength = stream.read(buffer); 
 
    if (ALLOW_WAIT_BUFFER_COMPLETION) { 
      int sleepCount = 0; 
      int maxSleepCount = Math.max(SLEEP_ITERATIONS_COUNT_MIN, Math.min((length - resultLength) / BYTES_PER_SLEEP_ITERATION, SLEEP_ITERATIONS_COUNT_MAX)); 
 
      if (resultLength < length) 
      { 
        for (; (resultLength < length) && (sleepCount < maxSleepCount);  
          goto 105) 
        { 
          InternalLog.debug("Reading ", DataUtils.getInt(length), " bytes from stream. ", DataUtils.getInt(resultLength), " bytes are ready. Will wait for buffer completion"); 
          try 
          { 
            sleepCount++; 
            Thread.sleep(SLEEP_ITERATION_LENGTH); 
          } catch (InterruptedException ex) { 
            InternalLog.debug(ex, "Error while buffer completion waiting"); 
          } 
          int count = -1; 
 
          if ((resultLength < length) && ((count = stream.read(buffer, resultLength, length - resultLength)) > 0)) 
          { 
            resultLength += count; 
          } 
        } 
 
        InternalLog.debug("Waiting for buffer completion is finished. ", DataUtils.getInt(resultLength), " bytes of ", DataUtils.getInt(length), " have been read"); 
      } 
 
    } 
 
    return resultLength; 
  } 
 
  protected int getUByteAsInt() throws IOException { 
    if (this._lengthLimit > 0) 
      this._lengthLimit -= 1; 
    else if (this._lengthLimit == 0) { 
      throw new CxEndOfLevelException(); 
    } 
    int byteVal = getStream().read(); 
 
    if ((byteVal == -1) && (ALLOW_WAIT_BUFFER_COMPLETION)) { 
      byteVal = waitAndReadByte(); 
    } 
 
    if (byteVal == -1) { 
      throw new CxInvalidOperationException("End of stream exception", "Unable to read byte, because the end of the input stream occurred"); 
    } 
 
    return byteVal; 
  } 
 
  protected int waitAndReadByte() throws IOException { 
    int sleepCount = 0; 
    int byteVal = -1; 
    while ((byteVal == -1) && (sleepCount < BYTE_WAITING_SLEEP_ITERATIONS_COUNT)) { 
      InternalLog.debug("Waiting for buffer completion, 1 byte is required, sleep step duration: ", DataUtils.getInt(BYTE_WAITING_SLEEP_ITERATION_DURATION)); 
      try 
      { 
        sleepCount++; 
        Thread.sleep(BYTE_WAITING_SLEEP_ITERATION_DURATION); 
      } catch (InterruptedException ex) { 
        InternalLog.debug(ex, "Error while waiting for buffer completion"); 
      } 
      byteVal = getStream().read(); 
    } 
    InternalLog.debug("Waiting for buffer completion is finished"); 
    return byteVal; 
  } 
 
  private CxException NewInvalidValueFlag() { 
    return new CxException("Invalid value flag: " + getFlagName(this._flagCurrent)); 
  } 
 
  void setLengthLimit(int value_) 
  { 
    this._lengthLimit = value_; 
  } 
 
  private String getFlagName(int flagCurrent_) 
  { 
    switch (flagCurrent_) { 
    case 0: 
      return "DATA"; 
    case 1: 
      return "NULL"; 
    case 3: 
      return "SHORT"; 
    case 2: 
      return "ZERO"; 
    } 
    return "Unknown " + flagCurrent_; 
  } 
}

READ ALSO
Поиск существующего фрагмента

Поиск существующего фрагмента

В активити создаю фрагменты frag1,frag2Сохраняю идентификатор frag2:

245
Как запускать единственный экземпляр Activity из Fragment?

Как запускать единственный экземпляр Activity из Fragment?

Имеется класс Home extends Activity с NavigationDrawer в нёмЧерез displayView создаю Fragment'ы в зависимости от нажатия элемента

241
Не работает Toast [требует правки]

Не работает Toast [требует правки]

Пытаюсь программировать на АндроидЧасто использовал Тосты

349
Лента новостей в Android

Лента новостей в Android

Есть ли специальное событие у ScrollView, когда его полностью проматывают? Это нужно, чтобы новости подгружались постепенно, а не сразу

455