글쓴이 :김덕태 1998년 1월 21일 16:24:27
In Reply to: 넷스케이프4 에서도 유니코드는 모두 제대로 나타납니다. posted by 윤경구 on 1998년 1월 21일 11:54:08:
윈95의 익스플로러 4.0, 넷스케이프 4.0 모두에서
유니코드 외부 인코딩 및 유니코드의 모든 현대한글을 포함하는 UTF8 인코딩으로
된 웹 문서를 제대로 출력해줍니다.
익스플로러는 여러가지 자동적으로 해주기 때문에 그냥 웹 문서를 방문하거나,
폰트를 선택해주면 되는 데,
넷스케이프에서는 Preferences의 유니코드에 대한 폰트를 `굴림체'와 같은
한글 폰트로 설정한 후,
UTF8 인코딩의 문서일 경우에는 UTF8을 선택하고,
유니코드 외부 인코딩의 문서일 경우에는 아무 인코딩이나 선택해도
유니코드 한글이 보입니다. (아마, 유니코드 외부 인코딩의 바이트 순서 표식 (0xfffe 혹은 0xfeff)
이 있으면 무조건 유니코드 인코딩이라고 가정하는 것 같군요.
경구님의 유니코드 문서를 가져와서 다음 프로그램을 사용하여 UTF8 인코딩으로 변환한 후,
저장하여 테스트해보았습니다.
하지만, 다른 플랫폼 (특히, 유닉스)에서 현대 한글을 출력하기가 곤란한 것이 걸림돌이지요.
C:\example\i18n> java EncodingConverter2 Unicode UTF8 < unicode.html >! utf8.html
/* Classes in sun.io packages should be carefully used
since many of them are not robust esp. for exception handling
*/
import java.io.*;
import sun.io.*;
class EncodingConverter2
{
public static void main(String[] args)
throws IOException
{
ByteToCharConverter btc = ByteToCharConverter.getConverter(args[0]);
btc.setSubstitutionMode( false );
CharToByteConverter ctb = CharToByteConverter.getConverter(args[1]);
ctb.setSubstitutionMode( false );
byte[] inBuf = new byte[1];
char[] unicodes = new char[btc.getMaxCharsPerByte()];
byte[] outBuf = new byte[(btc.getMaxCharsPerByte()+1)
* ctb.getMaxBytesPerChar()];
int inOff = 0;
for(int b; (b = System.in.read()) != -1; inOff++)
{
inBuf[0] = (byte) b;
int uniLen = 0;
try
{ uniLen = btc.convert( inBuf, 0, 1, unicodes, 0, unicodes.length );
} catch( MalformedInputException ex )
{ handleBadInput( inOff );
} catch( UnknownCharacterException ex )
{ handleBadInput( inOff );
} catch( ConversionBufferFullException ex )
{ exit(ex);
}
if ( uniLen != 0 )
{
try
{ int outLen = ctb.convert( unicodes, 0, uniLen,
outBuf, 0, outBuf.length );
System.out.write(outBuf, 0, outLen);
} catch( MalformedInputException ex )
{ handleBadInput( inOff );
} catch( UnknownCharacterException ex )
{ handleBadOutput( inOff, ctb, unicodes, uniLen, outBuf );
} catch( ConversionBufferFullException ex )
{ exit(ex);
}
}
}
try
{ int uniLen = btc.flush( unicodes, 0, unicodes.length );
int outLen = ctb.convert( unicodes, 0, uniLen,
outBuf, 0, outBuf.length );
System.out.write(outBuf, 0, outLen);
} catch( MalformedInputException ex )
{ handleBadInput( inOff );
} catch( UnknownCharacterException ex )
{ handleBadInput( inOff );
} catch( ConversionBufferFullException ex )
{ exit(ex);
}
System.out.close();
}
static void handleBadOutput( int inOff, CharToByteConverter ctb,
char[] unicodes, int uniLen, byte[] outBuf )
{
System.err.println( "경고: 위치 " + inOff
+ "의 입력 문자를 표현할 수 없는 출력 인코딩." );
try
{ ctb.setSubstitutionMode( true );
int outLen = ctb.convert( unicodes, 0, uniLen,
outBuf, 0, outBuf.length );
System.out.write(outBuf, 0, outLen);
ctb.setSubstitutionMode( false );
} catch( Exception ex )
{ exit( ex );
}
}
static void handleBadInput( int inOff )
{
System.err.println( "오류: 위치 " + inOff
+ "에 잘못된 입력 문자로 중단됨." );
System.exit(-1);
}
static void exit( Throwable ex )
{ System.err.println( "오류: 기대되지 않는 오류로 중단됨. 원인 -> " + ex );
System.exit(-1);
}
}