예제 #1
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Reads an array of strings from the TIFF file.
   *
   * @param count Number of strings to read
   * @param value Offset from which to read
   */
  protected String[] readASCIIArray(long count, long value) throws IOException {
    _raf.seek(value);

    int nstrs = 0;
    List list = new LinkedList();
    byte[] buf = new byte[(int) count];
    _raf.read(buf);
    StringBuffer strbuf = new StringBuffer();
    for (int i = 0; i < count; i++) {
      int b = buf[i];
      if (b == 0) {
        list.add(strbuf.toString());
        strbuf.setLength(0);
      } else {
        strbuf.append((char) b);
      }
    }
    /* We can't use ArrayList.toArray because that returns an
    Object[], not a String[] ... sigh. */
    String[] strs = new String[nstrs];
    ListIterator iter = list.listIterator();
    for (int i = 0; i < nstrs; i++) {
      strs[i] = (String) iter.next();
    }
    return strs;
  }
 public static void writeArray(String s, int[] x) {
   try {
     RandomAccessFile output = new RandomAccessFile(s, "rw");
     for (int i = 0; i < x.length; i++) output.writeInt(x[i]);
   } catch (IOException e) {
     System.out.println(e.getMessage());
   }
 }
예제 #3
0
 /**
  * Record a specific string
  *
  * @param s
  */
 public static void record(String s) {
   try {
     if (!recording) return;
     Date now = new Date();
     String ts = stamp.format(now);
     R.writeBytes(ts);
     R.writeBytes(" ");
     R.writeBytes(s);
     R.writeBytes("\n");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
예제 #4
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Reads a string value from the TIFF file.
   *
   * @param count Length of string
   * @param value Offset of string
   */
  protected String readASCII(long count, long value) throws IOException {
    _raf.seek(value);

    byte[] buffer = new byte[(int) count];
    _raf.read(buffer);

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < count; i++) {
      if (buffer[i] == 0) {
        break;
      }
      sb.append((char) buffer[i]);
    }
    return sb.toString();
  }
예제 #5
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /** Reads an array of SRATIONAL values and returns it as an array of Rational. */
  protected Rational[] readSignedRationalArray(long count, long value) throws IOException {
    _raf.seek(value);

    byte[] buffer = new byte[(int) calcValueSize(SRATIONAL, count)];
    _raf.read(buffer);
    DataInputStream stream = new DataInputStream(new ByteArrayInputStream(buffer));

    Rational[] rarray = new Rational[(int) count];
    for (int i = 0; i < count; i++) {
      long numer = ModuleBase.readSignedInt(stream, _bigEndian, null);
      long denom = ModuleBase.readSignedInt(stream, _bigEndian, null);
      rarray[i] = new Rational(numer, denom);
    }

    return rarray;
  }
예제 #6
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /** Reads an SRATIONAL value and returns it as a Rational. */
  protected Rational readSignedRational(long count, long value) throws IOException {
    _raf.seek(value);

    long numer = ModuleBase.readSignedInt(_raf, _bigEndian);
    long denom = ModuleBase.readSignedInt(_raf, _bigEndian);
    return new Rational(numer, denom);
  }
예제 #7
0
 /**
  * Start recording
  *
  * @param stub
  */
 public static void startRecorder(String stub) {
   try {
     stamp = new SimpleDateFormat("HH:mm:ss");
     SimpleDateFormat dateformat = new SimpleDateFormat("yyMMdd");
     Date d = new Date();
     String s = dateformat.format(d);
     String name = logdir + stub + s + ".txt";
     R = new RandomAccessFile(name, "rw");
     System.out.println("Log file: " + name);
     long N = R.length();
     R.seek(N);
     recording = true;
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
예제 #8
0
  /**
   * Read block from file.
   *
   * @param file - File to read.
   * @param off - Marker position in file to start read from if {@code -1} read last blockSz bytes.
   * @param blockSz - Maximum number of chars to read.
   * @param lastModified - File last modification time.
   * @return Read file block.
   * @throws IOException In case of error.
   */
  public static VisorFileBlock readBlock(File file, long off, int blockSz, long lastModified)
      throws IOException {
    RandomAccessFile raf = null;

    try {
      long fSz = file.length();
      long fLastModified = file.lastModified();

      long pos = off >= 0 ? off : Math.max(fSz - blockSz, 0);

      // Try read more that file length.
      if (fLastModified == lastModified && fSz != 0 && pos >= fSz)
        throw new IOException(
            "Trying to read file block with wrong offset: " + pos + " while file size: " + fSz);

      if (fSz == 0)
        return new VisorFileBlock(file.getPath(), pos, fLastModified, 0, false, EMPTY_FILE_BUF);
      else {
        int toRead = Math.min(blockSz, (int) (fSz - pos));

        byte[] buf = new byte[toRead];

        raf = new RandomAccessFile(file, "r");

        raf.seek(pos);

        int cntRead = raf.read(buf, 0, toRead);

        if (cntRead != toRead)
          throw new IOException(
              "Count of requested and actually read bytes does not match [cntRead="
                  + cntRead
                  + ", toRead="
                  + toRead
                  + ']');

        boolean zipped = buf.length > 512;

        return new VisorFileBlock(
            file.getPath(), pos, fSz, fLastModified, zipped, zipped ? zipBytes(buf) : buf);
      }
    } finally {
      U.close(raf, null);
    }
  }
예제 #9
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /** Reads a TIFF array of signed 16-bit values and returns it as an int array. */
  protected int[] readSShortArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    int[] iarray = new int[(int) count];
    for (int i = 0; i < count; i++) {
      iarray[i] = ModuleBase.readSignedShort(_raf, _bigEndian);
    }
    return iarray;
  }
예제 #10
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Reads a TIFF array of signed 32-bit integer values and returns it as a long array.
   *
   * @param type TIFF type to read; must be a 32-bit type
   * @param count Number of values to read
   * @param value Offset from which to read
   */
  protected long[] readLongArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    long[] array = new long[(int) count];
    for (int i = 0; i < count; i++) {
      array[i] = readUnsigned(type);
    }
    return array;
  }
예제 #11
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /** Reads a TIFF array of unsigned 16-bit values and returns it as an int array. */
  protected int[] readShortArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    int[] iarray = new int[(int) count];
    for (int i = 0; i < count; i++) {
      iarray[i] = (int) readUnsigned(type);
    }
    return iarray;
  }
