From: Olyutorskii Date: Wed, 24 Aug 2011 11:12:08 +0000 (+0900) Subject: binioパッケージと統合 X-Git-Tag: fromMercurial~43 X-Git-Url: http://git.osdn.net/view?p=mikutoga%2FTogaGem.git;a=commitdiff_plain;h=0ded51c2894bc773e619e5853df259051050068e binioパッケージと統合 --- diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/AbstractExporter.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/AbstractExporter.java deleted file mode 100644 index 6f21bfc..0000000 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/AbstractExporter.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * abstract exporter - * - * License : The MIT License - * Copyright(c) 2010 MikuToga Partners - */ - -package jp.sourceforge.mikutoga.pmd.model.binio; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CoderResult; -import java.nio.charset.CodingErrorAction; - -/** - * 抽象化されたエクスポーター共通部。 - */ -public abstract class AbstractExporter { - - private static final Charset CS_WIN31J = Charset.forName("windows-31j"); - - private static final String ERRMSG_TOOLONGTEXT = "too long text"; - private static final String ERRMSG_INVUCSSEQ = "invalid unicode sequence"; - private static final String ERRMSG_NONWIN31J = "no character in win31j"; - - private static final int BYTES_SHORT = Short .SIZE / Byte.SIZE; - private static final int BYTES_INT = Integer.SIZE / Byte.SIZE; - private static final int BYTES_FLOAT = Float .SIZE / Byte.SIZE; - - private static final int BUFSZ_BYTE = 512; - private static final int BUFSZ_CHAR = 512; - - private final OutputStream ostream; - - private final byte[] barray; - private final ByteBuffer bbuf; - - private final CharBuffer cbuf; - private final CharsetEncoder encoder; - - - /** - * コンストラクタ。 - * @param stream 出力ストリーム - * @throws NullPointerException 引数がnull - */ - protected AbstractExporter(OutputStream stream) - throws NullPointerException{ - super(); - - if(stream == null) throw new NullPointerException(); - this.ostream = stream; - - this.barray = new byte[BUFSZ_BYTE]; - this.bbuf = ByteBuffer.wrap(this.barray); - this.bbuf.order(ByteOrder.LITTLE_ENDIAN); - - this.cbuf = CharBuffer.allocate(BUFSZ_CHAR); - this.encoder = CS_WIN31J.newEncoder(); - this.encoder.onMalformedInput(CodingErrorAction.REPORT); - this.encoder.onUnmappableCharacter(CodingErrorAction.REPORT); - this.encoder.reset(); - - return; - } - - /** - * 出力をフラッシュする。 - * I/O効率とデバッグ効率のバランスを考え、ご利用は計画的に。 - * @throws IOException 出力エラー - */ - protected void flush() throws IOException{ - this.ostream.flush(); - return; - } - - /** - * byte値を出力する。 - * @param bVal byte値 - * @throws IOException 出力エラー - */ - protected void dumpByte(byte bVal) throws IOException{ - this.ostream.write((int)bVal); - return; - } - - /** - * int値をbyte値で出力する。 - * byte値に収まらない上位ビットはそのまま捨てられる。 - * @param iVal int値 - * @throws IOException 出力エラー - */ - protected void dumpByte(int iVal) throws IOException{ - dumpByte((byte)iVal); - return; - } - - /** - * short値を出力する。 - * @param sVal short値 - * @throws IOException 出力エラー - */ - protected void dumpShort(short sVal) throws IOException{ - this.bbuf.clear(); - this.bbuf.putShort(sVal); - this.ostream.write(this.barray, 0, BYTES_SHORT); - return; - } - - /** - * int値をshort値で出力する。 - * short値に収まらない上位ビットはそのまま捨てられる。 - * @param iVal int値 - * @throws IOException 出力エラー - */ - protected void dumpShort(int iVal) throws IOException{ - dumpShort((short)iVal); - return; - } - - /** - * int値を出力する。 - * @param iVal int値 - * @throws IOException 出力エラー - */ - protected void dumpInt(int iVal) throws IOException{ - this.bbuf.clear(); - this.bbuf.putInt(iVal); - this.ostream.write(this.barray, 0, BYTES_INT); - return; - } - - /** - * float値を出力する。 - * @param fVal float値 - * @throws IOException 出力エラー - */ - protected void dumpFloat(float fVal) throws IOException{ - this.bbuf.clear(); - this.bbuf.putFloat(fVal); - this.ostream.write(this.barray, 0, BYTES_FLOAT); - return; - } - - /** - * 文字列をwindows-31jエンコーディングで出力する。 - * @param seq 文字列 - * @return 出力されたbyte総数。 - * @throws IOException 出力エラー - * @throws IllegalPmdTextException 文字エンコーディングに関するエラー - */ - protected int dumpCharSequence(CharSequence seq) - throws IOException, IllegalPmdTextException{ - encodeToByteBuffer(seq); - this.bbuf.flip(); - int length = dumpByteBuffer(); - return length; - } - - /** - * windows-31jにエンコーディングした文字列を内部バッファに蓄積する。 - * @param seq 文字列 - * @throws IllegalPmdTextException 文字エンコーディングに関するエラー。 - * 考えられる状況としては、 - * - * など。 - */ - private void encodeToByteBuffer(CharSequence seq) - throws IllegalPmdTextException{ - this.cbuf.clear(); - try{ - this.cbuf.append(seq); - }catch(BufferOverflowException e){ - throw new IllegalPmdTextException(ERRMSG_TOOLONGTEXT); - } - this.cbuf.flip(); - - this.bbuf.clear(); - - CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); - if( ! result.isUnderflow()){ - if(result.isOverflow()){ - throw new IllegalPmdTextException(ERRMSG_TOOLONGTEXT); - }else if(result.isMalformed()){ - throw new IllegalPmdTextException(ERRMSG_INVUCSSEQ); - }else if(result.isUnmappable()){ - throw new IllegalPmdTextException(ERRMSG_NONWIN31J); - }else{ - assert false; - throw new AssertionError(); - } - } - - return; - } - - /** - * 内部バッファに蓄積されたbyte値並びを出力する。 - * @return 出力されたbyte数 - * @throws IOException 出力エラー - */ - private int dumpByteBuffer() throws IOException{ - int offset = this.bbuf.position(); - int limit = this.bbuf.limit(); - int length = limit - offset; - this.ostream.write(this.barray, offset, length); - - this.bbuf.position(limit); - - return length; - } - -} diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/IllegalPmdException.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/IllegalPmdException.java index c744d1c..d9ee392 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/IllegalPmdException.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/IllegalPmdException.java @@ -30,4 +30,13 @@ public class IllegalPmdException extends Exception{ return; } + /** + * コンストラクタ。 + * @param cause 原因 + */ + public IllegalPmdException(Throwable cause){ + super(cause); + return; + } + } diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterBase.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterBase.java index f4eced9..b894bbc 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterBase.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterBase.java @@ -15,6 +15,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import jp.sourceforge.mikutoga.binio.BinaryExporter; +import jp.sourceforge.mikutoga.binio.IllegalTextExportException; import jp.sourceforge.mikutoga.math.MkPos2D; import jp.sourceforge.mikutoga.math.MkPos3D; import jp.sourceforge.mikutoga.math.MkVec3D; @@ -39,7 +41,7 @@ import jp.sourceforge.mikutoga.pmd.parser.PmdLimits; * 英名対応以降のPMDファイルフォーマットを * 使いたくない場合はこのエクスポーターを用いて出力せよ。 */ -public class PmdExporterBase extends AbstractExporter{ +public class PmdExporterBase extends BinaryExporter{ /** 前(親)ボーンが無い場合の便宜的なボーンID。 */ public static final int NOPREVBONE_ID = 0xffff; @@ -109,42 +111,8 @@ public class PmdExporterBase extends AbstractExporter{ * 指定バイト長をはみ出した。 */ protected void dumpText(String text, int maxByteLength) - throws IOException, IllegalPmdTextException{ - dumpText(text, maxByteLength, FDFILLER); - return; - } - - /** - * 文字列を指定されたバイト長で出力する。 - * 文字列の改行記号はLF(0x0a)に正規化される。 - * エンコード結果がバイト長に満たない場合は - * fillerがパディングされる。 - * @param text 文字列 - * @param maxByteLength バイト超指定 - * @param filler 出力結果が足りない場合の詰め物。 - * それでも足りない場合は最後のbyte要素が繰り返し出力される。 - * @throws IOException 出力エラー - * @throws IllegalPmdTextException エンコード結果が - * 指定バイト長をはみ出した - */ - protected void dumpText(String text, int maxByteLength, byte[] filler) - throws IOException, IllegalPmdTextException{ - String normalized = normalizeBreak(text); - int blen = dumpCharSequence(normalized); - int remain = maxByteLength - blen; - - if(remain < 0) throw new IllegalPmdTextException("too long text"); - - int fillerIdx = 0; - while(remain > 0){ - if(fillerIdx >= filler.length){ - fillerIdx = filler.length - 1; - } - dumpByte(filler[fillerIdx]); - fillerIdx++; - remain--; - } - + throws IOException, IllegalTextExportException{ + dumpFixedW31j(text, maxByteLength, FDFILLER); return; } @@ -156,15 +124,19 @@ public class PmdExporterBase extends AbstractExporter{ */ public void dumpPmdModel(PmdModel model) throws IOException, IllegalPmdException{ - dumpBasic(model); - dumpVertexList(model); - dumpSurfaceList(model); - dumpMaterialList(model); - dumpBoneList(model); - dumpIKChainList(model); - dumpMorphList(model); - dumpMorphGroup(model); - dumpBoneGroupList(model); + try{ + dumpBasic(model); + dumpVertexList(model); + dumpSurfaceList(model); + dumpMaterialList(model); + dumpBoneList(model); + dumpIKChainList(model); + dumpMorphList(model); + dumpMorphGroup(model); + dumpBoneGroupList(model); + }catch(IllegalTextExportException e){ + throw new IllegalPmdException(e); + } return; } @@ -176,7 +148,7 @@ public class PmdExporterBase extends AbstractExporter{ * @throws IllegalPmdTextException モデル名もしくは説明が長すぎる */ private void dumpBasic(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ for(int idx=0; idx < MAGIC_BYTES.length; idx++){ dumpByte(MAGIC_BYTES[idx]); } @@ -282,7 +254,7 @@ public class PmdExporterBase extends AbstractExporter{ * @throws IllegalPmdTextException シェーディングファイル情報が長すぎる */ private void dumpMaterialList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ List materialList = model.getMaterialList(); int materialNum = materialList.size(); @@ -338,10 +310,10 @@ public class PmdExporterBase extends AbstractExporter{ * シェーディングファイル情報を出力する。 * @param shade シェーディング情報 * @throws IOException 出力エラー - * @throws IllegalPmdTextException ファイル名が長すぎる + * @throws IllegalTextExportException ファイル名が長すぎる */ private void dumpShadeFileInfo(ShadeInfo shade) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ String textureFile = shade.getTextureFileName(); String spheremapFile = shade.getSpheremapFileName(); @@ -356,9 +328,9 @@ public class PmdExporterBase extends AbstractExporter{ if(text.length() <= 0) filler = NULLFILLER; else filler = FDFILLER; - dumpText(text.toString(), - PmdLimits.MAXBYTES_TEXTUREFILENAME, - filler ); + dumpFixedW31j(text.toString(), + PmdLimits.MAXBYTES_TEXTUREFILENAME, + filler ); return; } @@ -370,7 +342,7 @@ public class PmdExporterBase extends AbstractExporter{ * @throws IllegalPmdTextException ボーン名が長すぎる */ private void dumpBoneList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ List boneList = model.getBoneList(); int boneNum = boneList.size(); @@ -392,7 +364,7 @@ public class PmdExporterBase extends AbstractExporter{ * @throws IllegalPmdTextException ボーン名が長すぎる */ private void dumpBone(BoneInfo bone) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ String boneName = bone.getBoneName().getPrimaryText(); dumpText(boneName, PmdLimits.MAXBYTES_BONENAME); @@ -483,7 +455,7 @@ public class PmdExporterBase extends AbstractExporter{ * @throws IllegalPmdTextException モーフ名が長すぎる */ private void dumpMorphList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ Map> morphMap = model.getMorphMap(); Set typeSet = morphMap.keySet(); List mergedMorphVertexList = model.mergeMorphVertex(); @@ -578,10 +550,10 @@ public class PmdExporterBase extends AbstractExporter{ * デフォルトボーングループ内訳は出力されない。 * @param model モデルデータ * @throws IOException 出力エラー - * @throws IllegalPmdTextException ボーングループ名が長すぎる + * @throws IllegalTextExportException ボーングループ名が長すぎる */ private void dumpBoneGroupList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ List groupList = model.getBoneGroupList(); int groupNum = groupList.size(); dumpByte(groupNum - 1); @@ -589,8 +561,8 @@ public class PmdExporterBase extends AbstractExporter{ int dispBoneNum = 0; for(BoneGroup group : groupList){ if(group.isDefaultBoneGroup()) continue; - dumpText(group.getGroupName().getPrimaryText(), - PmdLimits.MAXBYTES_BONEGROUPNAME, LFFILLER ); + dumpFixedW31j(group.getGroupName().getPrimaryText(), + PmdLimits.MAXBYTES_BONEGROUPNAME, LFFILLER ); dispBoneNum += group.getBoneList().size(); } dumpInt(dispBoneNum); diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt1.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt1.java index 38f629c..2a3355f 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt1.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt1.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Map; +import jp.sourceforge.mikutoga.binio.IllegalTextExportException; import jp.sourceforge.mikutoga.pmd.MorphType; import jp.sourceforge.mikutoga.pmd.model.BoneGroup; import jp.sourceforge.mikutoga.pmd.model.BoneInfo; @@ -60,7 +61,7 @@ public class PmdExporterExt1 extends PmdExporterBase{ * @throws IllegalPmdTextException 文字列が長すぎる。 */ private void dumpGlobalInfo(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalPmdException{ boolean hasGlobal = model.hasGlobalText(); byte globalFlag; if(hasGlobal) globalFlag = 0x01; @@ -68,10 +69,14 @@ public class PmdExporterExt1 extends PmdExporterBase{ dumpByte(globalFlag); if(hasGlobal){ - dumpBasicGlobal(model); - dumpBoneGlobal(model); - dumpMorphGlobal(model); - dumpBoneGroupGlobal(model); + try{ + dumpBasicGlobal(model); + dumpBoneGlobal(model); + dumpMorphGlobal(model); + dumpBoneGroupGlobal(model); + }catch(IllegalTextExportException e){ + throw new IllegalPmdException(e); + } } flush(); @@ -86,7 +91,7 @@ public class PmdExporterExt1 extends PmdExporterBase{ * @throws IllegalPmdTextException 文字列が長すぎる。 */ private void dumpBasicGlobal(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ String modelName = model.getModelName().getGlobalText(); if(modelName == null) modelName = ""; dumpText(modelName, PmdLimits.MAXBYTES_MODELNAME); @@ -105,7 +110,7 @@ public class PmdExporterExt1 extends PmdExporterBase{ * @throws IllegalPmdTextException 文字列が長すぎる。 */ private void dumpBoneGlobal(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ for(BoneInfo bone : model.getBoneList()){ String boneName = bone.getBoneName().getGlobalText(); if(boneName == null) boneName = ""; @@ -122,7 +127,7 @@ public class PmdExporterExt1 extends PmdExporterBase{ * @throws IllegalPmdTextException 文字列が長すぎる。 */ private void dumpMorphGlobal(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ Map> morphMap = model.getMorphMap(); for(MorphType type : MorphType.values()){ @@ -146,7 +151,7 @@ public class PmdExporterExt1 extends PmdExporterBase{ * @throws IllegalPmdTextException 文字列が長すぎる */ private void dumpBoneGroupGlobal(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ for(BoneGroup group : model.getBoneGroupList()){ if(group.isDefaultBoneGroup()) continue; String groupName = group.getGroupName().getGlobalText(); diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt2.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt2.java index 290c150..78fb5cd 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt2.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt2.java @@ -9,6 +9,7 @@ package jp.sourceforge.mikutoga.pmd.model.binio; import java.io.IOException; import java.io.OutputStream; +import jp.sourceforge.mikutoga.binio.IllegalTextExportException; import jp.sourceforge.mikutoga.pmd.model.PmdModel; import jp.sourceforge.mikutoga.pmd.model.ToonMap; import jp.sourceforge.mikutoga.pmd.parser.PmdLimits; @@ -43,7 +44,11 @@ public class PmdExporterExt2 extends PmdExporterExt1{ throws IOException, IllegalPmdException{ super.dumpPmdModel(model); - dumpToonMap(model); + try{ + dumpToonMap(model); + }catch(IllegalTextExportException e){ + throw new IllegalPmdException(e); + } return; } @@ -55,7 +60,7 @@ public class PmdExporterExt2 extends PmdExporterExt1{ * @throws IllegalPmdTextException トゥーンファイル名が長すぎる */ private void dumpToonMap(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ ToonMap map = model.getToonMap(); for(int idx = 0; idx < PmdLimits.TOON_FIXEDNUM; idx++){ diff --git a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt3.java b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt3.java index 2f978ec..d34bb1c 100644 --- a/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt3.java +++ b/src/main/java/jp/sourceforge/mikutoga/pmd/model/binio/PmdExporterExt3.java @@ -10,6 +10,7 @@ package jp.sourceforge.mikutoga.pmd.model.binio; import java.io.IOException; import java.io.OutputStream; import java.util.List; +import jp.sourceforge.mikutoga.binio.IllegalTextExportException; import jp.sourceforge.mikutoga.pmd.Deg3d; import jp.sourceforge.mikutoga.pmd.Rad3d; import jp.sourceforge.mikutoga.pmd.RigidShapeType; @@ -55,8 +56,12 @@ public class PmdExporterExt3 extends PmdExporterExt2{ throws IOException, IllegalPmdException{ super.dumpPmdModel(model); - dumpRigidList(model); - dumpJointList(model); + try{ + dumpRigidList(model); + dumpJointList(model); + }catch(IllegalTextExportException e){ + throw new IllegalPmdException(e); + } return; } @@ -68,7 +73,7 @@ public class PmdExporterExt3 extends PmdExporterExt2{ * @throws IllegalPmdTextException 長すぎる剛体名 */ private void dumpRigidList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ List rigidList = model.getRigidList(); int rigidNum = rigidList.size(); dumpInt(rigidNum); @@ -89,7 +94,7 @@ public class PmdExporterExt3 extends PmdExporterExt2{ * @throws IllegalPmdTextException 長すぎる剛体名 */ private void dumpRigid(RigidInfo rigid) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ String rigidName = rigid.getRigidName().getPrimaryText(); dumpText(rigidName, PmdLimits.MAXBYTES_RIGIDNAME); @@ -172,7 +177,7 @@ public class PmdExporterExt3 extends PmdExporterExt2{ * @throws IllegalPmdTextException 長すぎるジョイント名 */ private void dumpJointList(PmdModel model) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ List jointList = model.getJointList(); int jointNum = jointList.size(); dumpInt(jointNum); @@ -193,7 +198,7 @@ public class PmdExporterExt3 extends PmdExporterExt2{ * @throws IllegalPmdTextException 長すぎるジョイント名 */ private void dumpJoint(JointInfo joint) - throws IOException, IllegalPmdTextException{ + throws IOException, IllegalTextExportException{ String jointName = joint.getJointName().getPrimaryText(); dumpText(jointName, PmdLimits.MAXBYTES_JOINTNAME);