1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 public final class WordMode extends AbstractMode implements Constants, Mode 29 { 30 private static final WordMode mode = new WordMode(); 31 32 private WordMode() 33 { 34 super(WORD_MODE, WORD_MODE_NAME); 35 setProperty(Property.VERTICAL_RULE, 0); 36 setProperty(Property.SHOW_LINE_NUMBERS, false); 37 } 38 39 public static final WordMode getMode() 40 { 41 return mode; 42 } 43 44 public final Formatter getFormatter(Buffer buffer) 45 { 46 return new PlainTextFormatter(buffer); 47 } 48 49 protected void setKeyMapDefaults(KeyMap km) 50 { 51 km.mapKey(KeyEvent.VK_B, CTRL_MASK | ALT_MASK, "binaryMode"); 52 } 53 54 public void loadFile(Buffer buffer, File toBeLoaded) 55 { 56 try { 57 load(buffer, toBeLoaded.getInputStream()); 58 } 59 catch (IOException e) { 60 Log.debug(e); 61 } 62 } 63 64 private static void load(Buffer buffer, InputStream inputStream) 65 { 66 byte[] bytes = new byte[4096]; 67 int totalBytes = 0; 68 try { 69 int bytesRead = inputStream.read(bytes); 70 if (bytesRead < 0x400) 71 return; 72 int magic = getWord(bytes, 0x200); 73 Log.debug("magic = 0x" + Integer.toHexString(magic)); 74 if (magic != 0xa5ec) 75 return; 76 int version = getWord(bytes, 0x202); 77 Log.debug("version = " + version); 78 if (version < 101) 79 return; 80 int status = getWord(bytes, 0x20a); 81 Log.debug("status = 0x" + Integer.toHexString(status)); 82 if ((status & 4) != 0) 83 Log.debug("document is fast saved"); 84 long beginText = getLong(bytes, 0x218) + 0x200; 85 Log.debug("beginText = 0x" + Long.toHexString(beginText)); 86 if (beginText > bytesRead) 87 return; long endText = getLong(bytes, 0x21c) + 0x200; 89 long textLength = endText - beginText; 90 Log.debug("textLength = " + textLength); 91 final String encoding = "Cp1252"; 92 ByteBuffer bb = new ByteBuffer(256); 93 boolean done = false; 94 Debug.assertTrue(beginText < Integer.MAX_VALUE); 95 int start = (int) beginText; 96 while (bytesRead > 0) { 97 for (int i = start; i < bytesRead; i++) { 98 byte b = bytes[i]; 99 switch (b) { 100 case 13: 101 if (bb.length() > 0) 102 wrapAndAppend(buffer, new String (bb.getBytes(), 0, bb.length(), encoding)); 103 buffer.appendLine(""); 104 bb.setLength(0); 105 break; 106 case 0: 107 if (bb.length() > 0) 108 wrapAndAppend(buffer, new String (bb.getBytes(), 0, bb.length(), encoding)); 109 bb.setLength(0); 110 done = true; 111 break; 112 default: 113 bb.append(b); 115 break; 116 } 117 if (done) 118 break; 119 } 120 if (done) 121 break; 122 bytesRead = inputStream.read(bytes); 123 start = 0; 124 if (bytesRead > 0) 125 buffer.loadProgress(totalBytes = totalBytes + bytesRead); 126 } 127 buffer.unmodified(); 128 buffer.setLoaded(true); 129 buffer.setForceReadOnly(true); 130 } 131 catch (Exception e) { 132 Log.error(e); 133 } 134 buffer.loadFinished(buffer.isLoaded()); 135 } 136 137 private static final void wrapAndAppend(Buffer buffer, String s) 138 { 139 buffer.append(Utilities.wrap(s, 65, 8)); 140 } 141 142 private static long getLong(byte[] bytes, int offset) 144 { 145 long byte0 = bytes[offset] & 0xff; 146 long byte1 = bytes[offset + 1] & 0xff; 147 long byte2 = bytes[offset + 2] & 0xff; 148 long byte3 = bytes[offset + 3] & 0xff; 149 return (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0; 150 } 151 152 private static int getWord(byte[] bytes, int offset) 154 { 155 int hi = bytes[offset + 1] & 0xff; 156 int lo = bytes[offset] & 0xff; 157 return (hi << 8) + lo; 158 } 159 } 160 | Popular Tags |