Exemplo n.º 1
0
 public static FileInputStream resetInput(FileInputStream stream, File in, long toPoint)
     throws IOException {
   stream.close();
   FileInputStream ret = new FileInputStream(in);
   ret.skip(toPoint);
   return ret;
 }
 /** 准备更新一个新的xml */
 public void init4updateXml() {
   try {
     fis = new FileInputStream(fullPath);
     fis.skip(headBytes.getBytes().length);
     builder = new SAXBuilder();
     doc = builder.build(fis);
     root = doc.getRootElement();
     nodes = root.getChild("nodes");
   } catch (Exception e) {
     e.printStackTrace();
     SysLogger.error("Error in XmlOperator.init4updateXml(),file=" + fullPath);
   }
 }
Exemplo n.º 3
0
 private void calculateDigest() {
   assert headSize.get() == headLength : "Head hasn't catched up, can't calculate digest";
   try {
     FileInputStream stream = new FileInputStream(file);
     stream.skip(headLength);
     byte[] buffer = new byte[4096];
     int bytesRead;
     while ((bytesRead = stream.read(buffer, 0, 4096)) > 0) {
       md.update(buffer, 0, bytesRead);
     }
     stream.close();
   } catch (IOException ex) {
     logger.error("error accessing file to calculate digest", ex);
   }
 }
Exemplo n.º 4
0
 /**
  * Skips at most {@code n} bytes in this stream. This method does nothing and returns 0 if {@code
  * n} is negative, but some subclasses may throw.
  *
  * <p>Note the "at most" in the description of this method: this method may choose to skip fewer
  * bytes than requested. Callers should <i>always</i> check the return value.
  *
  * <p>This default implementation reads bytes into a temporary buffer. Concrete subclasses should
  * provide their own implementation.
  *
  * @param byteCount the number of bytes to skip.
  * @return the number of bytes actually skipped.
  * @throws IOException if this stream is closed or another IOException occurs.
  */
 @Override
 public long skip(long byteCount) throws IOException {
   return fis.skip(byteCount);
 }
