|
Java example source code file (Gen.java)
The Gen.java Java example source code/* * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.IOException; import java.io.File; import java.io.FileOutputStream; import java.io.DataOutputStream; import java.io.RandomAccessFile; import java.util.List; import java.util.Map; import java.util.Set; /** * <code>Gen is one of back-end classes of javazic, and generates * ZoneInfoMappings and zone-specific file for each zone. */ class Gen extends BackEnd { /** * Generates datafile in binary TLV format for each time zone. * Regarding contents of output files, see {@link ZoneInfoFile}. * * @param Timezone * @return 0 if no errors, or 1 if error occurred. */ int processZoneinfo(Timezone tz) { try { int size; String outputDir = Main.getOutputDir(); String zonefile = ZoneInfoFile.getFileName(tz.getName()); /* If outputDir doesn't end with file-separator, adds it. */ if (!outputDir.endsWith(File.separator)) { outputDir += File.separatorChar; } /* If zonefile includes file-separator, it's treated as part of * pathname. And make directory if necessary. */ int index = zonefile.lastIndexOf(File.separatorChar); if (index != -1) { outputDir += zonefile.substring(0, index+1); } File outD = new File(outputDir); outD.mkdirs(); FileOutputStream fos = new FileOutputStream(outputDir + zonefile.substring(index+1)); DataOutputStream dos = new DataOutputStream(fos); /* Output Label */ dos.write(ZoneInfoFile.JAVAZI_LABEL, 0, ZoneInfoFile.JAVAZI_LABEL.length); /* Output Version of ZoneInfoFile */ dos.writeByte(ZoneInfoFile.JAVAZI_VERSION); List<Long> transitions = tz.getTransitions(); if (transitions != null) { List<Integer> dstOffsets = tz.getDstOffsets(); List<Integer> offsets = tz.getOffsets(); if ((dstOffsets == null && offsets != null) || (dstOffsets != null && offsets == null)) { Main.panic("Data not exist. (dstOffsets or offsets)"); return 1; } /* Output Transition records */ dos.writeByte(ZoneInfoFile.TAG_Transition); size = transitions.size(); dos.writeShort((size * 8) & 0xFFFF); int dstoffset; for (int i = 0; i < size; i++) { /* if DST offset is 0, this means DST isn't used. * (NOT: offset's index is 0.) */ if ((dstoffset = dstOffsets.get(i).intValue()) == -1) { dstoffset = 0; } dos.writeLong((transitions.get(i).longValue() << 12) | (dstoffset << 4) | offsets.get(i).intValue()); } /* Output data for GMTOffset */ List<Integer> gmtoffset = tz.getGmtOffsets(); dos.writeByte(ZoneInfoFile.TAG_Offset); size = gmtoffset.size(); dos.writeShort((size * 4) & 0xFFFF); for (int i = 0; i < size; i++) { dos.writeInt(gmtoffset.get(i)); } } /* Output data for SimpleTimeZone */ List<RuleRec> stz = tz.getLastRules(); if (stz != null) { RuleRec[] rr = new RuleRec[2]; boolean wall = true; rr[0] = stz.get(0); rr[1] = stz.get(1); dos.writeByte(ZoneInfoFile.TAG_SimpleTimeZone); wall = rr[0].getTime().isWall() && rr[1].getTime().isWall(); if (wall) { dos.writeShort(32); } else { dos.writeShort(40); } for (int i = 0; i < 2; i++) { dos.writeInt(rr[i].getMonthNum() - 1); // 0-based month number dos.writeInt(rr[i].getDay().getDayForSimpleTimeZone()); dos.writeInt(rr[i].getDay().getDayOfWeekForSimpleTimeZoneInt()); dos.writeInt((int)rr[i].getTime().getTime()); if (!wall) { dos.writeInt((rr[i].getTime().getType() & 0xFF) - 1); } } } /* Output RawOffset */ dos.writeByte(ZoneInfoFile.TAG_RawOffset); dos.writeShort(4); dos.writeInt(tz.getRawOffset()); /* Output willGMTOffsetChange flag */ if (tz.willGMTOffsetChange()) { dos.writeByte(ZoneInfoFile.TAG_GMTOffsetWillChange); dos.writeShort(1); dos.writeByte(1); } /* Output LastDSTSaving */ dos.writeByte(ZoneInfoFile.TAG_LastDSTSaving); dos.writeShort(2); dos.writeShort(tz.getLastDSTSaving()/1000); /* Output checksum */ dos.writeByte(ZoneInfoFile.TAG_CRC32); dos.writeShort(4); dos.writeInt(tz.getCRC32()); fos.close(); dos.close(); } catch(IOException e) { Main.panic("IO error: "+e.getMessage()); return 1; } return 0; } /** * Generates ZoneInfoMappings in binary TLV format for each zone. * Regarding contents of output files, see {@link ZoneInfoFile}. * * @param Mappings * @return 0 if no errors, or 1 if error occurred. */ int generateSrc(Mappings map) { try { int index; int block_size; int roi_size; long fp; String outputDir = Main.getOutputDir(); /* If outputDir doesn't end with file-separator, adds it. */ if (!outputDir.endsWith(File.separator)) { outputDir += File.separatorChar; } File outD = new File(outputDir); outD.mkdirs(); /* Open ZoneInfoMapping file to write. */ RandomAccessFile raf = new RandomAccessFile(outputDir + ZoneInfoFile.JAVAZM_FILE_NAME, "rw"); /* Whether rawOffsetIndex list exists or not. */ List<Integer> roi = map.getRawOffsetsIndex(); if (roi == null) { Main.panic("Data not exist. (rawOffsetsIndex)"); return 1; } roi_size = roi.size(); /* Whether rawOffsetIndexTable list exists or not. */ List<Set Other Java examples (source code examples)Here is a short list of links related to this Java Gen.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.