Пример #1
0
 /**
  * This writes a string value to the file The string UTF-8 encoded then stored, so the
  * byteArray.length may be greater than s.length(), so make the column wider to be safe.
  *
  * @param s a String. If s is too long, it is truncated. If too short, it is space-padded at the
  *     end.
  * @param length
  * @return the corresponding byte[] (or null if s is null)
  */
 public void writeString(int col, int row, String s) throws IOException {
   int colWidth = columnWidths[col];
   if (s.length() > colWidth) s = s.substring(0, colWidth);
   byte ar[] = String2.getUTF8Bytes(s);
   // truncating is tricky because don't want to have 1/2 of a 2-byte char
   while (ar.length > colWidth) {
     if (reallyVerbose)
       String2.log("s=" + String2.annotatedString(s) + " will be shortened by 1 char.");
     s = s.substring(0, s.length() - 1); // remove last byte
     ar = String2.getUTF8Bytes(s);
   }
   if (ar.length < colWidth) {
     byte tar[] = new byte[colWidth];
     System.arraycopy(ar, 0, tar, 0, ar.length);
     Arrays.fill(tar, ar.length, colWidth, (byte) ' ');
     ar = tar;
   }
   write(col, row, ar);
 }