📄 199-203.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Futil Recipes for Feudal Times</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=4//-->
<!--PAGES=199-203//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="195-199.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="203-207.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading48"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose that the following variables are predefined:
</P>
<!-- CODE //-->
<PRE>
char c;
String string;
OutputStream os;
short s;
int i, offset, length;
long l;
float f;
double d;
byte b;
byte bytes[];
</PRE>
<!-- END CODE //-->
<P>To construct a new <I>DataOutputStream</I> instance, use this code:</P>
<!-- CODE SNIP //-->
<PRE>
DataOutputStream dos = new DataOutputStream(is);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a byte, with blocking:
</P>
<!-- CODE SNIP //-->
<PRE>
dos.write(b);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a subarray of bytes:
</P>
<!-- CODE SNIP //-->
<PRE>
dos.write(bytes, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To flush the output stream:
</P>
<!-- CODE SNIP //-->
<PRE>
dos.flush();
</PRE>
<!-- END CODE SNIP //-->
<P>To write a boolean (as a 0 or 1 byte):
</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeBoolean(aboolean);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a byte to an underlying 8-bit representation:
</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeByte(b);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a <I>short</I> to an underlying 16 bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeShort(s);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a <I>char</I> to an underlying 16-bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeChar(c);
</PRE>
<!-- END CODE SNIP //-->
<P>To write an <I>int</I> to an underlying 32-bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeInt(i);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a <I>long</I> to an underlying 64-bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeLong(l);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a <I>float</I> to an underlying 32-bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeFloat(f);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a <I>double</I> to an underlying 64-bit representation with the high byte first:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeDouble(d);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a string as a sequence of bytes (this casts each <I>char</I> as a byte and then writes the byte; it will write <I>s.len</I> bytes):</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeBytes(s);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a string as a sequence of 16-bit <I>chars</I>. This will output 2 * <I>s.len</I> bytes:</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeChars(s);
</PRE>
<!-- END CODE SNIP //-->
<P>To write a string in a machine-independent UTF-8 format:
</P>
<!-- CODE SNIP //-->
<PRE>
dos.writeUTF(s);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the number of bytes written:
</P>
<!-- CODE SNIP //-->
<PRE>
i = dos.size();
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading49"></A><FONT COLOR="#000077">Java, C, C++ -> HTML: The HTML Converter</FONT></H4>
<P>Formats! You want formats? We got formats! Formats for browsers (HTML). Formats for word processors (RTF). Formats for computer languages (Java, C, C++). Formats for images (JPEG, GIF, PPM). Formats—you can’t live with ’em, you can’t live without ’em!
</P>
<P>In this section we address the conversion of Java, C, and C++ into formatted and colored HTML.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CD-ROM: </B>One of the packages included with DiffCAD is the <I>htmlconverter</I> package.<HR></FONT>
</BLOCKQUOTE>
<P>The <I>htmlconverter</I> package contains the support systems for the <I>HtmlGenerator</I> class, a Java, C, and C++ source code conversion program that generates formatted and colored HTML files. The following code was converted to HTML by <I>HtmlGenerator</I>. The HTML was then converted into RTF (rich text format), the format Microsoft Word reads. The conversion was performed using a program called MacLinkPlus [DataViz].</P>
<!-- CODE SNIP //-->
<PRE>
1. <B>package</B> htmlconverter;
2. <B>import</B> java.io.*;
</PRE>
<!-- END CODE SNIP //-->
<P><I>JavaStream</I> extends <I>DataInputStream</I> and so inherits the <I>DataInputStream</I> methods.</P>
<!-- CODE SNIP //-->
<PRE>
3. <B>public class</B> JavaStream <B>extends</B> DataInputStream
4. <B>implements</B> JavaText, CText, CplusplusText {
5. JavaHtmlString mainText = <B>new</B> JavaHtmlString();
6. JavaHtmlString comments = <B>new</B> JavaHtmlString();
7. JavaHtmlString strings = <B>new</B> JavaHtmlString();
8. JavaHtmlString keywords = <B>new</B> JavaHtmlString();
9. DataOutputStream dos;
10. <B>public static</B> String[] reservedWords = javaReservedWords;
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>JavaStream</I> constructor takes an <I>InputStream</I>.</P>
<!-- CODE //-->
<PRE>
11. JavaStream(DataInputStream s0, DataOutputStream s1) {
12. super(s0);
13. dos = s1;
14. }
15. <B>public void</B> convertToHtml() {
16. <B>int</B> index;
17. <B>boolean</B> isCommentedOut = <B>false;</B>
18. <B>boolean</B> isQuoted = <B>false;</B>
19. <B>boolean</B> isSingleQuoted = <B>false;</B>
</PRE>
<!-- END CODE //-->
<P>The basic idea behind the <I>convertToHtml</I> method is that a string of characters should be read using the <I>readln</I> method. Because <I>JavaStream</I> is a subclass of <I>DataInputStream</I>, <I>JavaStream</I> inherits the <I>readln</I> method.</P>
<!-- CODE SNIP //-->
<PRE>
1. String line;
2. String str1, str2;
3. try {
4. while (true) {
</PRE>
<!-- END CODE SNIP //-->
<P>Line 5 reads until the end of a line and places the characters into the <I>line</I> string.</P>
<!-- CODE SNIP //-->
<PRE>
5. line = readLine(); // get a line
6. if (line == null) { // if line is null
7. break; // break this loop
8. }
</PRE>
<!-- END CODE SNIP //-->
<P>After a line of text is read, a series of search-and-replace operations is performed. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
1. for (int counter = 0; counter < line.length(); counter++) {
</PRE>
<!-- END CODE SNIP //-->
<P>Recall from Chapter 2 that to find <I>aString</I> starting from <I>anInt</I> we use the following:</P>
<!-- CODE SNIP //-->
<PRE>
anInt = anotherString.indexOf(aString, anInt);
</PRE>
<!-- END CODE SNIP //-->
<P>So in line 2, we start from the <I>counter</I> position and look for the less-than character.</P>
<!-- CODE SNIP //-->
<PRE>
2. index = line.indexOf("<", counter);
3. if (index < 0) break;
4. // if < is not found then break this loop
</PRE>
<!-- END CODE SNIP //-->
<P>We then insert the string at the index point, replacing the '<I><'</I> with '<I>&lt'</I>. This replacement is performed because HTML represents the '<' character as '<I>&lt'</I> and holds '<' as reserved.</P>
<!-- CODE SNIP //-->
<PRE>
5. line = line.substring(0, index) + "&lt;"
6. + line.substring(index + 1, line.length());
7. // replace < to &lt;
8. counter = index + 4;
9. // update counter
10. }
</PRE>
<!-- END CODE SNIP //-->
<P>This search-and-replace is continued until all the replacements are performed. Then the line is written to the <I>DataOutputStream</I> instance.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="195-199.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="203-207.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -