| author | wenzelm |
| Sun, 11 Nov 2018 12:13:24 +0100 | |
| changeset 69283 | 39044da8bb5a |
| parent 67304 | 3cf05d7cf174 |
| child 73340 | 0ffcad1f6130 |
| permissions | -rw-r--r-- |
|
66457
9098c36abd1a
separate base plugin for important services that should be always available, despite startup errors of the main plugin;
wenzelm
parents:
62527
diff
changeset
|
1 |
/* Title: Tools/jEdit/src-base/isabelle_encoding.scala |
| 36760 | 2 |
Author: Makarius |
3 |
||
4 |
Isabelle encoding -- based on UTF-8. |
|
5 |
*/ |
|
| 34619 | 6 |
|
|
66457
9098c36abd1a
separate base plugin for important services that should be always available, despite startup errors of the main plugin;
wenzelm
parents:
62527
diff
changeset
|
7 |
package isabelle.jedit_base |
| 34619 | 8 |
|
| 34760 | 9 |
|
| 36015 | 10 |
import isabelle._ |
11 |
||
| 34619 | 12 |
import org.gjt.sp.jedit.io.Encoding |
13 |
||
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
14 |
import java.nio.charset.{Charset, CodingErrorAction, CharacterCodingException}
|
| 34620 | 15 |
import java.io.{InputStream, OutputStream, Reader, Writer, InputStreamReader, OutputStreamWriter,
|
16 |
CharArrayReader, ByteArrayOutputStream} |
|
17 |
||
| 48277 | 18 |
import scala.io.{Codec, BufferedSource}
|
| 34619 | 19 |
|
20 |
||
| 34738 | 21 |
class Isabelle_Encoding extends Encoding |
| 34619 | 22 |
{
|
| 43516 | 23 |
private val BUFSIZE = 32768 |
| 34620 | 24 |
|
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
25 |
private def text_reader(in: InputStream, codec: Codec, strict: Boolean): Reader = |
| 34620 | 26 |
{
|
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
27 |
val source = (new BufferedSource(in)(codec)).mkString |
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
28 |
|
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
29 |
if (strict && Codepoint.iterator(source).exists(Symbol.is_code)) |
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
30 |
throw new CharacterCodingException() |
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
31 |
|
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
32 |
new CharArrayReader(Symbol.decode(source).toArray) |
| 34620 | 33 |
} |
| 34619 | 34 |
|
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
35 |
override def getTextReader(in: InputStream): Reader = |
|
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
36 |
text_reader(in, UTF8.codec(), true) |
| 34619 | 37 |
|
| 37175 | 38 |
override def getPermissiveTextReader(in: InputStream): Reader = |
39 |
{
|
|
| 50203 | 40 |
val codec = UTF8.codec() |
| 37175 | 41 |
codec.onMalformedInput(CodingErrorAction.REPLACE) |
42 |
codec.onUnmappableCharacter(CodingErrorAction.REPLACE) |
|
|
67304
3cf05d7cf174
more robust treatment of conflicts with existing Unicode text;
wenzelm
parents:
66457
diff
changeset
|
43 |
text_reader(in, codec, false) |
| 37175 | 44 |
} |
| 34620 | 45 |
|
46 |
override def getTextWriter(out: OutputStream): Writer = |
|
47 |
{
|
|
48 |
val buffer = new ByteArrayOutputStream(BUFSIZE) {
|
|
49 |
override def flush() |
|
50 |
{
|
|
| 50203 | 51 |
val text = Symbol.encode(toString(UTF8.charset_name)) |
| 62527 | 52 |
out.write(UTF8.bytes(text)) |
| 34620 | 53 |
out.flush() |
54 |
} |
|
| 34621 | 55 |
override def close() { out.close() }
|
| 34620 | 56 |
} |
| 50203 | 57 |
new OutputStreamWriter(buffer, UTF8.charset.newEncoder()) |
| 34620 | 58 |
} |
| 34619 | 59 |
} |