You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColumnImpl.java 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. /*
  2. Copyright (c) 2005 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.ObjectOutputStream;
  18. import java.io.ObjectStreamException;
  19. import java.io.Reader;
  20. import java.io.Serializable;
  21. import java.math.BigDecimal;
  22. import java.math.BigInteger;
  23. import java.nio.ByteBuffer;
  24. import java.nio.ByteOrder;
  25. import java.nio.CharBuffer;
  26. import java.nio.charset.Charset;
  27. import java.sql.Blob;
  28. import java.sql.Clob;
  29. import java.sql.SQLException;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.UUID;
  35. import java.util.regex.Matcher;
  36. import java.util.regex.Pattern;
  37. import com.healthmarketscience.jackcess.Column;
  38. import com.healthmarketscience.jackcess.ColumnBuilder;
  39. import com.healthmarketscience.jackcess.DataType;
  40. import com.healthmarketscience.jackcess.PropertyMap;
  41. import com.healthmarketscience.jackcess.Table;
  42. import com.healthmarketscience.jackcess.complex.ComplexColumnInfo;
  43. import com.healthmarketscience.jackcess.complex.ComplexValue;
  44. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  45. import com.healthmarketscience.jackcess.impl.complex.ComplexValueForeignKeyImpl;
  46. import com.healthmarketscience.jackcess.util.ColumnValidator;
  47. import com.healthmarketscience.jackcess.util.SimpleColumnValidator;
  48. import org.apache.commons.lang.builder.ToStringBuilder;
  49. import org.apache.commons.logging.Log;
  50. import org.apache.commons.logging.LogFactory;
  51. /**
  52. * Access database column definition
  53. * @author Tim McCune
  54. * @usage _general_class_
  55. */
  56. public class ColumnImpl implements Column, Comparable<ColumnImpl> {
  57. protected static final Log LOG = LogFactory.getLog(ColumnImpl.class);
  58. /**
  59. * Placeholder object for adding rows which indicates that the caller wants
  60. * the RowId of the new row. Must be added as an extra value at the end of
  61. * the row values array.
  62. * @see TableImpl#asRowWithRowId
  63. * @usage _intermediate_field_
  64. */
  65. public static final Object RETURN_ROW_ID = "<RETURN_ROW_ID>";
  66. /**
  67. * Access stores numeric dates in days. Java stores them in milliseconds.
  68. */
  69. private static final double MILLISECONDS_PER_DAY =
  70. (24L * 60L * 60L * 1000L);
  71. /**
  72. * Access starts counting dates at Jan 1, 1900. Java starts counting
  73. * at Jan 1, 1970. This is the # of millis between them for conversion.
  74. */
  75. private static final long MILLIS_BETWEEN_EPOCH_AND_1900 =
  76. 25569L * (long)MILLISECONDS_PER_DAY;
  77. /**
  78. * mask for the fixed len bit
  79. * @usage _advanced_field_
  80. */
  81. public static final byte FIXED_LEN_FLAG_MASK = (byte)0x01;
  82. /**
  83. * mask for the auto number bit
  84. * @usage _advanced_field_
  85. */
  86. public static final byte AUTO_NUMBER_FLAG_MASK = (byte)0x04;
  87. /**
  88. * mask for the auto number guid bit
  89. * @usage _advanced_field_
  90. */
  91. public static final byte AUTO_NUMBER_GUID_FLAG_MASK = (byte)0x40;
  92. /**
  93. * mask for the hyperlink bit (on memo types)
  94. * @usage _advanced_field_
  95. */
  96. public static final byte HYPERLINK_FLAG_MASK = (byte)0x80;
  97. /**
  98. * mask for the unknown bit (possible "can be null"?)
  99. * @usage _advanced_field_
  100. */
  101. public static final byte UNKNOWN_FLAG_MASK = (byte)0x02;
  102. // some other flags?
  103. // 0x10: replication related field (or hidden?)
  104. protected static final byte COMPRESSED_UNICODE_EXT_FLAG_MASK = (byte)0x01;
  105. private static final byte CALCULATED_EXT_FLAG_MASK = (byte)0xC0;
  106. static final byte NUMERIC_NEGATIVE_BYTE = (byte)0x80;
  107. /** the value for the "general" sort order */
  108. private static final short GENERAL_SORT_ORDER_VALUE = 1033;
  109. /**
  110. * the "general" text sort order, legacy version (access 2000-2007)
  111. * @usage _intermediate_field_
  112. */
  113. public static final SortOrder GENERAL_LEGACY_SORT_ORDER =
  114. new SortOrder(GENERAL_SORT_ORDER_VALUE, (byte)0);
  115. /**
  116. * the "general" text sort order, latest version (access 2010+)
  117. * @usage _intermediate_field_
  118. */
  119. public static final SortOrder GENERAL_SORT_ORDER =
  120. new SortOrder(GENERAL_SORT_ORDER_VALUE, (byte)1);
  121. /** pattern matching textual guid strings (allows for optional surrounding
  122. '{' and '}') */
  123. private static final Pattern GUID_PATTERN = Pattern.compile("\\s*[{]?([\\p{XDigit}]{8})-([\\p{XDigit}]{4})-([\\p{XDigit}]{4})-([\\p{XDigit}]{4})-([\\p{XDigit}]{12})[}]?\\s*");
  124. /** header used to indicate unicode text compression */
  125. private static final byte[] TEXT_COMPRESSION_HEADER =
  126. { (byte)0xFF, (byte)0XFE };
  127. private static final char MIN_COMPRESS_CHAR = 1;
  128. private static final char MAX_COMPRESS_CHAR = 0xFF;
  129. /** owning table */
  130. private final TableImpl _table;
  131. /** Whether or not the column is of variable length */
  132. private final boolean _variableLength;
  133. /** Whether or not the column is an autonumber column */
  134. private final boolean _autoNumber;
  135. /** Whether or not the column is a calculated column */
  136. private final boolean _calculated;
  137. /** Data type */
  138. private final DataType _type;
  139. /** Maximum column length */
  140. private final short _columnLength;
  141. /** 0-based column number */
  142. private final short _columnNumber;
  143. /** index of the data for this column within a list of row data */
  144. private int _columnIndex;
  145. /** display index of the data for this column */
  146. private final int _displayIndex;
  147. /** Column name */
  148. private final String _name;
  149. /** the offset of the fixed data in the row */
  150. private final int _fixedDataOffset;
  151. /** the index of the variable length data in the var len offset table */
  152. private final int _varLenTableIndex;
  153. /** the auto number generator for this column (if autonumber column) */
  154. private final AutoNumberGenerator _autoNumberGenerator;
  155. /** properties for this column, if any */
  156. private PropertyMap _props;
  157. /** Validator for writing new values */
  158. private ColumnValidator _validator = SimpleColumnValidator.INSTANCE;
  159. /**
  160. * @usage _advanced_method_
  161. */
  162. protected ColumnImpl(TableImpl table, String name, DataType type,
  163. int colNumber, int fixedOffset, int varLenIndex) {
  164. _table = table;
  165. _name = name;
  166. _type = type;
  167. if(!_type.isVariableLength()) {
  168. _columnLength = (short)type.getFixedSize();
  169. } else {
  170. _columnLength = (short)type.getMaxSize();
  171. }
  172. _variableLength = type.isVariableLength();
  173. _autoNumber = false;
  174. _calculated = false;
  175. _autoNumberGenerator = null;
  176. _columnNumber = (short)colNumber;
  177. _columnIndex = colNumber;
  178. _displayIndex = colNumber;
  179. _fixedDataOffset = fixedOffset;
  180. _varLenTableIndex = varLenIndex;
  181. }
  182. /**
  183. * Read a column definition in from a buffer
  184. * @usage _advanced_method_
  185. */
  186. ColumnImpl(InitArgs args)
  187. throws IOException
  188. {
  189. _table = args.table;
  190. _name = args.name;
  191. _displayIndex = args.displayIndex;
  192. _type = args.type;
  193. _columnNumber = args.buffer.getShort(
  194. args.offset + getFormat().OFFSET_COLUMN_NUMBER);
  195. _columnLength = args.buffer.getShort(
  196. args.offset + getFormat().OFFSET_COLUMN_LENGTH);
  197. _variableLength = ((args.flags & FIXED_LEN_FLAG_MASK) == 0);
  198. _autoNumber = ((args.flags &
  199. (AUTO_NUMBER_FLAG_MASK | AUTO_NUMBER_GUID_FLAG_MASK)) != 0);
  200. _calculated = ((args.extFlags & CALCULATED_EXT_FLAG_MASK) != 0);
  201. _autoNumberGenerator = createAutoNumberGenerator();
  202. if(_variableLength) {
  203. _varLenTableIndex = args.buffer.getShort(
  204. args.offset + getFormat().OFFSET_COLUMN_VARIABLE_TABLE_INDEX);
  205. _fixedDataOffset = 0;
  206. } else {
  207. _fixedDataOffset = args.buffer.getShort(
  208. args.offset + getFormat().OFFSET_COLUMN_FIXED_DATA_OFFSET);
  209. _varLenTableIndex = 0;
  210. }
  211. }
  212. /**
  213. * Creates the appropriate ColumnImpl class and reads a column definition in
  214. * from a buffer
  215. * @param table owning table
  216. * @param buffer Buffer containing column definition
  217. * @param offset Offset in the buffer at which the column definition starts
  218. * @usage _advanced_method_
  219. */
  220. public static ColumnImpl create(TableImpl table, ByteBuffer buffer,
  221. int offset, String name, int displayIndex)
  222. throws IOException
  223. {
  224. InitArgs args = new InitArgs(table, buffer, offset, name, displayIndex);
  225. boolean calculated = ((args.extFlags & CALCULATED_EXT_FLAG_MASK) != 0);
  226. byte colType = args.colType;
  227. if(calculated) {
  228. // "real" data type is in the "result type" property
  229. PropertyMap colProps = table.getPropertyMaps().get(name);
  230. Byte resultType = (Byte)colProps.getValue(PropertyMap.RESULT_TYPE_PROP);
  231. if(resultType != null) {
  232. colType = resultType;
  233. }
  234. }
  235. try {
  236. args.type = DataType.fromByte(colType);
  237. } catch(IOException e) {
  238. LOG.warn(withErrorContext("Unsupported column type " + colType,
  239. table.getDatabase(), table.getName(), name));
  240. boolean variableLength = ((args.flags & FIXED_LEN_FLAG_MASK) == 0);
  241. args.type = (variableLength ? DataType.UNSUPPORTED_VARLEN :
  242. DataType.UNSUPPORTED_FIXEDLEN);
  243. return new UnsupportedColumnImpl(args);
  244. }
  245. if(calculated) {
  246. return CalculatedColumnUtil.create(args);
  247. }
  248. switch(args.type) {
  249. case TEXT:
  250. return new TextColumnImpl(args);
  251. case MEMO:
  252. return new MemoColumnImpl(args);
  253. case COMPLEX_TYPE:
  254. return new ComplexColumnImpl(args);
  255. default:
  256. // fall through
  257. }
  258. if(args.type.getHasScalePrecision()) {
  259. return new NumericColumnImpl(args);
  260. }
  261. if(args.type.isLongValue()) {
  262. return new LongValueColumnImpl(args);
  263. }
  264. return new ColumnImpl(args);
  265. }
  266. /**
  267. * Sets the usage maps for this column.
  268. */
  269. void setUsageMaps(UsageMap ownedPages, UsageMap freeSpacePages) {
  270. // base does nothing
  271. }
  272. /**
  273. * Secondary column initialization after the table is fully loaded.
  274. */
  275. void postTableLoadInit() throws IOException {
  276. // base does nothing
  277. }
  278. public TableImpl getTable() {
  279. return _table;
  280. }
  281. public DatabaseImpl getDatabase() {
  282. return getTable().getDatabase();
  283. }
  284. /**
  285. * @usage _advanced_method_
  286. */
  287. public JetFormat getFormat() {
  288. return getDatabase().getFormat();
  289. }
  290. /**
  291. * @usage _advanced_method_
  292. */
  293. public PageChannel getPageChannel() {
  294. return getDatabase().getPageChannel();
  295. }
  296. public String getName() {
  297. return _name;
  298. }
  299. public boolean isVariableLength() {
  300. return _variableLength;
  301. }
  302. public boolean isAutoNumber() {
  303. return _autoNumber;
  304. }
  305. /**
  306. * @usage _advanced_method_
  307. */
  308. public short getColumnNumber() {
  309. return _columnNumber;
  310. }
  311. public int getColumnIndex() {
  312. return _columnIndex;
  313. }
  314. /**
  315. * @usage _advanced_method_
  316. */
  317. public void setColumnIndex(int newColumnIndex) {
  318. _columnIndex = newColumnIndex;
  319. }
  320. /**
  321. * @usage _advanced_method_
  322. */
  323. public int getDisplayIndex() {
  324. return _displayIndex;
  325. }
  326. public DataType getType() {
  327. return _type;
  328. }
  329. public int getSQLType() throws SQLException {
  330. return _type.getSQLType();
  331. }
  332. public boolean isCompressedUnicode() {
  333. return false;
  334. }
  335. public byte getPrecision() {
  336. return (byte)getType().getDefaultPrecision();
  337. }
  338. public byte getScale() {
  339. return (byte)getType().getDefaultScale();
  340. }
  341. /**
  342. * @usage _intermediate_method_
  343. */
  344. public SortOrder getTextSortOrder() {
  345. return null;
  346. }
  347. /**
  348. * @usage _intermediate_method_
  349. */
  350. public short getTextCodePage() {
  351. return 0;
  352. }
  353. public short getLength() {
  354. return _columnLength;
  355. }
  356. public short getLengthInUnits() {
  357. return (short)getType().toUnitSize(getLength());
  358. }
  359. public boolean isCalculated() {
  360. return _calculated;
  361. }
  362. /**
  363. * @usage _advanced_method_
  364. */
  365. public int getVarLenTableIndex() {
  366. return _varLenTableIndex;
  367. }
  368. /**
  369. * @usage _advanced_method_
  370. */
  371. public int getFixedDataOffset() {
  372. return _fixedDataOffset;
  373. }
  374. protected Charset getCharset() {
  375. return getDatabase().getCharset();
  376. }
  377. protected Calendar getCalendar() {
  378. return getDatabase().getCalendar();
  379. }
  380. public boolean isAppendOnly() {
  381. return (getVersionHistoryColumn() != null);
  382. }
  383. public ColumnImpl getVersionHistoryColumn() {
  384. return null;
  385. }
  386. /**
  387. * Returns the number of database pages owned by this column.
  388. * @usage _intermediate_method_
  389. */
  390. public int getOwnedPageCount() {
  391. return 0;
  392. }
  393. /**
  394. * @usage _advanced_method_
  395. */
  396. public void setVersionHistoryColumn(ColumnImpl versionHistoryCol) {
  397. throw new UnsupportedOperationException();
  398. }
  399. public boolean isHyperlink() {
  400. return false;
  401. }
  402. public ComplexColumnInfo<? extends ComplexValue> getComplexInfo() {
  403. return null;
  404. }
  405. public ColumnValidator getColumnValidator() {
  406. return _validator;
  407. }
  408. public void setColumnValidator(ColumnValidator newValidator) {
  409. if(isAutoNumber()) {
  410. // cannot set autonumber validator (autonumber values are controlled
  411. // internally)
  412. if(newValidator != null) {
  413. throw new IllegalArgumentException(withErrorContext(
  414. "Cannot set ColumnValidator for autonumber columns"));
  415. }
  416. // just leave default validator instance alone
  417. return;
  418. }
  419. if(newValidator == null) {
  420. newValidator = getDatabase().getColumnValidatorFactory()
  421. .createValidator(this);
  422. if(newValidator == null) {
  423. newValidator = SimpleColumnValidator.INSTANCE;
  424. }
  425. }
  426. _validator = newValidator;
  427. }
  428. byte getOriginalDataType() {
  429. return _type.getValue();
  430. }
  431. private AutoNumberGenerator createAutoNumberGenerator() {
  432. if(!_autoNumber || (_type == null)) {
  433. return null;
  434. }
  435. switch(_type) {
  436. case LONG:
  437. return new LongAutoNumberGenerator();
  438. case GUID:
  439. return new GuidAutoNumberGenerator();
  440. case COMPLEX_TYPE:
  441. return new ComplexTypeAutoNumberGenerator();
  442. default:
  443. LOG.warn(withErrorContext("Unknown auto number column type " + _type));
  444. return new UnsupportedAutoNumberGenerator(_type);
  445. }
  446. }
  447. /**
  448. * Returns the AutoNumberGenerator for this column if this is an autonumber
  449. * column, {@code null} otherwise.
  450. * @usage _advanced_method_
  451. */
  452. public AutoNumberGenerator getAutoNumberGenerator() {
  453. return _autoNumberGenerator;
  454. }
  455. public PropertyMap getProperties() throws IOException {
  456. if(_props == null) {
  457. _props = getTable().getPropertyMaps().get(getName());
  458. }
  459. return _props;
  460. }
  461. public Object setRowValue(Object[] rowArray, Object value) {
  462. rowArray[_columnIndex] = value;
  463. return value;
  464. }
  465. public Object setRowValue(Map<String,Object> rowMap, Object value) {
  466. rowMap.put(_name, value);
  467. return value;
  468. }
  469. public Object getRowValue(Object[] rowArray) {
  470. return rowArray[_columnIndex];
  471. }
  472. public Object getRowValue(Map<String,?> rowMap) {
  473. return rowMap.get(_name);
  474. }
  475. public boolean storeInNullMask() {
  476. return (getType() == DataType.BOOLEAN);
  477. }
  478. public boolean writeToNullMask(Object value) {
  479. return toBooleanValue(value);
  480. }
  481. public Object readFromNullMask(boolean isNull) {
  482. return Boolean.valueOf(!isNull);
  483. }
  484. /**
  485. * Deserialize a raw byte value for this column into an Object
  486. * @param data The raw byte value
  487. * @return The deserialized Object
  488. * @usage _advanced_method_
  489. */
  490. public Object read(byte[] data) throws IOException {
  491. return read(data, PageChannel.DEFAULT_BYTE_ORDER);
  492. }
  493. /**
  494. * Deserialize a raw byte value for this column into an Object
  495. * @param data The raw byte value
  496. * @param order Byte order in which the raw value is stored
  497. * @return The deserialized Object
  498. * @usage _advanced_method_
  499. */
  500. public Object read(byte[] data, ByteOrder order) throws IOException {
  501. ByteBuffer buffer = ByteBuffer.wrap(data).order(order);
  502. switch(getType()) {
  503. case BOOLEAN:
  504. throw new IOException(withErrorContext("Tried to read a boolean from data instead of null mask."));
  505. case BYTE:
  506. return Byte.valueOf(buffer.get());
  507. case INT:
  508. return Short.valueOf(buffer.getShort());
  509. case LONG:
  510. return Integer.valueOf(buffer.getInt());
  511. case DOUBLE:
  512. return Double.valueOf(buffer.getDouble());
  513. case FLOAT:
  514. return Float.valueOf(buffer.getFloat());
  515. case SHORT_DATE_TIME:
  516. return readDateValue(buffer);
  517. case BINARY:
  518. return data;
  519. case TEXT:
  520. return decodeTextValue(data);
  521. case MONEY:
  522. return readCurrencyValue(buffer);
  523. case NUMERIC:
  524. return readNumericValue(buffer);
  525. case GUID:
  526. return readGUIDValue(buffer, order);
  527. case UNKNOWN_0D:
  528. case UNKNOWN_11:
  529. // treat like "binary" data
  530. return data;
  531. case COMPLEX_TYPE:
  532. return new ComplexValueForeignKeyImpl(this, buffer.getInt());
  533. default:
  534. throw new IOException(withErrorContext("Unrecognized data type: " + _type));
  535. }
  536. }
  537. /**
  538. * Decodes "Currency" values.
  539. *
  540. * @param buffer Column value that points to currency data
  541. * @return BigDecimal representing the monetary value
  542. * @throws IOException if the value cannot be parsed
  543. */
  544. private BigDecimal readCurrencyValue(ByteBuffer buffer)
  545. throws IOException
  546. {
  547. if(buffer.remaining() != 8) {
  548. throw new IOException(withErrorContext("Invalid money value"));
  549. }
  550. return new BigDecimal(BigInteger.valueOf(buffer.getLong(0)), 4);
  551. }
  552. /**
  553. * Writes "Currency" values.
  554. */
  555. private void writeCurrencyValue(ByteBuffer buffer, Object value)
  556. throws IOException
  557. {
  558. Object inValue = value;
  559. try {
  560. BigDecimal decVal = toBigDecimal(value);
  561. inValue = decVal;
  562. // adjust scale (will cause the an ArithmeticException if number has too
  563. // many decimal places)
  564. decVal = decVal.setScale(4);
  565. // now, remove scale and convert to long (this will throw if the value is
  566. // too big)
  567. buffer.putLong(decVal.movePointRight(4).longValueExact());
  568. } catch(ArithmeticException e) {
  569. throw (IOException)
  570. new IOException(withErrorContext(
  571. "Currency value '" + inValue + "' out of range"))
  572. .initCause(e);
  573. }
  574. }
  575. /**
  576. * Decodes a NUMERIC field.
  577. */
  578. private BigDecimal readNumericValue(ByteBuffer buffer)
  579. {
  580. boolean negate = (buffer.get() != 0);
  581. byte[] tmpArr = ByteUtil.getBytes(buffer, 16);
  582. if(buffer.order() != ByteOrder.BIG_ENDIAN) {
  583. fixNumericByteOrder(tmpArr);
  584. }
  585. return toBigDecimal(tmpArr, negate, getScale());
  586. }
  587. static BigDecimal toBigDecimal(byte[] bytes, boolean negate, int scale)
  588. {
  589. if((bytes[0] & 0x80) != 0) {
  590. // the data is effectively unsigned, but the BigInteger handles it as
  591. // signed twos complement. we need to add an extra byte to the input so
  592. // that it will be treated as unsigned
  593. bytes = ByteUtil.copyOf(bytes, 0, bytes.length + 1, 1);
  594. }
  595. BigInteger intVal = new BigInteger(bytes);
  596. if(negate) {
  597. intVal = intVal.negate();
  598. }
  599. return new BigDecimal(intVal, scale);
  600. }
  601. /**
  602. * Writes a numeric value.
  603. */
  604. private void writeNumericValue(ByteBuffer buffer, Object value)
  605. throws IOException
  606. {
  607. Object inValue = value;
  608. try {
  609. BigDecimal decVal = toBigDecimal(value);
  610. inValue = decVal;
  611. int signum = decVal.signum();
  612. if(signum < 0) {
  613. decVal = decVal.negate();
  614. }
  615. // write sign byte
  616. buffer.put((signum < 0) ? NUMERIC_NEGATIVE_BYTE : 0);
  617. // adjust scale according to this column type (will cause the an
  618. // ArithmeticException if number has too many decimal places)
  619. decVal = decVal.setScale(getScale());
  620. // check precision
  621. if(decVal.precision() > getPrecision()) {
  622. throw new IOException(withErrorContext(
  623. "Numeric value is too big for specified precision "
  624. + getPrecision() + ": " + decVal));
  625. }
  626. // convert to unscaled BigInteger, big-endian bytes
  627. byte[] intValBytes = toUnscaledByteArray(
  628. decVal, getType().getFixedSize() - 1);
  629. if(buffer.order() != ByteOrder.BIG_ENDIAN) {
  630. fixNumericByteOrder(intValBytes);
  631. }
  632. buffer.put(intValBytes);
  633. } catch(ArithmeticException e) {
  634. throw (IOException)
  635. new IOException(withErrorContext(
  636. "Numeric value '" + inValue + "' out of range"))
  637. .initCause(e);
  638. }
  639. }
  640. byte[] toUnscaledByteArray(BigDecimal decVal, int maxByteLen)
  641. throws IOException
  642. {
  643. // convert to unscaled BigInteger, big-endian bytes
  644. byte[] intValBytes = decVal.unscaledValue().toByteArray();
  645. if(intValBytes.length > maxByteLen) {
  646. if((intValBytes[0] == 0) && ((intValBytes.length - 1) == maxByteLen)) {
  647. // in order to not return a negative two's complement value,
  648. // toByteArray() may return an extra leading 0 byte. we are working
  649. // with unsigned values, so we can drop the extra leading 0
  650. intValBytes = ByteUtil.copyOf(intValBytes, 1, maxByteLen);
  651. } else {
  652. throw new IOException(withErrorContext(
  653. "Too many bytes for valid BigInteger?"));
  654. }
  655. } else if(intValBytes.length < maxByteLen) {
  656. intValBytes = ByteUtil.copyOf(intValBytes, 0, maxByteLen,
  657. (maxByteLen - intValBytes.length));
  658. }
  659. return intValBytes;
  660. }
  661. /**
  662. * Decodes a date value.
  663. */
  664. private Date readDateValue(ByteBuffer buffer)
  665. {
  666. // seems access stores dates in the local timezone. guess you just hope
  667. // you read it in the same timezone in which it was written!
  668. long dateBits = buffer.getLong();
  669. long time = fromDateDouble(Double.longBitsToDouble(dateBits));
  670. return new DateExt(time, dateBits);
  671. }
  672. /**
  673. * Returns a java long time value converted from an access date double.
  674. * @usage _advanced_method_
  675. */
  676. public long fromDateDouble(double value)
  677. {
  678. long time = Math.round(value * MILLISECONDS_PER_DAY);
  679. time -= MILLIS_BETWEEN_EPOCH_AND_1900;
  680. time -= getFromLocalTimeZoneOffset(time);
  681. return time;
  682. }
  683. /**
  684. * Writes a date value.
  685. */
  686. private void writeDateValue(ByteBuffer buffer, Object value)
  687. {
  688. if(value == null) {
  689. buffer.putDouble(0d);
  690. } else if(value instanceof DateExt) {
  691. // this is a Date value previously read from readDateValue(). use the
  692. // original bits to store the value so we don't lose any precision
  693. buffer.putLong(((DateExt)value).getDateBits());
  694. } else {
  695. buffer.putDouble(toDateDouble(value));
  696. }
  697. }
  698. /**
  699. * Returns an access date double converted from a java Date/Calendar/Number
  700. * time value.
  701. * @usage _advanced_method_
  702. */
  703. public double toDateDouble(Object value)
  704. {
  705. // seems access stores dates in the local timezone. guess you just
  706. // hope you read it in the same timezone in which it was written!
  707. long time = toDateLong(value);
  708. time += getToLocalTimeZoneOffset(time);
  709. time += MILLIS_BETWEEN_EPOCH_AND_1900;
  710. return time / MILLISECONDS_PER_DAY;
  711. }
  712. /**
  713. * @return an appropriate Date long value for the given object
  714. */
  715. private static long toDateLong(Object value)
  716. {
  717. return ((value instanceof Date) ?
  718. ((Date)value).getTime() :
  719. ((value instanceof Calendar) ?
  720. ((Calendar)value).getTimeInMillis() :
  721. ((Number)value).longValue()));
  722. }
  723. /**
  724. * Gets the timezone offset from UTC to local time for the given time
  725. * (including DST).
  726. */
  727. private long getToLocalTimeZoneOffset(long time)
  728. {
  729. Calendar c = getCalendar();
  730. c.setTimeInMillis(time);
  731. return ((long)c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET));
  732. }
  733. /**
  734. * Gets the timezone offset from local time to UTC for the given time
  735. * (including DST).
  736. */
  737. private long getFromLocalTimeZoneOffset(long time)
  738. {
  739. // getting from local time back to UTC is a little wonky (and not
  740. // guaranteed to get you back to where you started)
  741. Calendar c = getCalendar();
  742. c.setTimeInMillis(time);
  743. // apply the zone offset first to get us closer to the original time
  744. c.setTimeInMillis(time - c.get(Calendar.ZONE_OFFSET));
  745. return ((long)c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET));
  746. }
  747. /**
  748. * Decodes a GUID value.
  749. */
  750. private static String readGUIDValue(ByteBuffer buffer, ByteOrder order)
  751. {
  752. if(order != ByteOrder.BIG_ENDIAN) {
  753. byte[] tmpArr = ByteUtil.getBytes(buffer, 16);
  754. // the first 3 guid components are integer components which need to
  755. // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
  756. ByteUtil.swap4Bytes(tmpArr, 0);
  757. ByteUtil.swap2Bytes(tmpArr, 4);
  758. ByteUtil.swap2Bytes(tmpArr, 6);
  759. buffer = ByteBuffer.wrap(tmpArr);
  760. }
  761. StringBuilder sb = new StringBuilder(22);
  762. sb.append("{");
  763. sb.append(ByteUtil.toHexString(buffer, 0, 4,
  764. false));
  765. sb.append("-");
  766. sb.append(ByteUtil.toHexString(buffer, 4, 2,
  767. false));
  768. sb.append("-");
  769. sb.append(ByteUtil.toHexString(buffer, 6, 2,
  770. false));
  771. sb.append("-");
  772. sb.append(ByteUtil.toHexString(buffer, 8, 2,
  773. false));
  774. sb.append("-");
  775. sb.append(ByteUtil.toHexString(buffer, 10, 6,
  776. false));
  777. sb.append("}");
  778. return (sb.toString());
  779. }
  780. /**
  781. * Writes a GUID value.
  782. */
  783. private void writeGUIDValue(ByteBuffer buffer, Object value)
  784. throws IOException
  785. {
  786. Matcher m = GUID_PATTERN.matcher(toCharSequence(value));
  787. if(!m.matches()) {
  788. throw new IOException(withErrorContext("Invalid GUID: " + value));
  789. }
  790. ByteBuffer origBuffer = null;
  791. byte[] tmpBuf = null;
  792. if(buffer.order() != ByteOrder.BIG_ENDIAN) {
  793. // write to a temp buf so we can do some swapping below
  794. origBuffer = buffer;
  795. tmpBuf = new byte[16];
  796. buffer = ByteBuffer.wrap(tmpBuf);
  797. }
  798. ByteUtil.writeHexString(buffer, m.group(1));
  799. ByteUtil.writeHexString(buffer, m.group(2));
  800. ByteUtil.writeHexString(buffer, m.group(3));
  801. ByteUtil.writeHexString(buffer, m.group(4));
  802. ByteUtil.writeHexString(buffer, m.group(5));
  803. if(tmpBuf != null) {
  804. // the first 3 guid components are integer components which need to
  805. // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
  806. ByteUtil.swap4Bytes(tmpBuf, 0);
  807. ByteUtil.swap2Bytes(tmpBuf, 4);
  808. ByteUtil.swap2Bytes(tmpBuf, 6);
  809. origBuffer.put(tmpBuf);
  810. }
  811. }
  812. /**
  813. * Returns {@code true} if the given value is a "guid" value.
  814. */
  815. static boolean isGUIDValue(Object value) throws IOException {
  816. return GUID_PATTERN.matcher(toCharSequence(value)).matches();
  817. }
  818. /**
  819. * Passes the given obj through the currently configured validator for this
  820. * column and returns the result.
  821. */
  822. public Object validate(Object obj) throws IOException {
  823. return _validator.validate(this, obj);
  824. }
  825. /**
  826. * Serialize an Object into a raw byte value for this column in little
  827. * endian order
  828. * @param obj Object to serialize
  829. * @return A buffer containing the bytes
  830. * @usage _advanced_method_
  831. */
  832. public ByteBuffer write(Object obj, int remainingRowLength)
  833. throws IOException
  834. {
  835. return write(obj, remainingRowLength, PageChannel.DEFAULT_BYTE_ORDER);
  836. }
  837. /**
  838. * Serialize an Object into a raw byte value for this column
  839. * @param obj Object to serialize
  840. * @param order Order in which to serialize
  841. * @return A buffer containing the bytes
  842. * @usage _advanced_method_
  843. */
  844. public ByteBuffer write(Object obj, int remainingRowLength, ByteOrder order)
  845. throws IOException
  846. {
  847. if(isRawData(obj)) {
  848. // just slap it right in (not for the faint of heart!)
  849. return ByteBuffer.wrap(((RawData)obj).getBytes());
  850. }
  851. return writeRealData(obj, remainingRowLength, order);
  852. }
  853. protected ByteBuffer writeRealData(Object obj, int remainingRowLength,
  854. ByteOrder order)
  855. throws IOException
  856. {
  857. if(!isVariableLength() || !getType().isVariableLength()) {
  858. return writeFixedLengthField(obj, order);
  859. }
  860. // this is an "inline" var length field
  861. switch(getType()) {
  862. case NUMERIC:
  863. // don't ask me why numerics are "var length" columns...
  864. ByteBuffer buffer = PageChannel.createBuffer(
  865. getType().getFixedSize(), order);
  866. writeNumericValue(buffer, obj);
  867. buffer.flip();
  868. return buffer;
  869. case TEXT:
  870. return encodeTextValue(
  871. obj, 0, getLengthInUnits(), false).order(order);
  872. case BINARY:
  873. case UNKNOWN_0D:
  874. case UNSUPPORTED_VARLEN:
  875. // should already be "encoded"
  876. break;
  877. default:
  878. throw new RuntimeException(withErrorContext(
  879. "unexpected inline var length type: " + getType()));
  880. }
  881. ByteBuffer buffer = ByteBuffer.wrap(toByteArray(obj)).order(order);
  882. return buffer;
  883. }
  884. /**
  885. * Serialize an Object into a raw byte value for this column
  886. * @param obj Object to serialize
  887. * @param order Order in which to serialize
  888. * @return A buffer containing the bytes
  889. * @usage _advanced_method_
  890. */
  891. protected ByteBuffer writeFixedLengthField(Object obj, ByteOrder order)
  892. throws IOException
  893. {
  894. int size = getType().getFixedSize(_columnLength);
  895. ByteBuffer buffer = writeFixedLengthField(
  896. obj, PageChannel.createBuffer(size, order));
  897. buffer.flip();
  898. return buffer;
  899. }
  900. protected ByteBuffer writeFixedLengthField(Object obj, ByteBuffer buffer)
  901. throws IOException
  902. {
  903. // since booleans are not written by this method, it's safe to convert any
  904. // incoming boolean into an integer.
  905. obj = booleanToInteger(obj);
  906. switch(getType()) {
  907. case BOOLEAN:
  908. //Do nothing
  909. break;
  910. case BYTE:
  911. buffer.put(toNumber(obj).byteValue());
  912. break;
  913. case INT:
  914. buffer.putShort(toNumber(obj).shortValue());
  915. break;
  916. case LONG:
  917. buffer.putInt(toNumber(obj).intValue());
  918. break;
  919. case MONEY:
  920. writeCurrencyValue(buffer, obj);
  921. break;
  922. case FLOAT:
  923. buffer.putFloat(toNumber(obj).floatValue());
  924. break;
  925. case DOUBLE:
  926. buffer.putDouble(toNumber(obj).doubleValue());
  927. break;
  928. case SHORT_DATE_TIME:
  929. writeDateValue(buffer, obj);
  930. break;
  931. case TEXT:
  932. // apparently text numeric values are also occasionally written as fixed
  933. // length...
  934. int numChars = getLengthInUnits();
  935. // force uncompressed encoding for fixed length text
  936. buffer.put(encodeTextValue(obj, numChars, numChars, true));
  937. break;
  938. case GUID:
  939. writeGUIDValue(buffer, obj);
  940. break;
  941. case NUMERIC:
  942. // yes, that's right, occasionally numeric values are written as fixed
  943. // length...
  944. writeNumericValue(buffer, obj);
  945. break;
  946. case BINARY:
  947. case UNKNOWN_0D:
  948. case UNKNOWN_11:
  949. case COMPLEX_TYPE:
  950. buffer.putInt(toNumber(obj).intValue());
  951. break;
  952. case UNSUPPORTED_FIXEDLEN:
  953. byte[] bytes = toByteArray(obj);
  954. if(bytes.length != getLength()) {
  955. throw new IOException(withErrorContext(
  956. "Invalid fixed size binary data, size "
  957. + getLength() + ", got " + bytes.length));
  958. }
  959. buffer.put(bytes);
  960. break;
  961. default:
  962. throw new IOException(withErrorContext(
  963. "Unsupported data type: " + getType()));
  964. }
  965. return buffer;
  966. }
  967. /**
  968. * Decodes a compressed or uncompressed text value.
  969. */
  970. String decodeTextValue(byte[] data)
  971. throws IOException
  972. {
  973. // see if data is compressed. the 0xFF, 0xFE sequence indicates that
  974. // compression is used (sort of, see algorithm below)
  975. boolean isCompressed = ((data.length > 1) &&
  976. (data[0] == TEXT_COMPRESSION_HEADER[0]) &&
  977. (data[1] == TEXT_COMPRESSION_HEADER[1]));
  978. if(isCompressed) {
  979. // this is a whacky compression combo that switches back and forth
  980. // between compressed/uncompressed using a 0x00 byte (starting in
  981. // compressed mode)
  982. StringBuilder textBuf = new StringBuilder(data.length);
  983. // start after two bytes indicating compression use
  984. int dataStart = TEXT_COMPRESSION_HEADER.length;
  985. int dataEnd = dataStart;
  986. boolean inCompressedMode = true;
  987. while(dataEnd < data.length) {
  988. if(data[dataEnd] == (byte)0x00) {
  989. // handle current segment
  990. decodeTextSegment(data, dataStart, dataEnd, inCompressedMode,
  991. textBuf);
  992. inCompressedMode = !inCompressedMode;
  993. ++dataEnd;
  994. dataStart = dataEnd;
  995. } else {
  996. ++dataEnd;
  997. }
  998. }
  999. // handle last segment
  1000. decodeTextSegment(data, dataStart, dataEnd, inCompressedMode, textBuf);
  1001. return textBuf.toString();
  1002. }
  1003. return decodeUncompressedText(data, getCharset());
  1004. }
  1005. /**
  1006. * Decodes a segnment of a text value into the given buffer according to the
  1007. * given status of the segment (compressed/uncompressed).
  1008. */
  1009. private void decodeTextSegment(byte[] data, int dataStart, int dataEnd,
  1010. boolean inCompressedMode,
  1011. StringBuilder textBuf)
  1012. {
  1013. if(dataEnd <= dataStart) {
  1014. // no data
  1015. return;
  1016. }
  1017. int dataLength = dataEnd - dataStart;
  1018. if(inCompressedMode) {
  1019. byte[] tmpData = new byte[dataLength * 2];
  1020. int tmpIdx = 0;
  1021. for(int i = dataStart; i < dataEnd; ++i) {
  1022. tmpData[tmpIdx] = data[i];
  1023. tmpIdx += 2;
  1024. }
  1025. data = tmpData;
  1026. dataStart = 0;
  1027. dataLength = data.length;
  1028. }
  1029. textBuf.append(decodeUncompressedText(data, dataStart, dataLength,
  1030. getCharset()));
  1031. }
  1032. /**
  1033. * @param textBytes bytes of text to decode
  1034. * @return the decoded string
  1035. */
  1036. private static CharBuffer decodeUncompressedText(
  1037. byte[] textBytes, int startPos, int length, Charset charset)
  1038. {
  1039. return charset.decode(ByteBuffer.wrap(textBytes, startPos, length));
  1040. }
  1041. /**
  1042. * Encodes a text value, possibly compressing.
  1043. */
  1044. ByteBuffer encodeTextValue(Object obj, int minChars, int maxChars,
  1045. boolean forceUncompressed)
  1046. throws IOException
  1047. {
  1048. CharSequence text = toCharSequence(obj);
  1049. if((text.length() > maxChars) || (text.length() < minChars)) {
  1050. throw new IOException(withErrorContext(
  1051. "Text is wrong length for " + getType() +
  1052. " column, max " + maxChars
  1053. + ", min " + minChars + ", got " + text.length()));
  1054. }
  1055. // may only compress if column type allows it
  1056. if(!forceUncompressed && isCompressedUnicode() &&
  1057. (text.length() <= getFormat().MAX_COMPRESSED_UNICODE_SIZE) &&
  1058. isUnicodeCompressible(text)) {
  1059. byte[] encodedChars = new byte[TEXT_COMPRESSION_HEADER.length +
  1060. text.length()];
  1061. encodedChars[0] = TEXT_COMPRESSION_HEADER[0];
  1062. encodedChars[1] = TEXT_COMPRESSION_HEADER[1];
  1063. for(int i = 0; i < text.length(); ++i) {
  1064. encodedChars[i + TEXT_COMPRESSION_HEADER.length] =
  1065. (byte)text.charAt(i);
  1066. }
  1067. return ByteBuffer.wrap(encodedChars);
  1068. }
  1069. return encodeUncompressedText(text, getCharset());
  1070. }
  1071. /**
  1072. * Returns {@code true} if the given text can be compressed using compressed
  1073. * unicode, {@code false} otherwise.
  1074. */
  1075. private static boolean isUnicodeCompressible(CharSequence text) {
  1076. // only attempt to compress > 2 chars (compressing less than 3 chars would
  1077. // not result in a space savings due to the 2 byte compression header)
  1078. if(text.length() <= TEXT_COMPRESSION_HEADER.length) {
  1079. return false;
  1080. }
  1081. // now, see if it is all compressible characters
  1082. for(int i = 0; i < text.length(); ++i) {
  1083. char c = text.charAt(i);
  1084. if((c < MIN_COMPRESS_CHAR) || (c > MAX_COMPRESS_CHAR)) {
  1085. return false;
  1086. }
  1087. }
  1088. return true;
  1089. }
  1090. /**
  1091. * Constructs a byte containing the flags for this column.
  1092. */
  1093. private static byte getColumnBitFlags(ColumnBuilder col) {
  1094. byte flags = UNKNOWN_FLAG_MASK;
  1095. if(!col.isVariableLength()) {
  1096. flags |= FIXED_LEN_FLAG_MASK;
  1097. }
  1098. if(col.isAutoNumber()) {
  1099. byte autoNumFlags = 0;
  1100. switch(col.getType()) {
  1101. case LONG:
  1102. case COMPLEX_TYPE:
  1103. autoNumFlags = AUTO_NUMBER_FLAG_MASK;
  1104. break;
  1105. case GUID:
  1106. autoNumFlags = AUTO_NUMBER_GUID_FLAG_MASK;
  1107. break;
  1108. default:
  1109. // unknown autonum type
  1110. }
  1111. flags |= autoNumFlags;
  1112. }
  1113. if(col.isHyperlink()) {
  1114. flags |= HYPERLINK_FLAG_MASK;
  1115. }
  1116. return flags;
  1117. }
  1118. @Override
  1119. public String toString() {
  1120. ToStringBuilder sb = CustomToStringStyle.builder(this)
  1121. .append("name", "(" + _table.getName() + ") " + _name);
  1122. byte typeValue = getOriginalDataType();
  1123. sb.append("type", "0x" + Integer.toHexString(typeValue) +
  1124. " (" + _type + ")")
  1125. .append("number", _columnNumber)
  1126. .append("length", _columnLength)
  1127. .append("variableLength", _variableLength);
  1128. if(_calculated) {
  1129. sb.append("calculated", _calculated);
  1130. }
  1131. if(_type.isTextual()) {
  1132. sb.append("compressedUnicode", isCompressedUnicode())
  1133. .append("textSortOrder", getTextSortOrder());
  1134. if(getTextCodePage() > 0) {
  1135. sb.append("textCodePage", getTextCodePage());
  1136. }
  1137. if(isAppendOnly()) {
  1138. sb.append("appendOnly", isAppendOnly());
  1139. }
  1140. if(isHyperlink()) {
  1141. sb.append("hyperlink", isHyperlink());
  1142. }
  1143. }
  1144. if(_type.getHasScalePrecision()) {
  1145. sb.append("precision", getPrecision())
  1146. .append("scale", getScale());
  1147. }
  1148. if(_autoNumber) {
  1149. sb.append("lastAutoNumber", _autoNumberGenerator.getLast());
  1150. }
  1151. if(getComplexInfo() != null) {
  1152. sb.append("complexInfo", getComplexInfo());
  1153. }
  1154. return sb.toString();
  1155. }
  1156. /**
  1157. * @param textBytes bytes of text to decode
  1158. * @param charset relevant charset
  1159. * @return the decoded string
  1160. * @usage _advanced_method_
  1161. */
  1162. public static String decodeUncompressedText(byte[] textBytes,
  1163. Charset charset)
  1164. {
  1165. return decodeUncompressedText(textBytes, 0, textBytes.length, charset)
  1166. .toString();
  1167. }
  1168. /**
  1169. * @param text Text to encode
  1170. * @param charset database charset
  1171. * @return A buffer with the text encoded
  1172. * @usage _advanced_method_
  1173. */
  1174. public static ByteBuffer encodeUncompressedText(CharSequence text,
  1175. Charset charset)
  1176. {
  1177. CharBuffer cb = ((text instanceof CharBuffer) ?
  1178. (CharBuffer)text : CharBuffer.wrap(text));
  1179. return charset.encode(cb);
  1180. }
  1181. /**
  1182. * Orders Columns by column number.
  1183. * @usage _general_method_
  1184. */
  1185. public int compareTo(ColumnImpl other) {
  1186. if (_columnNumber > other.getColumnNumber()) {
  1187. return 1;
  1188. } else if (_columnNumber < other.getColumnNumber()) {
  1189. return -1;
  1190. } else {
  1191. return 0;
  1192. }
  1193. }
  1194. /**
  1195. * @param columns A list of columns in a table definition
  1196. * @return The number of variable length columns found in the list
  1197. * @usage _advanced_method_
  1198. */
  1199. public static short countVariableLength(List<ColumnBuilder> columns) {
  1200. short rtn = 0;
  1201. for (ColumnBuilder col : columns) {
  1202. if (col.isVariableLength()) {
  1203. rtn++;
  1204. }
  1205. }
  1206. return rtn;
  1207. }
  1208. /**
  1209. * @param columns A list of columns in a table definition
  1210. * @return The number of variable length columns which are not long values
  1211. * found in the list
  1212. * @usage _advanced_method_
  1213. */
  1214. private static short countNonLongVariableLength(List<ColumnBuilder> columns) {
  1215. short rtn = 0;
  1216. for (ColumnBuilder col : columns) {
  1217. if (col.isVariableLength() && !col.getType().isLongValue()) {
  1218. rtn++;
  1219. }
  1220. }
  1221. return rtn;
  1222. }
  1223. /**
  1224. * @return an appropriate BigDecimal representation of the given object.
  1225. * <code>null</code> is returned as 0 and Numbers are converted
  1226. * using their double representation.
  1227. */
  1228. static BigDecimal toBigDecimal(Object value)
  1229. {
  1230. if(value == null) {
  1231. return BigDecimal.ZERO;
  1232. } else if(value instanceof BigDecimal) {
  1233. return (BigDecimal)value;
  1234. } else if(value instanceof BigInteger) {
  1235. return new BigDecimal((BigInteger)value);
  1236. } else if(value instanceof Number) {
  1237. return new BigDecimal(((Number)value).doubleValue());
  1238. }
  1239. return new BigDecimal(value.toString());
  1240. }
  1241. /**
  1242. * @return an appropriate Number representation of the given object.
  1243. * <code>null</code> is returned as 0 and Strings are parsed as
  1244. * Doubles.
  1245. */
  1246. private static Number toNumber(Object value)
  1247. {
  1248. if(value == null) {
  1249. return BigDecimal.ZERO;
  1250. } else if(value instanceof Number) {
  1251. return (Number)value;
  1252. }
  1253. return Double.valueOf(value.toString());
  1254. }
  1255. /**
  1256. * @return an appropriate CharSequence representation of the given object.
  1257. * @usage _advanced_method_
  1258. */
  1259. public static CharSequence toCharSequence(Object value)
  1260. throws IOException
  1261. {
  1262. if(value == null) {
  1263. return null;
  1264. } else if(value instanceof CharSequence) {
  1265. return (CharSequence)value;
  1266. } else if(value instanceof Clob) {
  1267. try {
  1268. Clob c = (Clob)value;
  1269. // note, start pos is 1-based
  1270. return c.getSubString(1L, (int)c.length());
  1271. } catch(SQLException e) {
  1272. throw (IOException)(new IOException(e.getMessage())).initCause(e);
  1273. }
  1274. } else if(value instanceof Reader) {
  1275. char[] buf = new char[8 * 1024];
  1276. StringBuilder sout = new StringBuilder();
  1277. Reader in = (Reader)value;
  1278. int read = 0;
  1279. while((read = in.read(buf)) != -1) {
  1280. sout.append(buf, 0, read);
  1281. }
  1282. return sout;
  1283. }
  1284. return value.toString();
  1285. }
  1286. /**
  1287. * @return an appropriate byte[] representation of the given object.
  1288. * @usage _advanced_method_
  1289. */
  1290. public static byte[] toByteArray(Object value)
  1291. throws IOException
  1292. {
  1293. if(value == null) {
  1294. return null;
  1295. } else if(value instanceof byte[]) {
  1296. return (byte[])value;
  1297. } else if(value instanceof OleUtil.OleBlobImpl) {
  1298. return ((OleUtil.OleBlobImpl)value).getBytes();
  1299. } else if(value instanceof Blob) {
  1300. try {
  1301. Blob b = (Blob)value;
  1302. // note, start pos is 1-based
  1303. return b.getBytes(1L, (int)b.length());
  1304. } catch(SQLException e) {
  1305. throw (IOException)(new IOException(e.getMessage())).initCause(e);
  1306. }
  1307. } else if(value instanceof RawData) {
  1308. return ((RawData)value).getBytes();
  1309. }
  1310. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  1311. if(value instanceof InputStream) {
  1312. ByteUtil.copy((InputStream)value, bout);
  1313. } else {
  1314. // if all else fails, serialize it
  1315. ObjectOutputStream oos = new ObjectOutputStream(bout);
  1316. oos.writeObject(value);
  1317. oos.close();
  1318. }
  1319. return bout.toByteArray();
  1320. }
  1321. /**
  1322. * Interpret a boolean value (null == false)
  1323. * @usage _advanced_method_
  1324. */
  1325. public static boolean toBooleanValue(Object obj) {
  1326. if(obj == null) {
  1327. return false;
  1328. } else if(obj instanceof Boolean) {
  1329. return ((Boolean)obj).booleanValue();
  1330. }
  1331. return Boolean.parseBoolean(obj.toString());
  1332. }
  1333. /**
  1334. * Swaps the bytes of the given numeric in place.
  1335. */
  1336. private static void fixNumericByteOrder(byte[] bytes)
  1337. {
  1338. // fix endianness of each 4 byte segment
  1339. for(int i = 0; i < bytes.length; i+=4) {
  1340. ByteUtil.swap4Bytes(bytes, i);
  1341. }
  1342. }
  1343. /**
  1344. * Treat booleans as integers (C-style).
  1345. */
  1346. protected static Object booleanToInteger(Object obj) {
  1347. if (obj instanceof Boolean) {
  1348. obj = ((Boolean) obj) ? 1 : 0;
  1349. }
  1350. return obj;
  1351. }
  1352. /**
  1353. * Returns a wrapper for raw column data that can be written without
  1354. * understanding the data. Useful for wrapping unparseable data for
  1355. * re-writing.
  1356. */
  1357. public static RawData rawDataWrapper(byte[] bytes) {
  1358. return new RawData(bytes);
  1359. }
  1360. /**
  1361. * Returns {@code true} if the given value is "raw" column data,
  1362. * {@code false} otherwise.
  1363. * @usage _advanced_method_
  1364. */
  1365. public static boolean isRawData(Object value) {
  1366. return(value instanceof RawData);
  1367. }
  1368. /**
  1369. * Writes the column definitions into a table definition buffer.
  1370. * @param buffer Buffer to write to
  1371. */
  1372. protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer)
  1373. throws IOException
  1374. {
  1375. List<ColumnBuilder> columns = creator.getColumns();
  1376. short fixedOffset = (short) 0;
  1377. short variableOffset = (short) 0;
  1378. // we specifically put the "long variable" values after the normal
  1379. // variable length values so that we have a better chance of fitting it
  1380. // all (because "long variable" values can go in separate pages)
  1381. short longVariableOffset = countNonLongVariableLength(columns);
  1382. for (ColumnBuilder col : columns) {
  1383. buffer.put(col.getType().getValue());
  1384. buffer.putInt(TableImpl.MAGIC_TABLE_NUMBER); //constant magic number
  1385. buffer.putShort(col.getColumnNumber()); //Column Number
  1386. if(col.isVariableLength()) {
  1387. if(!col.getType().isLongValue()) {
  1388. buffer.putShort(variableOffset++);
  1389. } else {
  1390. buffer.putShort(longVariableOffset++);
  1391. }
  1392. } else {
  1393. buffer.putShort((short) 0);
  1394. }
  1395. buffer.putShort(col.getColumnNumber()); //Column Number again
  1396. if(col.getType().isTextual()) {
  1397. // this will write 4 bytes (note we don't support writing dbs which
  1398. // use the text code page)
  1399. writeSortOrder(buffer, col.getTextSortOrder(), creator.getFormat());
  1400. } else {
  1401. // note scale/precision not stored for calculated numeric fields
  1402. if(col.getType().getHasScalePrecision() && !col.isCalculated()) {
  1403. buffer.put(col.getPrecision()); // numeric precision
  1404. buffer.put(col.getScale()); // numeric scale
  1405. } else {
  1406. buffer.put((byte) 0x00); //unused
  1407. buffer.put((byte) 0x00); //unused
  1408. }
  1409. buffer.putShort((short) 0); //Unknown
  1410. }
  1411. buffer.put(getColumnBitFlags(col)); // misc col flags
  1412. // note access doesn't seem to allow unicode compression for calced fields
  1413. if(col.isCalculated()) {
  1414. buffer.put(CALCULATED_EXT_FLAG_MASK);
  1415. } else if (col.isCompressedUnicode()) { //Compressed
  1416. buffer.put(COMPRESSED_UNICODE_EXT_FLAG_MASK);
  1417. } else {
  1418. buffer.put((byte)0);
  1419. }
  1420. buffer.putInt(0); //Unknown, but always 0.
  1421. //Offset for fixed length columns
  1422. if(col.isVariableLength()) {
  1423. buffer.putShort((short) 0);
  1424. } else {
  1425. buffer.putShort(fixedOffset);
  1426. fixedOffset += col.getType().getFixedSize(col.getLength());
  1427. }
  1428. if(!col.getType().isLongValue()) {
  1429. short length = col.getLength();
  1430. if(col.isCalculated()) {
  1431. // calced columns have additional value overhead
  1432. if(!col.getType().isVariableLength() ||
  1433. col.getType().getHasScalePrecision()) {
  1434. length = CalculatedColumnUtil.CALC_FIXED_FIELD_LEN;
  1435. } else {
  1436. length += CalculatedColumnUtil.CALC_EXTRA_DATA_LEN;
  1437. }
  1438. }
  1439. buffer.putShort(length); //Column length
  1440. } else {
  1441. buffer.putShort((short)0x0000); // unused
  1442. }
  1443. }
  1444. for (ColumnBuilder col : columns) {
  1445. TableImpl.writeName(buffer, col.getName(), creator.getCharset());
  1446. }
  1447. }
  1448. /**
  1449. * Reads the sort order info from the given buffer from the given position.
  1450. */
  1451. static SortOrder readSortOrder(ByteBuffer buffer, int position,
  1452. JetFormat format)
  1453. {
  1454. short value = buffer.getShort(position);
  1455. byte version = 0;
  1456. if(format.SIZE_SORT_ORDER == 4) {
  1457. version = buffer.get(position + 3);
  1458. }
  1459. if(value == 0) {
  1460. // probably a file we wrote, before handling sort order
  1461. return format.DEFAULT_SORT_ORDER;
  1462. }
  1463. if(value == GENERAL_SORT_ORDER_VALUE) {
  1464. if(version == GENERAL_LEGACY_SORT_ORDER.getVersion()) {
  1465. return GENERAL_LEGACY_SORT_ORDER;
  1466. }
  1467. if(version == GENERAL_SORT_ORDER.getVersion()) {
  1468. return GENERAL_SORT_ORDER;
  1469. }
  1470. }
  1471. return new SortOrder(value, version);
  1472. }
  1473. /**
  1474. * Reads the column cade page info from the given buffer, if supported for
  1475. * this db.
  1476. */
  1477. static short readCodePage(ByteBuffer buffer, int offset, JetFormat format)
  1478. {
  1479. int cpOffset = format.OFFSET_COLUMN_CODE_PAGE;
  1480. return ((cpOffset >= 0) ? buffer.getShort(offset + cpOffset) : 0);
  1481. }
  1482. /**
  1483. * Read the extra flags field for a column definition.
  1484. */
  1485. static byte readExtraFlags(ByteBuffer buffer, int offset, JetFormat format)
  1486. {
  1487. int extFlagsOffset = format.OFFSET_COLUMN_EXT_FLAGS;
  1488. return ((extFlagsOffset >= 0) ? buffer.get(offset + extFlagsOffset) : 0);
  1489. }
  1490. /**
  1491. * Writes the sort order info to the given buffer at the current position.
  1492. */
  1493. private static void writeSortOrder(ByteBuffer buffer, SortOrder sortOrder,
  1494. JetFormat format) {
  1495. if(sortOrder == null) {
  1496. sortOrder = format.DEFAULT_SORT_ORDER;
  1497. }
  1498. buffer.putShort(sortOrder.getValue());
  1499. if(format.SIZE_SORT_ORDER == 4) {
  1500. buffer.put((byte)0x00); // unknown
  1501. buffer.put(sortOrder.getVersion());
  1502. }
  1503. }
  1504. /**
  1505. * Returns {@code true} if the value is immutable, {@code false} otherwise.
  1506. * This only handles values that are returned from the {@link #read} method.
  1507. */
  1508. static boolean isImmutableValue(Object value) {
  1509. // for now, the only mutable value this class returns is byte[]
  1510. return !(value instanceof byte[]);
  1511. }
  1512. /**
  1513. * Converts the given value to the "internal" representation for the given
  1514. * data type.
  1515. */
  1516. public static Object toInternalValue(DataType dataType, Object value)
  1517. throws IOException
  1518. {
  1519. if(value == null) {
  1520. return null;
  1521. }
  1522. switch(dataType) {
  1523. case BOOLEAN:
  1524. return ((value instanceof Boolean) ? value : toBooleanValue(value));
  1525. case BYTE:
  1526. return ((value instanceof Byte) ? value : toNumber(value).byteValue());
  1527. case INT:
  1528. return ((value instanceof Short) ? value :
  1529. toNumber(value).shortValue());
  1530. case LONG:
  1531. return ((value instanceof Integer) ? value :
  1532. toNumber(value).intValue());
  1533. case MONEY:
  1534. return toBigDecimal(value);
  1535. case FLOAT:
  1536. return ((value instanceof Float) ? value :
  1537. toNumber(value).floatValue());
  1538. case DOUBLE:
  1539. return ((value instanceof Double) ? value :
  1540. toNumber(value).doubleValue());
  1541. case SHORT_DATE_TIME:
  1542. return ((value instanceof DateExt) ? value :
  1543. new Date(toDateLong(value)));
  1544. case TEXT:
  1545. case MEMO:
  1546. case GUID:
  1547. return ((value instanceof String) ? value :
  1548. toCharSequence(value).toString());
  1549. case NUMERIC:
  1550. return toBigDecimal(value);
  1551. case COMPLEX_TYPE:
  1552. // leave alone for now?
  1553. return value;
  1554. default:
  1555. // some variation of binary data
  1556. return toByteArray(value);
  1557. }
  1558. }
  1559. String withErrorContext(String msg) {
  1560. return withErrorContext(msg, getDatabase(), getTable().getName(), getName());
  1561. }
  1562. private static String withErrorContext(
  1563. String msg, DatabaseImpl db, String tableName, String colName) {
  1564. return msg + " (Db=" + db.getName() + ";Table=" + tableName + ";Column=" +
  1565. colName + ")";
  1566. }
  1567. /**
  1568. * Date subclass which stashes the original date bits, in case we attempt to
  1569. * re-write the value (will not lose precision).
  1570. */
  1571. private static final class DateExt extends Date
  1572. {
  1573. private static final long serialVersionUID = 0L;
  1574. /** cached bits of the original date value */
  1575. private transient final long _dateBits;
  1576. private DateExt(long time, long dateBits) {
  1577. super(time);
  1578. _dateBits = dateBits;
  1579. }
  1580. public long getDateBits() {
  1581. return _dateBits;
  1582. }
  1583. private Object writeReplace() throws ObjectStreamException {
  1584. // if we are going to serialize this Date, convert it back to a normal
  1585. // Date (in case it is restored outside of the context of jackcess)
  1586. return new Date(super.getTime());
  1587. }
  1588. }
  1589. /**
  1590. * Wrapper for raw column data which can be re-written.
  1591. */
  1592. private static class RawData implements Serializable
  1593. {
  1594. private static final long serialVersionUID = 0L;
  1595. private final byte[] _bytes;
  1596. private RawData(byte[] bytes) {
  1597. _bytes = bytes;
  1598. }
  1599. private byte[] getBytes() {
  1600. return _bytes;
  1601. }
  1602. @Override
  1603. public String toString() {
  1604. return CustomToStringStyle.valueBuilder(this)
  1605. .append(null, getBytes())
  1606. .toString();
  1607. }
  1608. private Object writeReplace() throws ObjectStreamException {
  1609. // if we are going to serialize this, convert it back to a normal
  1610. // byte[] (in case it is restored outside of the context of jackcess)
  1611. return getBytes();
  1612. }
  1613. }
  1614. /**
  1615. * Base class for the supported autonumber types.
  1616. * @usage _advanced_class_
  1617. */
  1618. public abstract class AutoNumberGenerator
  1619. {
  1620. protected AutoNumberGenerator() {}
  1621. /**
  1622. * Returns the last autonumber generated by this generator. Only valid
  1623. * after a call to {@link Table#addRow}, otherwise undefined.
  1624. */
  1625. public abstract Object getLast();
  1626. /**
  1627. * Returns the next autonumber for this generator.
  1628. * <p>
  1629. * <i>Warning, calling this externally will result in this value being
  1630. * "lost" for the table.</i>
  1631. */
  1632. public abstract Object getNext(Object prevRowValue);
  1633. /**
  1634. * Restores a previous autonumber generated by this generator.
  1635. */
  1636. public abstract void restoreLast(Object last);
  1637. /**
  1638. * Returns the type of values generated by this generator.
  1639. */
  1640. public abstract DataType getType();
  1641. }
  1642. private final class LongAutoNumberGenerator extends AutoNumberGenerator
  1643. {
  1644. private LongAutoNumberGenerator() {}
  1645. @Override
  1646. public Object getLast() {
  1647. // the table stores the last long autonumber used
  1648. return getTable().getLastLongAutoNumber();
  1649. }
  1650. @Override
  1651. public Object getNext(Object prevRowValue) {
  1652. // the table stores the last long autonumber used
  1653. return getTable().getNextLongAutoNumber();
  1654. }
  1655. @Override
  1656. public void restoreLast(Object last) {
  1657. if(last instanceof Integer) {
  1658. getTable().restoreLastLongAutoNumber((Integer)last);
  1659. }
  1660. }
  1661. @Override
  1662. public DataType getType() {
  1663. return DataType.LONG;
  1664. }
  1665. }
  1666. private final class GuidAutoNumberGenerator extends AutoNumberGenerator
  1667. {
  1668. private Object _lastAutoNumber;
  1669. private GuidAutoNumberGenerator() {}
  1670. @Override
  1671. public Object getLast() {
  1672. return _lastAutoNumber;
  1673. }
  1674. @Override
  1675. public Object getNext(Object prevRowValue) {
  1676. // format guids consistently w/ Column.readGUIDValue()
  1677. _lastAutoNumber = "{" + UUID.randomUUID() + "}";
  1678. return _lastAutoNumber;
  1679. }
  1680. @Override
  1681. public void restoreLast(Object last) {
  1682. _lastAutoNumber = null;
  1683. }
  1684. @Override
  1685. public DataType getType() {
  1686. return DataType.GUID;
  1687. }
  1688. }
  1689. private final class ComplexTypeAutoNumberGenerator extends AutoNumberGenerator
  1690. {
  1691. private ComplexTypeAutoNumberGenerator() {}
  1692. @Override
  1693. public Object getLast() {
  1694. // the table stores the last ComplexType autonumber used
  1695. return getTable().getLastComplexTypeAutoNumber();
  1696. }
  1697. @Override
  1698. public Object getNext(Object prevRowValue) {
  1699. int nextComplexAutoNum =
  1700. ((prevRowValue == null) ?
  1701. // the table stores the last ComplexType autonumber used
  1702. getTable().getNextComplexTypeAutoNumber() :
  1703. // same value is shared across all ComplexType values in a row
  1704. ((ComplexValueForeignKey)prevRowValue).get());
  1705. return new ComplexValueForeignKeyImpl(ColumnImpl.this, nextComplexAutoNum);
  1706. }
  1707. @Override
  1708. public void restoreLast(Object last) {
  1709. if(last instanceof ComplexValueForeignKey) {
  1710. getTable().restoreLastComplexTypeAutoNumber(
  1711. ((ComplexValueForeignKey)last).get());
  1712. }
  1713. }
  1714. @Override
  1715. public DataType getType() {
  1716. return DataType.COMPLEX_TYPE;
  1717. }
  1718. }
  1719. private final class UnsupportedAutoNumberGenerator extends AutoNumberGenerator
  1720. {
  1721. private final DataType _genType;
  1722. private UnsupportedAutoNumberGenerator(DataType genType) {
  1723. _genType = genType;
  1724. }
  1725. @Override
  1726. public Object getLast() {
  1727. return null;
  1728. }
  1729. @Override
  1730. public Object getNext(Object prevRowValue) {
  1731. throw new UnsupportedOperationException();
  1732. }
  1733. @Override
  1734. public void restoreLast(Object last) {
  1735. throw new UnsupportedOperationException();
  1736. }
  1737. @Override
  1738. public DataType getType() {
  1739. return _genType;
  1740. }
  1741. }
  1742. /**
  1743. * Information about the sort order (collation) for a textual column.
  1744. * @usage _intermediate_class_
  1745. */
  1746. public static final class SortOrder
  1747. {
  1748. private final short _value;
  1749. private final byte _version;
  1750. public SortOrder(short value, byte version) {
  1751. _value = value;
  1752. _version = version;
  1753. }
  1754. public short getValue() {
  1755. return _value;
  1756. }
  1757. public byte getVersion() {
  1758. return _version;
  1759. }
  1760. @Override
  1761. public int hashCode() {
  1762. return _value;
  1763. }
  1764. @Override
  1765. public boolean equals(Object o) {
  1766. return ((this == o) ||
  1767. ((o != null) && (getClass() == o.getClass()) &&
  1768. (_value == ((SortOrder)o)._value) &&
  1769. (_version == ((SortOrder)o)._version)));
  1770. }
  1771. @Override
  1772. public String toString() {
  1773. return CustomToStringStyle.valueBuilder(this)
  1774. .append(null, _value + "(" + _version + ")")
  1775. .toString();
  1776. }
  1777. }
  1778. /**
  1779. * Utility struct for passing params through ColumnImpl constructors.
  1780. */
  1781. static final class InitArgs
  1782. {
  1783. public final TableImpl table;
  1784. public final ByteBuffer buffer;
  1785. public final int offset;
  1786. public final String name;
  1787. public final int displayIndex;
  1788. public final byte colType;
  1789. public final byte flags;
  1790. public final byte extFlags;
  1791. public DataType type;
  1792. InitArgs(TableImpl table, ByteBuffer buffer, int offset, String name,
  1793. int displayIndex) {
  1794. this.table = table;
  1795. this.buffer = buffer;
  1796. this.offset = offset;
  1797. this.name = name;
  1798. this.displayIndex = displayIndex;
  1799. this.colType = buffer.get(offset + table.getFormat().OFFSET_COLUMN_TYPE);
  1800. this.flags = buffer.get(offset + table.getFormat().OFFSET_COLUMN_FLAGS);
  1801. this.extFlags = readExtraFlags(buffer, offset, table.getFormat());
  1802. }
  1803. }
  1804. }