/**
  * Marks the read limit of the StringReader.
  *
  * @param limit the maximum limit of bytes that can be read before the mark position becomes
  *     invalid
  */
 public synchronized void mark(final int limit) {
   try {
     in.mark(limit);
   } catch (IOException ioe) {
     throw new RuntimeException(ioe.getMessage());
   }
 }
  private String spejdPost(JsonObject jo) {
    @SuppressWarnings({"resource"})
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpResponse resp;

    HttpPost httpPost = new HttpPost(SPEJD_SERVICE_POST);
    httpPost.addHeader("Content-Type", "application/json");

    StringEntity reqEntity = null;
    try {
      reqEntity = new StringEntity(jo.toString());
    } catch (UnsupportedEncodingException e) {
      System.out.println(e.toString());
    }

    assert reqEntity != null;

    httpPost.setEntity(reqEntity);
    try {
      resp = httpclient.execute(httpPost);
      return EntityUtils.toString(resp.getEntity());
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
Esempio n. 3
0
 public static void main(String[] arguments) {
   try {
     // read byte data into a byte buffer
     String data = "friends.dat";
     FileInputStream inData = new FileInputStream(data);
     FileChannel inChannel = inData.getChannel();
     long inSize = inChannel.size();
     ByteBuffer source = ByteBuffer.allocate((int) inSize);
     inChannel.read(source, 0);
     source.position(0);
     System.out.println("Original byte data:");
     for (int i = 0; source.remaining() > 0; i++) {
       System.out.print(source.get() + " ");
     }
     // convert byte data into character data
     source.position(0);
     Charset ascii = Charset.forName("US-ASCII");
     CharsetDecoder toAscii = ascii.newDecoder();
     CharBuffer destination = toAscii.decode(source);
     destination.position(0);
     System.out.println("\n\nNew character data:");
     for (int i = 0; destination.remaining() > 0; i++) {
       System.out.print(destination.get());
     }
     System.out.println();
   } catch (FileNotFoundException fne) {
     System.out.println(fne.getMessage());
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }
Esempio n. 4
0
 public void run() {
   for (; ; ) {
     try {
       int n = sel.select();
       if (n > 0) processSelectedKeys();
       processPendingTargets();
       if (shutdown) {
         sel.close();
         return;
       }
     } catch (IOException x) {
       x.printStackTrace();
     }
   }
 }
 public void run() {
   while (!shutdown) {
     try {
       registerTargets();
       if (selector.select() > 0) {
         processSelectedKeys();
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   try {
     selector.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 public void receiveTarget() {
   // 接收用户输入的地址,向targets队列中加入任务
   try {
     BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
     String msg = null;
     while ((msg = localReader.readLine()) != null) {
       if (!msg.equals("bye")) {
         Target target = new Target(msg);
         addTarget(target);
       } else {
         shutdown = true;
         selector.wakeup();
         break;
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  public void registerTargets() {
    // 取出targets队列中的任务,向Selector注册连接就绪事件
    synchronized (targets) {
      while (targets.size() > 0) {
        Target target = (Target) targets.removeFirst();

        try {
          target.channel.register(selector, SelectionKey.OP_CONNECT, target);
        } catch (IOException x) {
          try {
            target.channel.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
          target.failure = x;
          addFinishedTarget(target);
        }
      }
    }
  }
Esempio n. 8
0
  /** @param args */
  public static void main(String[] args) {
    FileChannel fc = null;
    RandomAccessFile raf = null;
    // StringBuilder sb;

    if (args.length != 1) {
      System.out.println("Usage: Ntfs filename");
      System.exit(1);
    }
    /*
    sb = new StringBuilder();
    int[] foo = {129,4,229,33};
    for (int b: foo) {
        sb.insert(0,String.format("%02X", b));
    }
    System.out.println(sb.toString());
    System.exit(0);
    */
    try {
      raf = new RandomAccessFile(args[0], "r");
      fc = raf.getChannel();
      Filesystem fs = new Filesystem(fc);

      fs.demo();
      // fs.displayFs();
    } catch (FileNotFoundException x) {
      System.out.println("FNF exp: " + x.getMessage());
    } catch (IOException x) {
      System.out.println("IO exp: " + x.getMessage());
    } finally {
      if (raf != null)
        try {
          raf.close();
        } catch (IOException e) {
          e.printStackTrace(); // To change body of catch statement use File | Settings | File
          // Templates.
        }
    }
  }
Esempio n. 9
0
  @Override
  public void action(final Object comp) {
    final String pth = path();
    final IOFile io = new IOFile(pth);
    String inf = io.isDir() && io.children().length > 0 ? DIR_NOT_EMPTY : null;
    ok = !pth.isEmpty();

    final SerialMethod mth = SerialMethod.valueOf(method.getSelectedItem());
    final OptionsOption<? extends Options> opts =
        mth == SerialMethod.JSON
            ? SerializerOptions.JSON
            : mth == SerialMethod.CSV ? SerializerOptions.CSV : null;
    final boolean showmparams = opts != null;
    mparams.setEnabled(showmparams);

    if (ok) {
      gui.gopts.set(GUIOptions.INPUTPATH, pth);
      try {
        if (comp == method) {
          if (showmparams) {
            final Options mopts = options(null).get(opts);
            mparams.setToolTipText(tooltip(mopts));
            mparams.setText(mopts.toString());
          } else {
            mparams.setToolTipText(null);
            mparams.setText("");
          }
        }
        Serializer.get(new ArrayOutput(), options(mth));
      } catch (final IOException ex) {
        ok = false;
        inf = ex.getMessage();
      }
    }

    info.setText(inf, ok ? Msg.WARN : Msg.ERROR);
    enableOK(buttons, B_OK, ok);
  }
Esempio n. 10
0
 byte[] getByteBodyContents() {
   if (bytePayload != null) return bytePayload;
   String body = null;
   if (payload != null) {
     body = payload;
   } else if (files.isEmpty()) {
     body = URLUtils.formURLEncodeMap(bodyParams);
   } else {
     byte res[] = null;
     try {
       res = URLUtils.doFormDataEncode(bodyParams, files, boundary).toByteArray();
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     return res;
   }
   try {
     return body.getBytes(getCharset());
   } catch (UnsupportedEncodingException uee) {
     throw new OAuthException("Unsupported Charset: " + getCharset(), uee);
   }
 }