langtools/src/share/classes/com/sun/tools/javac/util/Log.java
changeset 167 f4b81c733bea
parent 10 06bc494ca11e
child 731 1dd22bdb9ca5
equal deleted inserted replaced
166:9f5e9d551da1 167:f4b81c733bea
   201 
   201 
   202     /** The buffer containing the file that's currently translated.
   202     /** The buffer containing the file that's currently translated.
   203      */
   203      */
   204     private char[] buf = null;
   204     private char[] buf = null;
   205 
   205 
       
   206     /** The length of useful data in buf
       
   207      */
       
   208     private int bufLen = 0;
       
   209 
   206     /** The position in the buffer at which last error was reported
   210     /** The position in the buffer at which last error was reported
   207      */
   211      */
   208     private int bp;
   212     private int bp;
   209 
   213 
   210     /** number of the current source line; first line is 1
   214     /** number of the current source line; first line is 1
   254 
   258 
   255     /** Re-assign source buffer for existing source name.
   259     /** Re-assign source buffer for existing source name.
   256      */
   260      */
   257     protected void setBuf(char[] newBuf) {
   261     protected void setBuf(char[] newBuf) {
   258         buf = newBuf;
   262         buf = newBuf;
       
   263         bufLen = buf.length;
   259         bp = 0;
   264         bp = 0;
   260         lineStart = 0;
   265         lineStart = 0;
   261         line = 1;
   266         line = 1;
   262     }
   267     }
   263 
   268 
   322     private void printErrLine(int pos, PrintWriter writer) {
   327     private void printErrLine(int pos, PrintWriter writer) {
   323         if (!findLine(pos))
   328         if (!findLine(pos))
   324             return;
   329             return;
   325 
   330 
   326         int lineEnd = lineStart;
   331         int lineEnd = lineStart;
   327         while (lineEnd < buf.length && buf[lineEnd] != CR && buf[lineEnd] != LF)
   332         while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
   328             lineEnd++;
   333             lineEnd++;
   329         if (lineEnd - lineStart == 0)
   334         if (lineEnd - lineStart == 0)
   330             return;
   335             return;
   331         printLines(writer, new String(buf, lineStart, lineEnd - lineStart));
   336         printLines(writer, new String(buf, lineStart, lineEnd - lineStart));
   332         for (bp = lineStart; bp < pos; bp++) {
   337         for (bp = lineStart; bp < pos; bp++) {
   334         }
   339         }
   335         writer.println("^");
   340         writer.println("^");
   336         writer.flush();
   341         writer.flush();
   337     }
   342     }
   338 
   343 
   339     protected static char[] getCharContent(JavaFileObject fileObject) throws IOException {
   344     protected void initBuf(JavaFileObject fileObject) throws IOException {
   340         CharSequence cs = fileObject.getCharContent(true);
   345         CharSequence cs = fileObject.getCharContent(true);
   341         if (cs instanceof CharBuffer) {
   346         if (cs instanceof CharBuffer) {
   342             return JavacFileManager.toArray((CharBuffer)cs);
   347             CharBuffer cb = (CharBuffer) cs;
       
   348             buf = JavacFileManager.toArray(cb);
       
   349             bufLen = cb.limit();
   343         } else {
   350         } else {
   344             return cs.toString().toCharArray();
   351             buf = cs.toString().toCharArray();
       
   352             bufLen = buf.length;
   345         }
   353         }
   346     }
   354     }
   347 
   355 
   348     /** Find the line in the buffer that contains the current position
   356     /** Find the line in the buffer that contains the current position
   349      * @param pos      Character offset into the buffer
   357      * @param pos      Character offset into the buffer
   351     private boolean findLine(int pos) {
   359     private boolean findLine(int pos) {
   352         if (pos == Position.NOPOS || currentSource() == null)
   360         if (pos == Position.NOPOS || currentSource() == null)
   353             return false;
   361             return false;
   354         try {
   362         try {
   355             if (buf == null) {
   363             if (buf == null) {
   356                 buf = getCharContent(currentSource());
   364                 initBuf(currentSource());
   357                 lineStart = 0;
   365                 lineStart = 0;
   358                 line = 1;
   366                 line = 1;
   359             } else if (lineStart > pos) { // messages don't come in order
   367             } else if (lineStart > pos) { // messages don't come in order
   360                 lineStart = 0;
   368                 lineStart = 0;
   361                 line = 1;
   369                 line = 1;
   362             }
   370             }
   363             bp = lineStart;
   371             bp = lineStart;
   364             while (bp < buf.length && bp < pos) {
   372             while (bp < bufLen && bp < pos) {
   365                 switch (buf[bp++]) {
   373                 switch (buf[bp++]) {
   366                 case CR:
   374                 case CR:
   367                     if (bp < buf.length && buf[bp] == LF) bp++;
   375                     if (bp < bufLen && buf[bp] == LF) bp++;
   368                     line++;
   376                     line++;
   369                     lineStart = bp;
   377                     lineStart = bp;
   370                     break;
   378                     break;
   371                 case LF:
   379                 case LF:
   372                     line++;
   380                     line++;
   373                     lineStart = bp;
   381                     lineStart = bp;
   374                     break;
   382                     break;
   375                 }
   383                 }
   376             }
   384             }
   377             return bp <= buf.length;
   385             return bp <= bufLen;
   378         } catch (IOException e) {
   386         } catch (IOException e) {
   379             //e.printStackTrace();
   387             //e.printStackTrace();
   380             // FIXME: include e.getLocalizedMessage() in error message
   388             // FIXME: include e.getLocalizedMessage() in error message
   381             printLines(errWriter, getLocalizedString("source.unavailable"));
   389             printLines(errWriter, getLocalizedString("source.unavailable"));
   382             errWriter.flush();
   390             errWriter.flush();
   702      */
   710      */
   703     protected int getColumnNumber(int pos) {
   711     protected int getColumnNumber(int pos) {
   704         if (findLine(pos)) {
   712         if (findLine(pos)) {
   705             int column = 0;
   713             int column = 0;
   706             for (bp = lineStart; bp < pos; bp++) {
   714             for (bp = lineStart; bp < pos; bp++) {
   707                 if (bp >= buf.length)
   715                 if (bp >= bufLen)
   708                     return 0;
   716                     return 0;
   709                 if (buf[bp] == '\t')
   717                 if (buf[bp] == '\t')
   710                     column = (column / TabInc * TabInc) + TabInc;
   718                     column = (column / TabInc * TabInc) + TabInc;
   711                 else
   719                 else
   712                     column++;
   720                     column++;