Exemplo n.º 1
1
  @Override
  public void run() {
    try {
      // Open socket
      socket = new Socket("127.0.0.1", 5432);
      // Get input streams
      DataOutputStream writer =
          new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
      DataInputStream scanner = new DataInputStream(socket.getInputStream());
      // Send calculation data
      writer.writeDouble(zoom);
      writer.writeDouble(zoomTranslateX);
      writer.writeDouble(zoomTranslateY);
      writer.writeInt(level);
      writer.writeInt(mode);
      writer.flush();
      System.out.println("Sent data");

      // Wait for server to do calculations
      while (scanner.available() == 0) {
        Thread.sleep(level);
      }
      System.out.println("Start drawing");
      // Start reading and drawing
      application.clearKochPanel();
      int counter = 0;
      while (true) {
        if (scanner.available() > 0) {
          application.drawEdge(
              new Edge(
                  scanner.readDouble(),
                  scanner.readDouble(),
                  scanner.readDouble(),
                  scanner.readDouble(),
                  Color.hsb(scanner.readDouble(), 1, 1)));
          counter = 0;
        } else {
          Thread.sleep(10);
          counter++;
          if (counter > 10) {
            break;
          }
        }
      }
      System.out.println("Finished drawing!");

      writer.writeChar('H');
      writer.flush();
      writer.close();
      scanner.close();
      socket.close();
    } catch (Exception e) {
      System.out.println("Receiving edges failed!");
      System.out.println(e.getMessage());
      Thread.currentThread().interrupt();
    }
  }
