예제 #1
0
  /** Returns a piped Reader giving the text content extracted by the outPutContent method. */
  public Reader getReader(String path, String encoding) {
    PipedReader pipeIn = null;
    PipedWriter pipeOut = null;

    try {
      pipeIn = new PipedReader();
      pipeOut = new PipedWriter(pipeIn);

      new ParserThread(pipeOut, path, encoding).start();
    } catch (IOException e) {
      SilverTrace.error(
          "indexEngine", "PipedParser", "indexEngine.MSG_PIPE_CREATION_FAILED", path, e);
      try {
        if (pipeIn != null) {
          pipeIn.close();
        }
        if (pipeOut != null) {
          pipeOut.close();
        }
      } catch (IOException ignored) {
      }

      pipeIn = null;
    }

    return pipeIn;
  }
예제 #2
0
 /** @tests java.io.PipedReader#ready() */
 public void test_ready() throws Exception {
   char[] c = null;
   preader = new PipedReader();
   t = new Thread(new PWriter(preader), "");
   t.start();
   Thread.sleep(500); // Allow writer to start
   assertTrue("Reader should be ready", preader.ready());
   c = new char[11];
   for (int i = 0; i < c.length; i++) c[i] = (char) preader.read();
   assertFalse("Reader should not be ready after reading all chars", preader.ready());
 }
예제 #3
0
 /** @tests java.io.PipedReader#close() */
 public void test_close() throws Exception {
   char[] c = null;
   preader = new PipedReader();
   t = new Thread(new PWriter(preader), "");
   t.start();
   Thread.sleep(500); // Allow writer to start
   c = new char[11];
   preader.read(c, 0, 11);
   preader.close();
   assertEquals("Read incorrect chars", "Hello World", new String(c));
 }
예제 #4
0
 /** @tests java.io.PipedReader#read(char[], int, int) */
 public void test_read$CII_4() throws IOException {
   PipedWriter pw = new PipedWriter();
   PipedReader obj = null;
   try {
     obj = new PipedReader(pw);
     obj.read(new char[0], (int) -1, (int) -1);
     fail("IndexOutOfBoundsException expected");
   } catch (ArrayIndexOutOfBoundsException t) {
     fail("IndexOutOfBoundsException expected");
   } catch (IndexOutOfBoundsException t) {
     // Expected
   }
 }
예제 #5
0
 /** @tests java.io.PipedReader#read(char[], int, int) */
 public void test_read$CII_2() throws IOException {
   // Regression for HARMONY-387
   PipedWriter pw = new PipedWriter();
   PipedReader obj = null;
   try {
     obj = new PipedReader(pw);
     obj.read(new char[0], (int) 0, (int) -1);
     fail("IndexOutOfBoundsException expected");
   } catch (IndexOutOfBoundsException t) {
     assertEquals(
         "IndexOutOfBoundsException rather than a subclass expected",
         IndexOutOfBoundsException.class,
         t.getClass());
   }
 }
예제 #6
0
  private void help() throws IOException {
    String[] help =
        new String[] {
          help_fw_page, help_bw_page, help_fw_dline, help_bw_dline, help_fw_sline, help_bw_sline,
          help_go_start, help_go_end, help_fw_search1, help_fw_search2, help_bw_search1,
              help_bw_search2,
          help_help, help_quit
        };
    // Remember the 'current' buffer so that we can repaint it
    // when we are done.
    ScreenBuffer prevBuffer = this.currentBuffer;

    // Prepare and paint the help screen
    ScreenBuffer buffer = new ScreenBuffer(true);
    for (int i = 0; i < help.length; i++) {
      prepareLine(help[i], i, buffer);
    }
    buffer.adjust(0, 0);
    buffer.output();
    prompt(str_any_key);

    // Wait till the user is done, then repaint the previous screen.
    pr.read();
    prompt();
    prevBuffer.output();
  }
예제 #7
0
  /** @tests java.io.PipedReader#connect(java.io.PipedWriter) */
  public void test_connectLjava_io_PipedWriter() throws Exception {
    char[] c = null;

    preader = new PipedReader();
    t = new Thread(pwriter = new PWriter(), "");
    preader.connect(pwriter.pw);
    t.start();
    Thread.sleep(500); // Allow writer to start
    c = new char[11];
    preader.read(c, 0, 11);

    assertEquals("Read incorrect chars", "Hello World", new String(c));
    try {
      preader.connect(pwriter.pw);
      fail("Failed to throw exception connecting to pre-connected reader");
    } catch (IOException e) {
      // Expected
    }
  }
