Package freenet.support.io

Examples of freenet.support.io.LineReadingInputStream


    handler.closedInput();
  }

  public void realRun() throws IOException {
    InputStream is = new BufferedInputStream(handler.sock.getInputStream(), 4096);
    LineReadingInputStream lis = new LineReadingInputStream(is);

    boolean firstMessage = true;

    while(true) {
      SimpleFieldSet fs;
      if(WrapperManager.hasShutdownHookBeenTriggered()) {
        FCPMessage msg = new ProtocolErrorMessage(ProtocolErrorMessage.SHUTTING_DOWN,true,"The node is shutting down","Node",false);
        handler.outputHandler.queue(msg);
        Closer.close(is);
        return;
      }
      // Read a message
      String messageType = lis.readLine(128, 128, true);
      if(messageType == null) {
        Closer.close(is);
        return;
      }
      if(messageType.equals(""))
View Full Code Here


   * Note that if this is application/x-www-form-urlencoded, it will come out as
   * params, whereas if it is multipart/form-data it will be separated into buckets.
   */
  private void parseMultiPartData() throws IOException {
    InputStream is = null;
    LineReadingInputStream lis = null;
    OutputStream bucketos = null;

    try {
      if(data == null)
        return;
      String ctype = this.headers.get("content-type");
      if(ctype == null)
        return;
      if(logMINOR)
        Logger.minor(this, "Uploaded content-type: " + ctype);
      String[] ctypeparts = ctype.split(";");
      if(ctypeparts[0].equalsIgnoreCase("application/x-www-form-urlencoded")) {
        // Completely different encoding, but easy to handle
        if(data.size() > 1024 * 1024)
          throw new IOException("Too big");
        byte[] buf = BucketTools.toByteArray(data);
        String s = new String(buf, "us-ascii");
        parseRequestParameters(s, true, true);
      }
      if(!ctypeparts[0].trim().equalsIgnoreCase("multipart/form-data") || (ctypeparts.length < 2))
        return;

      String boundary = null;
      for(String ctypepart: ctypeparts) {
        String[] subparts = ctypepart.split("=");
        if((subparts.length == 2) && subparts[0].trim().equalsIgnoreCase("boundary"))
          boundary = subparts[1];
      }

      if((boundary == null) || (boundary.length() == 0))
        return;
      if(boundary.charAt(0) == '"')
        boundary = boundary.substring(1);
      if(boundary.charAt(boundary.length() - 1) == '"')
        boundary = boundary.substring(0, boundary.length() - 1);

      boundary = "--" + boundary;

      if(logMINOR)
        Logger.minor(this, "Boundary is: " + boundary);

      is = this.data.getInputStream();
      lis = new LineReadingInputStream(is);

      String line;
      line = lis.readLine(100, 100, false); // really it's US-ASCII, but ISO-8859-1 is close enough.
      while((is.available() > 0) && !line.equals(boundary)) {
        line = lis.readLine(100, 100, false);
      }

      boundary = "\r\n" + boundary;

      RandomAccessBucket filedata = null;
      String name = null;
      String filename = null;
      String contentType = null;

      while(is.available() > 0) {
        name = null;
        filename = null;
        contentType = null;
        // chomp headers
        while((line = lis.readLine(200, 200, true)) /* should be UTF-8 as we told the browser to send UTF-8 */ != null) {
          if(line.length() == 0)
            break;

          String[] lineparts = line.split(":");
          if(lineparts == null || lineparts.length == 0)
View Full Code Here

   */
  public static void handle(Socket sock, ToadletContainer container, PageMaker pageMaker, UserAlertManager userAlertManager, BookmarkManager bookmarkManager) {
    try {
      InputStream is = new BufferedInputStream(sock.getInputStream(), 4096);
     
      LineReadingInputStream lis = new LineReadingInputStream(is);
     
      while(true) {
       
        String firstLine = lis.readLine(32768, 128, false); // ISO-8859-1 or US-ASCII, _not_ UTF-8
        if (firstLine == null) {
          sock.close();
          return;
        } else if (firstLine.equals("")) {
          continue;
        }
       
        if(logMINOR)
          Logger.minor(ToadletContextImpl.class, "first line: "+firstLine);
       
        String[] split = firstLine.split(" ");
       
        if(split.length != 3)
          throw new ParseException("Could not parse request line (split.length="+split.length+"): "+firstLine, -1);
       
        if(!split[2].startsWith("HTTP/1."))
          throw new ParseException("Unrecognized protocol "+split[2], -1);
       
        URI uri;
        try {
          uri = URIPreEncoder.encodeURI(split[1]).normalize();
          if(logMINOR) Logger.minor(ToadletContextImpl.class, "URI: "+uri+" path "+uri.getPath()+" host "+uri.getHost()+" frag "+uri.getFragment()+" port "+uri.getPort()+" query "+uri.getQuery()+" scheme "+uri.getScheme());
        } catch (URISyntaxException e) {
          sendURIParseError(sock.getOutputStream(), true, e);
          return;
        }
        String method = split[0];
       
        MultiValueTable<String,String> headers = new MultiValueTable<String,String>();
       
        while(true) {
          String line = lis.readLine(32768, 128, false); // ISO-8859 or US-ASCII, not UTF-8
          if (line == null) {
            sock.close();
            return;
          }
          //System.out.println("Length="+line.length()+": "+line);
View Full Code Here

    public static void maybeDisplayWrapperLogfile(ToadletContext ctx, HTMLNode contentNode) {
        final File logs = new File("wrapper.log");
        long logSize = logs.length();
        if(logs.exists() && logs.isFile() && logs.canRead() && (logSize > 0)) {
            HTMLNode logInfoboxContent = ctx.getPageMaker().getInfobox("infobox-info", "Current status", contentNode, "start-progress", true);
            LineReadingInputStream logreader = null;
            try {
                logreader = FileUtil.getLogTailReader(logs, 2000);
              String line;
              while ((line = logreader.readLine(100000, 200, true)) != null) {
                  logInfoboxContent.addChild("#", line);
                  logInfoboxContent.addChild("br");
              }
            } catch(IOException e) {}
            finally {
View Full Code Here

     * @param byteLimit The maximum number of bytes to read
     * @return The trailing portion of the file
     * @throws IOException if an I/O error occurs
     */
    private static String readLogTail(File logfile, long byteLimit) throws IOException {
        LineReadingInputStream stream = null;
        try {
            stream = FileUtil.getLogTailReader(logfile, byteLimit);
            return FileUtil.readUTF(stream).toString();
        } finally {
            Closer.close(stream);
View Full Code Here

        InetAddress localhost = InetAddress.getByName("127.0.0.1");
        Socket sock = new Socket(localhost, 9481);
        OutputStream sockOS = sock.getOutputStream();
        InputStream sockIS = sock.getInputStream();
        System.out.println("Connected to node.");
        LineReadingInputStream lis = new LineReadingInputStream(sockIS);
        OutputStreamWriter osw = new OutputStreamWriter(sockOS, "UTF-8");
        osw.write("ClientHello\nExpectedVersion=0.7\nName=BootstrapPullTest-"+System.currentTimeMillis()+"\nEnd\n");
        osw.flush();
         String name = lis.readLine(65536, 128, true);
         SimpleFieldSet fs = new SimpleFieldSet(lis, 65536, 128, true, false, true);
         if(!name.equals("NodeHello")) {
           System.err.println("No NodeHello from insertor node!");
           System.exit(EXIT_INSERTER_PROBLEM);
         }
         System.out.println("Connected to "+sock);
         osw.write("ClientPut\nIdentifier=test-insert\nURI=CHK@\nVerbosity=1023\nUploadFrom=direct\nMaxRetries=-1\nDataLength="+TEST_SIZE+"\nData\n");
         osw.flush();
         InputStream is = new FileInputStream(dataFile);
         FileUtil.copy(is, sockOS, TEST_SIZE);
         System.out.println("Sent data");
         while(true) {
             name = lis.readLine(65536, 128, true);
             fs = new SimpleFieldSet(lis, 65536, 128, true, false, true);
           System.out.println("Got FCP message: \n"+name);
           System.out.print(fs.toOrderedString());
           if(name.equals("ProtocolError")) {
             System.err.println("Protocol error when inserting data.");
View Full Code Here

    try{
      fcpSocket = new Socket("127.0.0.1", FCPServer.DEFAULT_FCP_PORT);
      fcpSocket.setSoTimeout(2000);

      InputStream is = fcpSocket.getInputStream();
      LineReadingInputStream lis = new LineReadingInputStream(is);
      OutputStream os = fcpSocket.getOutputStream();

      try{
        sfs.putSingle("Name", "AddRef");
        sfs.putSingle("ExpectedVersion", "2.0");
        fcpm = FCPMessage.create("ClientHello", sfs);
        fcpm.send(os);
        os.flush();

        String messageName = lis.readLine(128, 128, true);
        sfs = getMessage(lis);
        fcpm = FCPMessage.create(messageName, sfs);
        if((fcpm == null) || !(fcpm instanceof NodeHelloMessage)){
          System.err.println("Not a valid FRED node!");
          System.exit(1);
        }
      } catch(MessageInvalidException me){
        me.printStackTrace();
      }
     
      try{
        sfs = SimpleFieldSet.readFrom(reference, false, true);
        fcpm = FCPMessage.create(AddPeer.NAME, sfs);
        fcpm.send(os);
        os.flush();

        // TODO: We ought to do stricter checking!
        // FIXME: some checks even
      } catch(MessageInvalidException me){
        System.err.println("Invalid reference file!"+me);
        me.printStackTrace();
      }

      lis.close();
      is.close();
      os.close();
      fcpSocket.close();
      System.out.println("That reference has been added");
    }catch (SocketException se){
View Full Code Here

   * @throws IOException */
  private static SimpleFieldSet initialLoad(File toRead) throws IOException {
    if(toRead == null) return null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    LineReadingInputStream lis = null;
    try {
      fis = new FileInputStream(toRead);
      bis = new BufferedInputStream(fis);
      lis = new LineReadingInputStream(bis);
      // Config file is UTF-8 too!
      return new SimpleFieldSet(lis, 1024*1024, 128, true, true, true); // FIXME? advanced users may edit the config file, hence true?
    } finally {
      Closer.close(lis);
      Closer.close(bis);
View Full Code Here

TOP

Related Classes of freenet.support.io.LineReadingInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.