Exemplo n.º 2
1
  public double saveBinaryFileWithBuffer() {
    double time = System.nanoTime();
    File file = new File("C:\\Users\\rvanduijnhoven\\Documents\\jsfoutput\\binFileWithBuffer.bin");
    DataOutputStream outPut = null;
    DataInputStream inPut = null;
    FileOutputStream fileOut = null;
    FileInputStream fileIn = null;
    BufferedInputStream buffInput = null;
    BufferedOutputStream buffOut = null;
    try {
      fileOut = new FileOutputStream(file);
      buffOut = new BufferedOutputStream(fileOut);
      outPut = new DataOutputStream(buffOut);
      for (Edge e : edges) {
        outPut.writeDouble(e.X1);
        outPut.writeDouble(e.Y1);
        outPut.writeDouble(e.X2);
        outPut.writeDouble(e.Y2);
        outPut.writeDouble(e.color.getRed());
        outPut.writeDouble(e.color.getGreen());
        outPut.writeDouble(e.color.getBlue());
        outPut.flush();
      }
      outPut.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    edges.clear();
    // Now read every edge from the file and draw it.
    try {
      fileIn = new FileInputStream(file);
      buffInput = new BufferedInputStream(fileIn);
      inPut = new DataInputStream(buffInput);

      if (inPut.available() > 0) {
        while (inPut.available() > 0) {
          double X1 = inPut.readDouble();
          double Y1 = inPut.readDouble();
          double X2 = inPut.readDouble();
          double Y2 = inPut.readDouble();
          double red = inPut.readDouble();
          double green = inPut.readDouble();
          double blue = inPut.readDouble();

          Edge e = new Edge(X1, Y1, X2, Y2, new Color(red, green, blue, 1));
          drawEdge(e);
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return System.nanoTime() - time;
  }
Exemplo n.º 3
1
  /**
   * Reads the payload of a tag, given the name and type.
   *
   * @param type The type.
   * @param name The name.
   * @param depth The depth.
   * @return The tag.
   * @throws IOException if an I/O error occurs.
   */
  private Tag readTagPayload(int type, String name, int depth) throws IOException {
    switch (type) {
      case NBTConstants.TYPE_END:
        if (depth == 0) {
          throw new IOException("TAG_End found without a TAG_Compound/TAG_List tag preceding it.");
        } else {
          return new EndTag();
        }
      case NBTConstants.TYPE_BYTE:
        return new ByteTag(name, is.readByte());
      case NBTConstants.TYPE_SHORT:
        return new ShortTag(name, is.readShort());
      case NBTConstants.TYPE_INT:
        return new IntTag(name, is.readInt());
      case NBTConstants.TYPE_LONG:
        return new LongTag(name, is.readLong());
      case NBTConstants.TYPE_FLOAT:
        return new FloatTag(name, is.readFloat());
      case NBTConstants.TYPE_DOUBLE:
        return new DoubleTag(name, is.readDouble());
      case NBTConstants.TYPE_BYTE_ARRAY:
        int length = is.readInt();
        byte[] bytes = new byte[length];
        is.readFully(bytes);
        return new ByteArrayTag(name, bytes);
      case NBTConstants.TYPE_STRING:
        length = is.readShort();
        bytes = new byte[length];
        is.readFully(bytes);
        return new StringTag(name, new String(bytes, NBTConstants.CHARSET));
      case NBTConstants.TYPE_LIST:
        int childType = is.readByte();
        length = is.readInt();

        List<Tag> tagList = new ArrayList<Tag>();
        for (int i = 0; i < length; i++) {
          Tag tag = readTagPayload(childType, "", depth + 1);
          if (tag instanceof EndTag) {
            throw new IOException("TAG_End not permitted in a list.");
          }
          tagList.add(tag);
        }

        return new ListTag(name, NBTUtils.getTypeClass(childType), tagList);
      case NBTConstants.TYPE_COMPOUND:
        Map<String, Tag> tagMap = new HashMap<String, Tag>();
        while (true) {
          Tag tag = readTag(depth + 1);
          if (tag instanceof EndTag) {
            break;
          } else {
            tagMap.put(tag.getName(), tag);
          }
        }

        return new CompoundTag(name, tagMap);
      default:
        throw new IOException("Invalid tag type: " + type + ".");
    }
  }
Exemplo n.º 4
0
  public static void main(String[] args) {

    String inputFileName =
        "C:\\Users\\Ahmed\\Desktop\\RA\\2011-2012\\Term1\\KNN\\BerlinMod Data\\trips.1000.dat";

    FileInputStream fin;
    try {
      fin = new FileInputStream(inputFileName);
      DataInputStream din = new DataInputStream(fin);

      while (true)
        System.out.println(
            din.readInt()
                + ", "
                + din.readLong()
                + ", "
                + din.readDouble()
                + ", "
                + din.readDouble());

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println("End of file reached");
      e.printStackTrace();
    }
  }
Exemplo n.º 5
0
  public RGAPlayer(int index, DataInputStream dis) throws IOException {
    super(index, ChromoTypes.RGA);
    jailLength = JAIL_LENGTH;

    chrNoOwners = new double[lotLength];
    chrPlayerOwns = new double[lotLength];
    chrOpponentOwns = new double[lotLength];
    chrTwoOpponentOwns = new double[lotLength];
    chrJail = new double[jailLength][jailLength];

    addToFitness(dis.readInt());

    for (int i = 0; i < chrNoOwners.length; i++) {
      chrNoOwners[i] = dis.readDouble();
    }
    for (int i = 0; i < chrPlayerOwns.length; i++) {
      chrPlayerOwns[i] = dis.readDouble();
    }
    for (int i = 0; i < chrOpponentOwns.length; i++) {
      chrOpponentOwns[i] = dis.readDouble();
    }
    for (int i = 0; i < chrTwoOpponentOwns.length; i++) {
      chrTwoOpponentOwns[i] = dis.readDouble();
    }

    for (int i = 0; i < jailLength; i++) {
      for (int j = 0; j < jailLength; j++) {
        chrJail[i][j] = dis.readDouble();
      }
    }
  }
Exemplo n.º 6
0
  public static void main(String[] args) throws IOException {
    FileReader in = new FileReader("frog.txt");
    FileWriter out = new FileWriter("prince.txt");

    int c;
    while ((c = in.read()) != -1) out.write(c);

    in.close();
    out.close();

    // Read file name from stdin
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter a valid filename: ");
    String fileName = stdin.readLine();

    DataOutputStream outStream =
        new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
    outStream.writeDouble(3.14);
    outStream.writeUTF("That is pi");
    outStream.writeDouble(1.41413);
    outStream.writeUTF("Square root of 2");
    outStream.close();
    DataInputStream inStream =
        new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
    System.out.println(inStream.readDouble());
    System.out.println(inStream.readUTF());
    System.out.println(inStream.readDouble());
    System.out.println(inStream.readUTF());
  }
Exemplo n.º 7
0
  @Override
  public void handle(SMPPlayer p, Server server, DataInputStream reader) {
    try {
      p.setOldLocation(p.getLocation());
      double x = reader.readDouble();
      double y = reader.readDouble();
      double stance = reader.readDouble();
      double z = reader.readDouble();
      boolean onGround = reader.readBoolean();

      if ((stance - y) < 0.1d || (stance - y) > 1.65d) {
        p.kick("Illegal Stance");
      }

      if ((Math.abs(x) >= Math.abs(p.getLocation().getX()) + 100)
          || // TODO: possibly add customization for this number and an option for if it will be
             // checked at all
          (Math.abs(y) >= Math.abs(p.getLocation().getY()) + 100)
          || (Math.abs(z) >= Math.abs(p.getLocation().getZ()) + 100)) {
        p.kick("You moved too quickly");
      }

      if (Math.abs(x) > 3.2E7d || Math.abs(y) > 3.2E7d) {
        p.kick("Illegal Position");
      }
      p.getLocation().setX(x);
      p.getLocation().setY(y);
      p.getLocation().setZ(z);
      p.setStance(stance);
      p.setOnGround(onGround);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 8
0
  public Response sendRequest(Request request) throws IOException {
    byte[] requestData = request.getBytes();
    dataOutputStream.writeInt(requestData.length);
    dataOutputStream.write(requestData);
    dataOutputStream.flush();

    byte responseType = dataInputStream.readByte();
    switch (responseType) {
      case Response.TYPE_SENT_OK:
        return new SendMessageResponse(dataInputStream.readDouble());
      case Response.TYPE_SENT_NOT_OK:
        return new SendMessageResponse(dataInputStream.readDouble());
      case Response.TYPE_EMPTY:
        return new EmptyResponse();
      case Response.TYPE_MESSAGE:
        return new MessageResponse(
            dataInputStream.readInt(),
            dataInputStream.readInt(),
            dataInputStream.readUTF(),
            dataInputStream.readDouble());
      case Response.TYPE_QUEUE_CREATED:
        return new CreateQueueResponse(dataInputStream.readInt());
      case Response.TYPE_QUEUE_DELETED:
        return new DeleteQueueResponse(dataInputStream.readBoolean());
      case Response.TYPE_QUEUE_QUERY:
        ArrayList<Integer> queues = new ArrayList<Integer>();
        int size = dataInputStream.readInt();
        for (int i = 0; i < size; ++i) {
          queues.add(dataInputStream.readInt());
        }
        return new QueryQueuesResponse(queues);
      default:
        return new InvalidRequestResponse();
    }
  }
Exemplo n.º 9
0
  public void load(DataInputStream in, String name, World w) {
    this.name = name;
    try {
      world = w;
      try {
        double x1 = in.readDouble();
        double y1 = in.readDouble();
        double z1 = in.readDouble();
        catArena.setLocB(world, x1, y1, z1);

        double x2 = in.readDouble();
        double y2 = in.readDouble();
        double z2 = in.readDouble();
        catArena.setLocG(world, x2, y2, z2);

        while (true) {

          int d = in.readInt();
          if (d == Material.CHEST.getId()) {
            blocks.add(new BlocksChest(in, world));
          } else if (d == Material.SIGN.getId() || d == Material.SIGN_POST.getId()) {
            blocks.add(new BlocksSign(in, world));

          } else {
            blocks.add(new Blocks(in, world));
          }
        }
      } catch (EOFException d) {

      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 10
0
  protected void read(DataInputStream s) {
    try {
      ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type t = Type.valueOf(s.readUTF());
      if (t == Type.DOUBLE) {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // double -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // double -> double
            doubleData = new double[length()];
          } else {
            // double -> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        }
      } else {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // float -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // float -> double
            doubleData = new double[length()];
          } else {
            // float-> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 11
0
  public static Edge parse(DataInputStream dis) throws IOException {
    double x1 = dis.readDouble();
    double y1 = dis.readDouble();
    double x2 = dis.readDouble();
    double y2 = dis.readDouble();

    double red = dis.readDouble();
    double green = dis.readDouble();
    double blue = dis.readDouble();

    return new Edge(x1, y1, x2, y2, new Color(red, green, blue, 1));
  }
Exemplo n.º 12
0
  private static Unit readUnitV3(DataInputStream dis) throws IOException {
    int length = dis.readInt();
    String name = "";
    for (int i = length - 1; i >= 0; i--) {
      name += dis.readChar();
    }
    String weapon = "";
    length = dis.readInt();
    for (int i = length - 1; i >= 0; i--) {
      weapon += dis.readChar();
    }
    double life = dis.readDouble();
    double movement = dis.readDouble();

    double energyCost = dis.readDouble();
    double metalCost = dis.readDouble();
    double energyDrain = dis.readDouble();
    double metalDrain = dis.readDouble();

    double width = dis.readDouble();
    double height = dis.readDouble();
    double depth = dis.readDouble();

    int buildTime = dis.readInt();

    ArrayList<String> buildTree = new ArrayList<String>();
    length = dis.readInt();
    for (int i = 0; i < length; i++) {
      int tempLength = dis.readInt();
      String tempName = "";
      for (int a = 0; a < tempLength; a++) {
        tempName += dis.readChar();
      }
      buildTree.add(tempName);
    }

    Weapon w = EngineConstants.weaponFactory.makeWeapon(weapon);
    Unit u =
        new Unit(
            name,
            w,
            life,
            movement,
            energyCost,
            metalCost,
            energyDrain,
            metalDrain,
            0,
            0,
            width,
            height,
            depth,
            buildTime);
    u.setBuildTree(buildTree);
    // System.out.println(name+" build tree = "+buildTree);
    return u;
  }
  @DeployableTestMethod
  @Test(timeout = 300000)
  public void testFileReadAndWriteBackWithDataOutputStreamAndDeferredBufferedReaderCreation()
      throws IOException {
    Random rng = new Random();
    String testString = "This string tests readLine";
    double testDouble = rng.nextDouble();
    int testInteger = rng.nextInt();
    File testFile = new File(TEST_DIRECTORY + "shortReadWriteTestFile.txt");

    DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(testFile));
    outputStream.writeDouble(testDouble);
    outputStream.writeInt(testInteger);
    outputStream.writeBytes(testString + "\n");
    outputStream.close();

    DataInputStream inputStream = new DataInputStream(new FileInputStream(testFile));
    double doubleReadBack = inputStream.readDouble();
    int integerReadBack = inputStream.readInt();

    BufferedReader bufferedReader =
        new BufferedReader(new InputStreamReader(inputStream, "US-ASCII"));
    String lineReadBack = bufferedReader.readLine();

    inputStream.close();
    bufferedReader.close();

    System.out.println(lineReadBack);
    System.out.println(testString);

    assertTrue(testDouble == doubleReadBack);
    assertTrue(testString.equals(lineReadBack));
    assertTrue(testInteger == integerReadBack);
  }
  @SuppressWarnings("deprecation")
  @DeployableTestMethod
  @Test(timeout = 300000)
  public void testFileReadAndWriteWithDataOutputStreamAndDataInputStream()
      throws IOException, FileNotFoundException, NullPointerException {
    Random rng = new Random();
    String testString = "This string tests readLine";
    double testDouble = rng.nextDouble();
    int testInteger = rng.nextInt();
    File testFile = new File(TEST_DIRECTORY + "shortReadWriteTestFile.txt");

    DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(testFile));
    outputStream.writeDouble(testDouble);
    outputStream.writeBytes(testString + "\n");
    outputStream.writeInt(testInteger);
    outputStream.close();

    DataInputStream inputStream = new DataInputStream(new FileInputStream(testFile));
    double doubleReadBack = inputStream.readDouble();
    String lineReadBack = inputStream.readLine();
    int integerReadBack = inputStream.readInt();

    assertTrue(testDouble == doubleReadBack);
    assertTrue(testString.equals(lineReadBack));
    assertTrue(testInteger == integerReadBack);
  }
Exemplo n.º 15
0
 /**
  * Reads a double value from the file.
  *
  * @return
  */
 public double readDouble() {
   try {
     return m_reader.readDouble();
   } catch (IOException e) {
     return 0;
   }
 }
Exemplo n.º 16
0
  /**
   * Load ensemble from file_name
   *
   * @param file_name File name
   */
  public void LoadEnsemble(String file_name) {
    String name;

    // Load networks
    for (int i = 0; i < Nnetworks; i++) {
      name = file_name + "_net_" + i;
      nets[i].LoadNetwork(name);
    }

    // Load weigths
    try {
      FileInputStream file = new FileInputStream(file_name);
      DataInputStream dataIn = new DataInputStream(file);

      // Load weights
      for (int i = 0; i < Noutputs; i++) {
        for (int j = 0; j < Nnetworks; j++) {
          weights[i][j] = dataIn.readDouble();
        }
      }

      dataIn.close();
    } catch (FileNotFoundException ex) {
      System.err.println("Unable to open ensemble files");
      System.exit(1);
    } catch (IOException ex) {
      System.err.println("IO exception");
      System.exit(1);
    }
  }
Exemplo n.º 17
0
  private static double[] byteToDouble(byte[] data) throws IOException {

    int len = data.length;

    if (len % WORDSIZE != 0) {
      throw new IOException("Array length is not divisible by wordsize");
    }

    int size = len / WORDSIZE;
    double[] result = new double[size];

    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));

    try {

      int ii = 0;
      while (inputStream.available() > 0) {
        result[ii] = inputStream.readDouble();
        ii++;
      }

    } catch (EOFException e) {
      throw new IOException("Unable to read from dataInputStream, found EOF");

    } catch (IOException e) {
      throw new IOException("Unable to read from dataInputStream, IO error");
    }

    return result;
  }
Exemplo n.º 18
0
  private static void dataStreams() throws IOException {
    DataOutputStream out =
        new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
    for (int i = 0; i < prices.length; i++) {
      out.writeDouble(prices[i]);
      out.writeInt(units[i]);
      out.writeUTF(descs[i]);
    }

    DataInputStream in =
        new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

    double price;
    int unit;
    String desc;
    double total = 0.0;
    try {
      while (true) {
        price = in.readDouble();
        unit = in.readInt();
        desc = in.readUTF();
        System.out.format("You ordered %d" + " units of %s at $%.2f%n", unit, desc, price);
        total += unit * price;
      }
    } catch (EOFException e) {
    }
  }
Exemplo n.º 19
0
  public static void main(String[] args) throws IOException {
    // Create a FileOutputStream.
    FileOutputStream outputFile = new FileOutputStream("primitives.data");

    // Create a DataOutputStream which is chained to the FileOutputStream.
    DataOutputStream outputStream = new DataOutputStream(outputFile);

    // Write Java primitive values in binary representation:
    outputStream.writeBoolean(true);
    outputStream.writeChar('A'); // int written as Unicode char
    outputStream.writeByte(Byte.MAX_VALUE); // int written as 8-bits byte
    outputStream.writeShort(Short.MIN_VALUE); // int written as 16-bits short
    outputStream.writeInt(Integer.MAX_VALUE);
    outputStream.writeLong(Long.MIN_VALUE);
    outputStream.writeFloat(Float.MAX_VALUE);
    outputStream.writeDouble(Math.PI);

    // Close the output stream, which also closes the underlying stream.
    outputStream.flush();
    outputStream.close();

    // Create a FileInputStream.
    FileInputStream inputFile = new FileInputStream("primitives.data");

    // Create a DataInputStream which is chained to the FileInputStream.
    DataInputStream inputStream = new DataInputStream(inputFile);

    // Read the binary representation of Java primitive values
    // in the same order they were written out:
    boolean v = inputStream.readBoolean();
    char c = inputStream.readChar();
    byte b = inputStream.readByte();
    short s = inputStream.readShort();
    int i = inputStream.readInt();
    long l = inputStream.readLong();
    float f = inputStream.readFloat();
    double d = inputStream.readDouble();

    // Check for end of stream:
    try {
      int value = inputStream.readByte();
      System.out.println("More input: " + value);
    } catch (EOFException eofe) {
      System.out.println("End of stream");
    } finally {
      // Close the input stream, which also closes the underlying stream.
      inputStream.close();
    }

    // Write the values read to the standard input stream:
    System.out.println("Values read:");
    System.out.println(v);
    System.out.println(c);
    System.out.println(b);
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(f);
    System.out.println(d);
  }
Exemplo n.º 20
0
  public static void main(String[] args) throws IOException {
    DataInputStream in = null;
    DataOutputStream out = null;
    try {

      out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

      for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);
        out.writeInt(units[i]);
        out.writeUTF(desks[i]);
      }

      out.close();

      in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
      while (true) {
        double price = in.readDouble();
        int unit = in.readInt();
        String desc = in.readUTF();
        System.out.format("You ordered %d units of %s $%.2f%n", unit, desc, price);
        double total = unit * price;
      }

    } catch (EOFException e) {
      System.out.println("Reached end of file");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
Exemplo n.º 21
0
  public static void main(String[] args) throws IOException {
    try (DataOutputStream dout = new DataOutputStream(new FileOutputStream("Test.dat"))) {
      dout.writeDouble(98.6);
      dout.writeInt(123);
      dout.writeBoolean(true);
    } catch (FileNotFoundException e) {
      System.out.println("Cannot Open Output");
      return;
    } catch (IOException e) {
      System.out.println("I/O Ero " + e);
    }

    try (DataInputStream din = new DataInputStream(new FileInputStream("Test.dat"))) {
      double d = din.readDouble();

      int i = din.readInt();
      boolean b = din.readBoolean();
      System.out.println("Gere are the values " + b + " " + i + " " + d);
    } catch (FileNotFoundException e) {
      System.out.println("Cannot Open Output");
      return;
    } catch (IOException e) {
      System.out.println("I/O Ero " + e);
    }
  }
Exemplo n.º 22
0
  private static void getMedicineList(Socket socket) {
    int medicineID;
    String medicineName;
    String medicineUnit;
    double price;

    try {
      OutputStream output = socket.getOutputStream();
      DataOutputStream dos = new DataOutputStream(output);
      InputStream input = socket.getInputStream();
      DataInputStream dis = new DataInputStream(input);

      dos.writeInt(8);

      int numberOfMedicine = dis.readInt();

      for (int i = 0; i < numberOfMedicine; i++) {
        medicineID = dis.readInt();
        medicineName = dis.readUTF();
        medicineUnit = dis.readUTF();
        price = dis.readDouble();

        medicineList.add(new Medicine(medicineID, medicineName, medicineUnit, price));
      }

    } catch (IOException e) {
      System.out.println("Cannot get IO streams.");
    }
  }
Exemplo n.º 23
0
  @Override
  public void run() {
    while (true)
      try {
        switch (inputStream.readUTF()) {
          case "GetAllEdges":
            level = inputStream.readInt();
            GetAllEdges();
            break;

          case "GetEdges":
            level = inputStream.readInt();
            GetEdges();
            break;

          case "ChangeZoomLevel":
            zoom = inputStream.readDouble();
            offset = (kpSize - zoom) / 2;
            GetEdges();
            break;
        }
      } catch (IOException e) {
        e.printStackTrace();
        break;
      }
  }
  /**
   * See if there are array elements.
   *
   * @param dbl0 Value of the first (maybe only) array element
   * @param result ResultSet for the sample table with blob
   * @return Array with given element and maybe more.
   * @throws Exception on error, including 'cancel'
   */
  private double[] readBlobArrayElements(final double dbl0, final ResultSet result)
      throws Exception {
    final String datatype;
    if (reader.isOracle()) datatype = result.getString(7);
    else datatype = result.getString(8);

    // ' ' or NULL indicate: Scalar, not an array
    if (datatype == null || " ".equals(datatype) || result.wasNull()) return new double[] {dbl0};

    // Decode BLOB
    final byte[] bytes = result.getBytes(reader.isOracle() ? 8 : 9);
    final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    final DataInputStream data = new DataInputStream(stream);
    if ("d".equals(datatype)) { // Read Double typed array elements
      final int nelm = data.readInt();
      final double[] array = new double[nelm];
      for (int i = 0; i < nelm; i++) array[i] = data.readDouble();
      data.close();
      return array;
    }
    // TODO Decode 'l' Long and 'i' Integer?
    else {
      throw new Exception("Sample BLOBs of type '" + datatype + "' are not decoded");
    }
  }
Exemplo n.º 25
0
  public static void main(String[] args) throws IOException {
    // open file output stream
    File outFile = new File("data");
    FileOutputStream outStream = new FileOutputStream(outFile);
    DataOutputStream outDataStream = new DataOutputStream(outStream);

    // write an int, boolean, double
    outDataStream.writeInt(44);
    outDataStream.writeBoolean(true);
    outDataStream.writeDouble(7.2);

    outDataStream.close();

    // open file input stream
    File inFile = new File("data");
    FileInputStream inStream = new FileInputStream(inFile);
    DataInputStream inDataStream = new DataInputStream(inStream);

    // read an int, boolean, double
    int n = inDataStream.readInt();
    boolean b = inDataStream.readBoolean();
    double d = inDataStream.readDouble();

    inDataStream.close();

    // what did we get?
    System.out.println("n = " + n);
    System.out.println("b = " + b);
    System.out.println("d = " + d);

    // can we add n to d?
    System.out.println("n + d = " + (n + d));
  }
Exemplo n.º 26
0
 private double readDouble() {
   try {
     return inStream.readDouble();
   } catch (IOException e) {
     IOError();
   }
   return 0;
 }
Exemplo n.º 27
0
 public Object read(DataInputStream in) throws IOException {
   nRows = in.readInt();
   nCols = in.readInt();
   items = new double[nRows][nCols];
   for (int i = 0; i < items.length; i++)
     for (int j = 0; j < items[i].length; j++) items[i][j] = in.readDouble();
   return this;
 }
Exemplo n.º 28
0
 @Override
 public double readDouble() throws IOException {
   if (bigEndian()) {
     return dataInput.readDouble();
   } else {
     return Double.longBitsToDouble(readLong());
   }
 }
Exemplo n.º 29
0
  public static final BinaryIOClass readFromFile(String filename) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(filename));
    int integer = dis.readInt();
    double decimal = dis.readDouble();
    char character = dis.readChar();

    return new BinaryIOClass(integer, decimal, character);
  }
 @Override
 public MeanInts.CountSum decode(InputStream inStream, Context context)
     throws CoderException, IOException {
   DataInputStream dataStream = new DataInputStream(inStream);
   long count = dataStream.readLong();
   double sum = dataStream.readDouble();
   return (new MeanInts()).new CountSum(count, sum);
 }