Exemplo n.º 5
0
  /**
   * Serves file from homeDir and its' subdirectories (only). Uses only URI, ignores all headers and
   * HTTP parameters.
   */
  Response serveFile(String uri, Map<String, String> header, File file, String mime) {
    Response res;
    try {
      // Calculate etag
      String etag =
          Integer.toHexString(
              (file.getAbsolutePath() + file.lastModified() + "" + file.length()).hashCode());

      // Support (simple) skipping:
      long startFrom = 0;
      long endAt = -1;
      String range = header.get("range");
      if (range != null) {
        if (range.startsWith("bytes=")) {
          range = range.substring("bytes=".length());
          int minus = range.indexOf('-');
          try {
            if (minus > 0) {
              startFrom = Long.parseLong(range.substring(0, minus));
              endAt = Long.parseLong(range.substring(minus + 1));
            }
          } catch (NumberFormatException ignored) {
          }
        }
      }

      // Change return code and add Content-Range header when skipping is
      // requested
      long fileLen = file.length();
      if (range != null && startFrom >= 0) {
        if (startFrom >= fileLen) {
          res = createResponse(Response.Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, "");
          res.addHeader("Content-Range", "bytes 0-0/" + fileLen);
          res.addHeader("ETag", etag);
        } else {
          if (endAt < 0) {
            endAt = fileLen - 1;
          }
          long newLen = endAt - startFrom + 1;
          if (newLen < 0) {
            newLen = 0;
          }

          final long dataLen = newLen;
          FileInputStream fis =
              new FileInputStream(file) {
                @Override
                public int available() throws IOException {
                  return (int) dataLen;
                }
              };
          fis.skip(startFrom);

          res = createResponse(Response.Status.PARTIAL_CONTENT, mime, fis);
          res.addHeader("Content-Length", "" + dataLen);
          res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen);
          res.addHeader("ETag", etag);
        }
      } else {
        if (etag.equals(header.get("if-none-match")))
          res = createResponse(Response.Status.NOT_MODIFIED, mime, "");
        else {
          res = createResponse(Response.Status.OK, mime, new FileInputStream(file));
          res.addHeader("Content-Length", "" + fileLen);
          res.addHeader("ETag", etag);
        }
      }
    } catch (IOException ioe) {
      res =
          createResponse(
              Response.Status.FORBIDDEN,
              NanoHTTPD.MIME_PLAINTEXT,
              "FORBIDDEN: Reading file failed.");
    }

    return res;
  }
  /** @author nielin modify at 2010-01-08 */
  public int writeXml(int flag) {
    int done = 0;
    // 需要做分布式判断
    String runmodel = PollingEngine.getCollectwebflag();
    try {
      I_HostLastCollectData hostlastmanager = new HostLastCollectDataManager();
      Hashtable ipAllData = (Hashtable) ShareData.getSharedata().get(ipaddress);

      Vector vector = new Vector();
      String[] netInterfaceItem = {
        "index",
        "ifDescr",
        "ifSpeed",
        "ifAdminStatus",
        "ifOperStatus",
        "OutBandwidthUtilHdx",
        "InBandwidthUtilHdx"
      };
      try {
        if ("0".equals(runmodel)) {
          // 采集与访问是集成模式
          vector = hostlastmanager.getInterface_share(ipaddress, netInterfaceItem, "index", "", "");
        } else {
          // 采集与访问是分离模式
          vector = hostlastmanager.getInterface(ipaddress, netInterfaceItem, "index", "", "");
        }
      } catch (Exception e) {
        e.printStackTrace();
        return done;
      }
      try {
        _fis =
            new FileInputStream(
                ResourceCenter.getInstance().getSysPath()
                    + "panel/model/"
                    + oid
                    + "_"
                    + imageType
                    + ".jsp");
      } catch (Exception e) {
        e.printStackTrace();
        _fis.close();
        return done;
      }
      _fis.skip(headBytes.getBytes().length);
      _builder = new SAXBuilder();
      _doc = _builder.build(_fis);
      _root = _doc.getRootElement();
      _nodes = _root.getChild("nodes");

      List list = _nodes.getChildren();

      PortconfigDao dao = new PortconfigDao();

      try {

        for (int i = 0; i < list.size(); i++) {
          Element eleNode = (Element) list.get(i);
          if (eleNode.getChildText("index") == null) break;
          int index = Integer.valueOf(eleNode.getChildText("index")).intValue();
          String x = eleNode.getChildText("x");
          String y = eleNode.getChildText("y");
          String img = eleNode.getChildText("img");
          String direction = eleNode.getChildText("direction");
          String ifname = "";
          String OutBandwidthUtilHdx = "0";
          String InBandwidthUtilHdx = "0";
          String portuse = "";
          if (vector != null && vector.size() > 0) {
            for (int m = 0; m < vector.size(); m++) {
              String[] strs = (String[]) vector.get(m);
              String _ifname = strs[1];
              String _index = strs[0];
              String opstatus = strs[4];
              OutBandwidthUtilHdx = strs[5];
              InBandwidthUtilHdx = strs[6];

              Portconfig portconfig = null;
              try {
                portconfig = dao.getPanelByipandindex(ipaddress, index + "");
              } catch (Exception e) {

              }
              if (portconfig != null
                  && portconfig.getLinkuse() != null
                  && portconfig.getLinkuse().trim().length() > 0) {
                portuse = portconfig.getLinkuse();
              }

              // SysLogger.info(ipaddress+"====ifOperStatus====="+strs[4]);
              if (Integer.parseInt(_index) == index) {
                ifname = _ifname;
                if ("down".equalsIgnoreCase(opstatus)) {
                  // DOWN
                  if ("1".equalsIgnoreCase(direction)) {
                    // 向上的端口
                    img = "image/up_down_gray.gif";
                  } else {
                    // 向下的端口
                    img = "image/down_down_gray.gif";
                  }
                } else {
                  // UP
                  if ("1".equalsIgnoreCase(direction)) {
                    // 向上的端口
                    img = "image/up_up_green.gif";
                  } else {
                    // 向下的端口
                    img = "image/down_up_green.gif";
                  }
                }
                break;
              }
            }
          }
          addNode(
              index + "",
              InBandwidthUtilHdx,
              OutBandwidthUtilHdx,
              img,
              ipaddress,
              ifname,
              portuse,
              x,
              y);
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        dao.close();
      }

      Format format = Format.getCompactFormat();
      format.setEncoding("GB2312");
      format.setIndent("	");
      serializer = new XMLOutputter(format);
      // SysLogger.info("path==="+fullPath);
      fos = new FileOutputStream(fullPath);
      fos.write(headBytes.getBytes());
      serializer.output(doc, fos);
      fos.close();
      done = 1;
    } catch (Exception e) {
      done = 0;
      e.printStackTrace();
      SysLogger.error("Error in XmlOperator.close()", e);
    }
    return done;
  }