예제 #12
0
 /**
  * Stop recording. This is not strictly necessary since the RandomAccessFile will keep most of the
  * information that has been logged.
  */
 public static void endRecord() {
   try {
     if (!recording) return;
     if (R != null) R.close();
     if (recording) recording = false;
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
예제 #13
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Reads a TIFF array of DOUBLE 64-bit values and returns it as a double array.
   *
   * @param count Number of values to read
   * @param value Offset from which to read
   */
  protected double[] readDoubleArray(long count, long value) throws IOException {
    _raf.seek(value);

    double[] darray = new double[(int) count];
    for (int i = 0; i < count; i++) {
      darray[i] = ModuleBase.readDouble(_raf, _bigEndian);
    }

    return darray;
  }
  public static String readArray(String s) {
    String output = "[ ";
    boolean endOfFile = false;

    try {
      RandomAccessFile input = new RandomAccessFile(s, "rw");
      input.seek(0);

      while (!endOfFile) {
        try {
          output = output + input.readInt() + " ";
        } catch (EOFException e) {
          endOfFile = true;
          output = output + "]";
        }
      }
    } catch (IOException e) {
      return e.getMessage();
    }

    return output;
  }
예제 #15
0
  /**
   * Decode file charset.
   *
   * @param f File to process.
   * @return File charset.
   * @throws IOException in case of error.
   */
  public static Charset decode(File f) throws IOException {
    SortedMap<String, Charset> charsets = Charset.availableCharsets();

    String[] firstCharsets = {
      Charset.defaultCharset().name(), "US-ASCII", "UTF-8", "UTF-16BE", "UTF-16LE"
    };

    Collection<Charset> orderedCharsets = U.newLinkedHashSet(charsets.size());

    for (String c : firstCharsets)
      if (charsets.containsKey(c)) orderedCharsets.add(charsets.get(c));

    orderedCharsets.addAll(charsets.values());

    try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
      FileChannel ch = raf.getChannel();

      ByteBuffer buf = ByteBuffer.allocate(4096);

      ch.read(buf);

      buf.flip();

      for (Charset charset : orderedCharsets) {
        CharsetDecoder decoder = charset.newDecoder();

        decoder.reset();

        try {
          decoder.decode(buf);

          return charset;
        } catch (CharacterCodingException ignored) {
        }
      }
    }

    return Charset.defaultCharset();
  }
예제 #16
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Reads and returns a single unsigned 32-bit integer value.
   *
   * @param type TIFF type to read; must be a 32-bit type
   * @param count Unused
   * @param value Offset from which to read
   */
  protected long readLong(int type, long count, long value) throws IOException {
    _raf.seek(value);

    return readUnsigned(type);
  }
예제 #17
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
 /**
  * Reads an array of bytes and returns it as a byte array.
  *
  * @param type Unused
  * @param count Number of bytes to read
  * @param value Offset from which to read
  */
 protected byte[] readTrueByteArray(int type, long count, long value) throws IOException {
   _raf.seek(value);
   byte[] array = new byte[(int) count];
   _raf.read(array);
   return array;
 }
예제 #18
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /**
   * Parse the IFD. Errors are not suppressed.
   *
   * @param byteOffsetIsValid If true, allow offsets on odd byte boundaries
   * @return The offset of the next IFD
   */
  public long parse(boolean byteOffsetIsValid) throws TiffException {
    /* Start at the IFD offset, read the number of entries, then
     * read the entire IFD.
     */
    long offset = _offset;
    _next = 0L;
    byte[] buffer;
    int nFields = 0;
    try {
      _raf.seek(offset);
      nFields = ModuleBase.readUnsignedShort(_raf, _bigEndian);
      offset += 2;

      int len = 12 * nFields;
      buffer = new byte[len];
      _raf.read(buffer, 0, len);

      /* Read the offset of the next IFD (or 0 if none). */
      offset += len;
      _next = ModuleBase.readUnsignedInt(_raf, _bigEndian);
    } catch (Exception e) {
      throw new TiffException("Premature EOF", offset);
    }

    DataInputStream ifdStream = new DataInputStream(new ByteArrayInputStream(buffer));

    try {
      int prevTag = 0;
      for (int i = 0; i < nFields; i++) {
        int tag = ModuleBase.readUnsignedShort(ifdStream, _bigEndian, null);
        /* Tags must be in ascending numerical order. */
        if (!debug_allowoutofsequence && tag < prevTag) {
          _info.setMessage(
              new ErrorMessage("Tag " + tag + " out of sequence", _offset + 2 + 12 * i));
          _info.setWellFormed(false);
        }
        prevTag = tag;

        int type = ModuleBase.readUnsignedShort(ifdStream, _bigEndian, null);
        /* Skip over tags with unknown type. */
        if (type < BYTE || type > IFD) {
          _info.setMessage(
              new ErrorMessage(
                  "Unknown data type", "Type = " + type + ", Tag = " + tag, _offset + 4 + 12 * i));
        } else {
          /* Type gives indication of the TIFF version. */
          if (SBYTE <= type && type <= IFD) {
            _version = 6;
          }

          long count = ModuleBase.readUnsignedInt(ifdStream, _bigEndian, null);
          long value = ModuleBase.readUnsignedInt(ifdStream, _bigEndian, null);
          if (calcValueSize(type, count) > 4) {
            /* Value is the word-aligned offset of the actual
             * value. */
            if ((value & 1) != 0) {
              if (byteOffsetIsValid) {
                _info.setMessage(
                    new InfoMessage(
                        "Value offset not word-aligned: " + value, _offset + 10 + 12 * i));
              } else {
                throw new TiffException(
                    "Value offset not " + "word-aligned: " + value, _offset + 10 + 12 * i);
              }
            }
          } else {
            /* Value is the actual value; pass the offset of
             * the value. */
            value = _offset + 10 + 12 * i;
          }
          lookupTag(tag, type, count, value);
        }
      }
    } catch (IOException e) {
      throw new TiffException("Read error", _offset + 2);
    }
    postParseInitialization();

    return _next;
  }
예제 #19
0
파일: IFD.java 프로젝트: KOST-CECO/SIP-Val
  /** Reads and returns a single unsigned 16-bit value. */
  protected int readShort(int type, long count, long value) throws IOException {
    _raf.seek(value);

    return (int) readUnsigned(type);
  }