src/Tools/jEdit/src/jedit/isabelle_encoding.scala
author wenzelm
Tue Dec 08 16:30:20 2009 +0100 (2009-12-08)
changeset 34760 dc7f5e0d9d27
parent 34759 bfea7839d9e1
child 34777 91d6089cef88
permissions -rw-r--r--
misc modernization of names;
     1 /*
     2  * Isabelle encoding -- based on utf-8
     3  *
     4  * @author Makarius
     5  */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import org.gjt.sp.jedit.io.Encoding
    11 import org.gjt.sp.jedit.buffer.JEditBuffer
    12 
    13 import java.nio.charset.{Charset, CharsetDecoder, CodingErrorAction}
    14 import java.io.{InputStream, OutputStream, Reader, Writer, InputStreamReader, OutputStreamWriter,
    15   CharArrayReader, ByteArrayOutputStream}
    16 
    17 import scala.io.{Source, BufferedSource}
    18 
    19 
    20 object Isabelle_Encoding
    21 {
    22   val NAME = "UTF-8-Isabelle"
    23 
    24   def is_active(buffer: JEditBuffer): Boolean =
    25     buffer.getProperty(JEditBuffer.ENCODING).asInstanceOf[String] == NAME
    26 }
    27 
    28 class Isabelle_Encoding extends Encoding
    29 {
    30   private val charset = Charset.forName(Isabelle_System.charset)
    31   private val BUFSIZE = 32768
    32 
    33   private def text_reader(in: InputStream, decoder: CharsetDecoder): Reader =
    34   {
    35     def source(): Source =
    36       BufferedSource.fromInputStream(in, decoder, BUFSIZE, { () => source() })
    37     new CharArrayReader(Isabelle.symbols.decode(source.mkString).toArray)
    38   }
    39 
    40 	override def getTextReader(in: InputStream): Reader =
    41     text_reader(in, charset.newDecoder())
    42 
    43 	override def getPermissiveTextReader(in: InputStream): Reader =
    44 	{
    45 		val decoder = charset.newDecoder()
    46 		decoder.onMalformedInput(CodingErrorAction.REPLACE)
    47 		decoder.onUnmappableCharacter(CodingErrorAction.REPLACE)
    48 		text_reader(in, decoder)
    49 	}
    50 
    51   override def getTextWriter(out: OutputStream): Writer =
    52   {
    53     val buffer = new ByteArrayOutputStream(BUFSIZE) {
    54       override def flush()
    55       {
    56         val text = Isabelle.symbols.encode(toString(Isabelle_System.charset))
    57         out.write(text.getBytes(Isabelle_System.charset))
    58         out.flush()
    59       }
    60       override def close() { out.close() }
    61     }
    62 		new OutputStreamWriter(buffer, charset.newEncoder())
    63   }
    64 }