예제 #8
0
 @Override
 public void run() {
   BufferedReader bufferedReader = new BufferedReader(reader);
   while (true) {
     try {
       System.out.println(reader.read());
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
예제 #9
0
 /**
  * Tear down the console and pipe.
  *
  * @throws IOException
  */
 private void tearDown() throws IOException {
   if (manager != null && console != null) {
     manager.unregisterConsole(console);
   }
   if (pw != null) {
     pw.close();
   }
   if (pr != null) {
     pr.close();
   }
 }
예제 #10
0
 /**
  * Read a line up to the next newline and return it as a String. The line is echoed at the prompt
  * location.
  *
  * @param ch a preread character.
  * @return
  * @throws IOException
  */
 private String readLine(int ch) throws IOException {
   StringBuffer sb = new StringBuffer();
   String line;
   do {
     sb.append((char) ch);
     line = sb.toString();
     prompt(line);
     ch = pr.read();
   } while (ch != -1 && ch != '\n');
   return line;
 }
예제 #11
0
 /** @tests java.io.PipedReader#read() */
 public void test_read() throws Exception {
   char[] c = null;
   preader = new PipedReader();
   t = new Thread(new PWriter(preader), "");
   t.start();
   Thread.sleep(500); // Allow writer to start
   c = new char[11];
   for (int i = 0; i < c.length; i++) {
     c[i] = (char) preader.read();
   }
   assertEquals("Read incorrect chars", "Hello World", new String(c));
 }
예제 #12
0
 /** @tests java.io.PipedReader#read(char[], int, int) */
 public void test_read$CII() throws Exception {
   char[] c = null;
   preader = new PipedReader();
   t = new Thread(new PWriter(preader), "");
   t.start();
   Thread.sleep(500); // Allow writer to start
   c = new char[11];
   int n = 0;
   int x = n;
   while (x < 11) {
     n = preader.read(c, x, 11 - x);
     x = x + n;
   }
   assertEquals("Read incorrect chars", "Hello World", new String(c));
   try {
     preader.close();
     preader.read(c, 8, 7);
     fail("Failed to throw exception reading from closed reader");
   } catch (IOException e) {
     // Expected
   }
 }
예제 #13
0
  /** Set up the pager's console and command pipe. */
  private void setup() throws NameNotFoundException, IOException {
    ShellManager sm = InitialNaming.lookup(ShellManager.NAME);
    manager = (TextScreenConsoleManager) sm.getCurrentShell().getConsole().getManager();
    console =
        manager.createConsole(
            "page",
            (ConsoleManager.CreateOptions.TEXT
                | ConsoleManager.CreateOptions.STACKED
                | ConsoleManager.CreateOptions.NO_LINE_EDITTING
                | ConsoleManager.CreateOptions.NO_SYSTEM_OUT_ERR));
    manager.focus(console);

    pageHeight = console.getDeviceHeight() - 1;
    pageWidth = console.getDeviceWidth();
    pageSize = pageHeight * pageWidth;
    tabSize = console.getTabSize();

    pw = new PipedWriter();
    pr = new PipedReader();
    pr.connect(pw);

    console.addKeyboardListener(this);
  }
예제 #14
0
파일: Pipe.java 프로젝트: karen89/wuyangnju
 /**
  * Closes the reader of this pipe. After calling this method no data can be read from the pipe.
  *
  * @exception IOException thrown if we cannot close the reader
  */
 public void closeReader() throws IOException {
   reader_.close();
 }
예제 #15
0
  public void listMethods(
      Context context,
      String PID,
      Date asOfDateTime,
      boolean xml,
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServerException {

    OutputStreamWriter out = null;
    Date versDateTime = asOfDateTime;
    ObjectMethodsDef[] methodDefs = null;
    PipedWriter pw = null;
    PipedReader pr = null;

    try {
      pw = new PipedWriter();
      pr = new PipedReader(pw);
      methodDefs = m_access.listMethods(context, PID, asOfDateTime);

      // Object Profile found.
      // Serialize the ObjectProfile object into XML
      new ObjectMethodsDefSerializerThread(context, PID, methodDefs, versDateTime, pw).start();
      if (xml) {
        // Return results as raw XML
        response.setContentType(CONTENT_TYPE_XML);

        // Insures stream read from PipedReader correctly translates
        // utf-8
        // encoded characters to OutputStreamWriter.
        out = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
        int bufSize = 4096;
        char[] buf = new char[bufSize];
        int len = 0;
        while ((len = pr.read(buf, 0, bufSize)) != -1) {
          out.write(buf, 0, len);
        }
        out.flush();
      } else {
        // Transform results into an html table
        response.setContentType(CONTENT_TYPE_HTML);
        out = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
        File xslFile = new File(m_server.getHomeDir(), "access/listMethods.xslt");
        Templates template = XmlTransformUtility.getTemplates(xslFile);
        Transformer transformer = template.newTransformer();
        transformer.setParameter("fedora", context.getEnvironmentValue(FEDORA_APP_CONTEXT_NAME));
        transformer.transform(new StreamSource(pr), new StreamResult(out));
      }
      out.flush();
    } catch (ServerException e) {
      throw e;
    } catch (Throwable th) {
      String message = "Error listing methods";
      logger.error(message, th);
      throw new GeneralException(message, th);
    } finally {
      try {
        if (pr != null) {
          pr.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (Throwable th) {
        String message =
            "[ListMethodsServlet] An error has occured. "
                + " The error was a \" "
                + th.getClass().getName()
                + " \". Reason: "
                + th.getMessage();
        throw new StreamIOException(message);
      }
    }
  }
예제 #16
0
  /** @tests java.io.PipedReader#read(char[], int, int) */
  public void test_read_$CII_IOException() throws IOException {
    PipedWriter pw = new PipedWriter();
    PipedReader pr = new PipedReader(pw);
    char[] buf = null;
    pr.close();
    try {
      pr.read(buf, 0, 10);
      fail("Should throws IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pr = new PipedReader();
    buf = null;
    pr.close();
    try {
      pr.read(buf, 0, 10);
      fail("Should throws IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    buf = new char[10];
    pr.close();
    try {
      pr.read(buf, -1, 0);
      fail("Should throws IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    buf = new char[10];
    pr.close();
    try {
      pr.read(buf, 0, -1);
      fail("Should throws IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    buf = new char[10];
    pr.close();
    try {
      pr.read(buf, 1, 10);
      fail("Should throws IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    pr.close();
    try {
      pr.read(new char[0], -1, -1);
      fail("should throw IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    pr.close();
    try {
      pr.read(null, 0, 1);
      fail("should throw IOException"); // $NON-NLS-1$
    } catch (IOException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    try {
      pr.read(null, -1, 1);
      fail("should throw IndexOutOfBoundsException"); // $NON-NLS-1$
    } catch (IndexOutOfBoundsException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    try {
      pr.read(null, 0, -1);
      fail("should throw NullPointerException"); // $NON-NLS-1$
    } catch (NullPointerException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    try {
      pr.read(new char[10], 11, 0);
      fail("should throw IndexOutOfBoundsException"); // $NON-NLS-1$
    } catch (IndexOutOfBoundsException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }

    pw = new PipedWriter();
    pr = new PipedReader(pw);
    try {
      pr.read(null, 1, 0);
      fail("should throw NullPointerException"); // $NON-NLS-1$
    } catch (NullPointerException e) {
      // expected
    } finally {
      pw = null;
      pr = null;
    }
  }
예제 #17
0
파일: Pipe.java 프로젝트: karen89/wuyangnju
 /**
  * This method reads a character from the pipe.
  *
  * @exception IOException thrown if we cannot read from the pipe
  * @return char next chracter in the stream
  */
 public int read() throws IOException {
   return reader_.read();
 }
예제 #18
0
  /**
   * Do the paging, reading commands from our private console input pipe to figure out what to do
   * next.
   *
   * @param r the source of data to be paged.
   * @throws IOException
   */
  private void pager() throws IOException {
    // Output first page.
    console.clear();
    bottomLineNo = -1;
    boolean exit = false;
    nextPage();

    // Process commands until we reach the EOF on the data source or
    // the command pipe.
    while (!exit) {
      prompt();
      int ch = pr.read();
      erasePrompt();
      switch (ch) {
        case -1:
          exit = true;
          break;
        case ' ':
        case 'f':
          if (lineStore.isLastLineNo(bottomLineNo)) {
            exit = true;
          } else {
            nextPage();
          }
          break;
        case 'b':
          prevPage();
          break;
        case 'k':
        case 'y':
          prevLine();
          break;
        case '\n':
          if (lineStore.isLastLineNo(bottomLineNo)) {
            exit = true;
          } else {
            nextLine();
          }
          break;
        case 'u':
          prevScreenLine();
          break;
        case 'd':
          nextScreenLine();
          break;
        case '<':
          gotoPage(0);
          break;
        case '>':
          gotoLastPage();
          break;
        case '/':
          searchForwards();
          break;
        case '?':
          searchBackwards();
          break;
        case '\004': // ^D
        case 'q':
          exit = true;
          break;
        case 'h':
          help();
        default:
          // ignore
      }
    }
  }