tag:blogger.com,1999:blog-87763781173896090262024-03-17T04:14:42.518-07:00My Experiments With JavaPallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.comBlogger63125tag:blogger.com,1999:blog-8776378117389609026.post-51727124266956267422024-03-17T04:01:00.000-07:002024-03-17T04:14:10.813-07:00All about Dropwizard Metrics<p>Dropwizard stands out as a favored Java framework for developing rest-based applications. Ensuring the observability of an application stands as a critical aspect for maintaining service continuity and stability. This directly impacts the end-user experience and, in many instances, revenue generation. The Metrics library simplifies the process within a Dropwizard application, enabling the collection of various system metrics alongside custom metrics. Additionally, it facilitates the authoring of health checks for the application.</p>
<p>To integrate metrics into a Dropwizard application, adding the following dependency is essential:</p>
<pre><code><dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.2</version>
</dependency>
</code></pre>
<h2>Metrics Overview:</h2>
<p>Metrics necessitate a metric registry to function effectively, which can accommodate numerous metrics.</p>
<pre><code>private final MetricRegistry metrics = new MetricRegistry();
</code></pre>
<p>In a Dropwizard application, access to the metric registry and health check registry is available in the initialize method:</p>
<pre><code>public void initialize(final Bootstrap<AppConfiguration> bootstrap) {
metrics = bootstrap.getMetricRegistry();
healthCheckRegistry = bootstrap.getHealthCheckRegistry();
}
</code></pre>
<p>Metrics are broadly categorized into:</p>
<ol>
<li><strong>Meters:</strong><br />
Meters gauge rates of events and track mean rates. They also monitor 1, 5, and 15-minute moving averages.
<pre><code>private final Meter requests = metrics.meter("requests");
public void handleRequest(Request request, Response response) {
requests.mark();
// ...
}
</code></pre>
</li>
<li><strong>Gauges:</strong><br />
Gauges provide a snapshot of a particular value at a given time. For instance, the number of pending jobs.
<pre><code>final Queue<String> queue = new LinkedList<String>();
final Gauge<Integer> pendingJobs = new Gauge<Integer>() {
public Integer getValue() {
return queue.size();
}
};
</code></pre>
</li>
<li><strong>Counters:</strong><br />
Counters are a type of gauge that can be incremented or decremented, offering more efficiency than gauges.
<pre><code>private final Counter pendingJobs = metrics.counter(name(QueueManager.class, "pending-jobs"));
public void addJob(Job job) {
pendingJobs.inc();
queue.offer(job);
}
public Job takeJob() {
pendingJobs.dec();
return queue.take();
}
</code></pre>
</li>
<li><strong>Histograms:</strong><br />
Histograms track various statistical aspects of a data stream, including minimum, maximum, mean, median, and various percentiles.
<pre><code>private final Histogram stockPrice = metrics.histogram(name(StockPriceHandler.class, "stock-price"));
public void updateStockPrice(Request request, Response response) {
stockPrice.update(request.getStockPrice());
}
</code></pre>
</li>
<li><strong>Timers:</strong><br />
Timers measure both the rate at which a piece of code is called and the duration of its execution.
<pre><code>private final Timer responses = metrics.timer(name(RequestHandler.class, "responses"));
public String handleRequest(Request request, Response response) {
try(final Timer.Context context = responses.time()) {
return "OK";
}
}
</code></pre>
Alternatively, the <code>@Timed</code> annotation can be used:
<pre><code>@GET
@Path("sayhello")
@Timed(name = "my-timed-metric")
public Response sayHello(@QueryParam("q") @DefaultValue("%") String queryString) {
return Response.ok().entity("Hello " + queryString).build();
}
</code></pre>
</li>
<li><strong>Healthchecks:</strong><br />Health checks determine whether a service is healthy or not.
<pre><code>public class DatabaseHealthCheck extends HealthCheck {
public final static String NAME = "database-health";
private final HibernateBundle<ApiAppConfiguration> hibernate;
public DatabaseHealthCheck(HibernateBundle<AppConfiguration> hibernate) {
this.hibernate = hibernate;
}
@Override
protected Result check() throws Exception {
if (hibernate.getSessionFactory().isClosed()) {
return Result.unhealthy("Database session factory is closed");
}
return Result.healthy();
}
}
</code></pre>
Registration of the health check in the health check registry is necessary:
<pre><code>environment.healthChecks().register(DatabaseHealthCheck.NAME, new DatabaseHealthCheck(hibernate));
</code></pre>
</li>
</ol>
<p>These metrics play a crucial role in monitoring and maintaining the health and performance of applications.</p>
<p>Reporters play a crucial role in outputting metrics gathered by Dropwizard. There are several out-of-the-box popular reporters available, including console, CSV, SLF4J, Graphite, and Prometheus.</p>
<p>Here is an example of a console reporter:</p>
<pre><code>ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
reporter.start(10, TimeUnit.SECONDS);
</code></pre>
<p>Additionally, we can create our custom reporter to integrate with our monitoring system:</p>
<pre><code>package example.monitoring;
import com.codahale.metrics.*;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import java.util.Map;
public class MyCustomReporter extends ScheduledReporter {
public MyCustomReporter(MetricRegistry registry, TimeUnit rateUnit, TimeUnit durationUnit) {
super(registry, "custom-metric-reporter", MetricFilter.ALL, rateUnit, durationUnit);
}
// This method will be called periodically by the ScheduledReporter
@Override
public void report(SortedMap<String, Gauge> gauges,
SortedMap<String, Counter> counters,
SortedMap<String, Histogram> histograms,
SortedMap<String, Meter> meters,
SortedMap<String, Timer> timers) {
// Implement reporting logic here
// Example: Print out all metric names and their values
System.out.println("MyCustomReporter in action !!!");
for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
System.out.println("Gauge: " + entry.getKey() + ", Value: " + entry.getValue().getValue());
}
for (Map.Entry<String, Counter> entry : counters.entrySet()) {
System.out.println("Counter: " + entry.getKey() + ", Count: " + entry.getValue().getCount());
}
// Similarly, report for histograms, meters, and timers
}
// Factory method to create an instance of CustomMetricReporter
public static MyCustomReporter forRegistry(MetricRegistry registry) {
return new MyCustomReporter(registry, TimeUnit.SECONDS, TimeUnit.SECONDS);
}
}
MyCustomReporter myCustomReporter = MyCustomReporter.forRegistry(metrics);
myCustomReporter.start(10, TimeUnit.SECONDS); // Report every 10 seconds
</code></pre>
<p>I hope this information is useful. Please let me know if you have any thoughts or questions about it.</p>
<p>Reference: <a href="https://metrics.dropwizard.io/">Dropwizard Metrics Documentation</a></p>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-41051024262925193972023-12-02T03:30:00.000-08:002023-12-02T03:30:15.770-08:00Java 17 Pattern Matching in Switch and Yield<p> In Java switch statements are used instead of multiple if-else statements. Something like below</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">switch </span>(day) {<br /> <span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Monday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an monday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Tuesday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an tuesday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Wednesday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an wednesday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Thursday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an thursday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Friday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an friday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Saturday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an saturday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Sunday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an sunday"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> default </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Invalid date"</span>)<span style="color: #cc7832;">;<br /></span>}</div><p>There are multiple advantages of this we dont' need to write multiple nested if-else block</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">if </span>(<span style="color: #6a8759;">"Monday"</span>.equals(day)) {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an monday"</span>)<span style="color: #cc7832;">;<br /></span>}<br /><span style="color: #cc7832;">else if </span>(<span style="color: #6a8759;">"Tuesday"</span>.equals(day)) {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an tuesday"</span>)<span style="color: #cc7832;">;<br /></span>}<br />......<br /><span style="color: #cc7832;">else </span>{<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Invalid date"</span>)<span style="color: #cc7832;">;<br /></span>}</div><p>Switch statements are there in java for a long back. over time it has evolved. Earlier Switch expressions are supposed to be numbers or enums. Case statements used to start with : and break need to be added to stop the overflow</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">switch </span>(day) {<br /> <span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Monday"</span>: System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an monday"</span>)<span style="color: #cc7832;">; break;<br /></span><span style="color: #cc7832;"> case </span><span style="color: #6a8759;">"Tuesday"</span>: System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an Tuesday"</span>)<span style="color: #cc7832;">; break;<br /></span><span style="color: #cc7832;"> </span>.....<br />}</div><p>In Java 17 switch is enhanced. Before Java 17 switch started supporting string as expression type. In 17 the case key word can be followed with -> so that we dont need break any more.</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Saturday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Today is an saturday"</span>)<span style="color: #cc7832;">;</span></div><p>we can have multiple levels in case.</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Saturday"</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"Sunday" </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Its weekend"</span>)<span style="color: #cc7832;">;</span></div><p>We can return value from the case using yield</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;"><span style="color: #cc7832;">int </span>dayOfTheWeek = <span style="color: #cc7832;">switch </span>(day) {<br /> <span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Monday" </span>-> {<br /> <span style="color: #cc7832;">yield </span><span style="color: #6897bb;">1</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> <span style="color: #cc7832;">case </span><span style="color: #6a8759;">"Tuesday" </span>-> {<br /> <span style="color: #cc7832;">yield </span><span style="color: #6897bb;">2</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> <span style="color: #cc7832;">default </span>-> {<br /> <span style="color: #cc7832;">yield </span>-<span style="color: #6897bb;">1</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br />}<span style="color: #cc7832;">;</span></div><p>In case expression we can try an instance of the operator or null check also</p><div style="background-color: #2b2b2b; color: #a9b7c6; font-family: 'JetBrains Mono',monospace; font-size: 9.8pt; white-space: pre;">Object x = <span style="color: #6897bb;">0</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;">switch </span>(x) {<br /> <span style="color: #cc7832;">case null </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"x is null"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> case </span>Integer i -> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"x is an integer"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> default </span>-> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"x is of type "</span>+x.getClass().getName())<span style="color: #cc7832;">;<br /></span>}</div><p>These are still preview features to try this we need to enable it in our java env</p><p><span style="background-color: #fafafa; font-family: "Source Code Pro", Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 14px; white-space-collapse: preserve;">java --enable-preview --</span><span class="hljs-built_in" style="box-sizing: border-box; color: #397300; font-family: "Source Code Pro", Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 14px; white-space-collapse: preserve;">source</span><span style="background-color: #fafafa; font-family: "Source Code Pro", Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 14px; white-space-collapse: preserve;"> 17 <filename.java></span></p><p>Please let me know your thoughts about this topic.</p><p>Reference</p><p>https://docs.oracle.com/en/java/javase/17/language/pattern-matching-switch-expressions-and-statements.html#GUID-E69EEA63-E204-41B4-AA7F-D58B26A3B232</p>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-56924033327197328812022-09-04T05:01:00.008-07:002022-09-04T11:00:18.612-07:00OCI function in golang connecting to autonomous database over TLS<p> In this post we will discuss how to connect oracle cloud infrastructure function implemented in golang connect to the autonomous database. We will configure the adb on TLS, so that we don't need the client credential wallet for connection. If we need to connect over mtls still this post will give some idea but we need to stage the client credential in the function runtime container, this I may discuss in a different post. </p><p>1. Configure ADB for TLS</p><p>We need to enable TLS connection in the autonomous database detail page. (<a href="https://docs.oracle.com/en-us/iaas/autonomous-database-shared/doc/support-tls-mtls-authentication.html" target="_blank">More details</a>). </p><p>a) In adb detail page under the Network section enable the access control list, here I configured the allow-only ip addresses of a particular vcn should able to connect the adb.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhyDMQGaeRkqDgJH6whwakGZCdx1uALHbCXzLZHsb7nn9l9dsLaK5LYsLX_ysn27iEWI4znbCIDgINzzDEVtW4iMmq5nSR2aw0qtr41t0RNn_zxrHTTofwN5FRSesLomq4MBixI3F8f0hkx3FHeiU5lbnfa40mhJQNtuDdYQPTjkmCCzAvqUL3jD1E-GA" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img alt="" data-original-height="155" data-original-width="396" height="156" src="https://blogger.googleusercontent.com/img/a/AVvXsEhyDMQGaeRkqDgJH6whwakGZCdx1uALHbCXzLZHsb7nn9l9dsLaK5LYsLX_ysn27iEWI4znbCIDgINzzDEVtW4iMmq5nSR2aw0qtr41t0RNn_zxrHTTofwN5FRSesLomq4MBixI3F8f0hkx3FHeiU5lbnfa40mhJQNtuDdYQPTjkmCCzAvqUL3jD1E-GA=w400-h156" width="400" /></a><a href="https://blogger.googleusercontent.com/img/a/AVvXsEj74b_lTiCLB6m7BeRoEBoIvElQHjGrqNltXbsNxe95GXr69WOmJ4xUDrqQCIkEIy0Kn233xmNMcn0nqfRMyWExSOfxKq4P3r8_V9DEIPJDwHPMyJHI5cofDrDrhrnrAVrJmnWds66Nwlorvkzzp2LmzvYJsEW7d57CMI1JUQik7_fH_cCJyb4S2iWw_A" style="margin-left: 1em; margin-right: 1em; text-align: left;"><img alt="" data-original-height="600" data-original-width="1766" height="136" src="https://blogger.googleusercontent.com/img/a/AVvXsEj74b_lTiCLB6m7BeRoEBoIvElQHjGrqNltXbsNxe95GXr69WOmJ4xUDrqQCIkEIy0Kn233xmNMcn0nqfRMyWExSOfxKq4P3r8_V9DEIPJDwHPMyJHI5cofDrDrhrnrAVrJmnWds66Nwlorvkzzp2LmzvYJsEW7d57CMI1JUQik7_fH_cCJyb4S2iWw_A=w400-h136" width="400" /></a></div><p> b) Set Mutual TLS authentication to not required, so that TLS connection will be allowed. </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEi1xCXP4Fw6Lh1J2sWqt7Ygbvs_ekFacD4uplYLmouoNg4TM56ajNLMqdvFMhqu_CH98rKUDKKWqenFmiBCz7CTcG3FKRX3hyoZM5StI3ORcXH1wwVzM7LJeJy5Nz7soQ_2fgQ1EktSP_phKv0xBuxK24sBGsWSr60NzBCPzQYf_Nf3a4Qgd_8nJ_jArA" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="147" data-original-width="454" height="130" src="https://blogger.googleusercontent.com/img/a/AVvXsEi1xCXP4Fw6Lh1J2sWqt7Ygbvs_ekFacD4uplYLmouoNg4TM56ajNLMqdvFMhqu_CH98rKUDKKWqenFmiBCz7CTcG3FKRX3hyoZM5StI3ORcXH1wwVzM7LJeJy5Nz7soQ_2fgQ1EktSP_phKv0xBuxK24sBGsWSr60NzBCPzQYf_Nf3a4Qgd_8nJ_jArA=w400-h130" width="400" /></a></div>2. Create a oracle function appliaction in the same VCN, which we configured in above step (1. a), if we wish to run the function in private subnet then few more things we need to take care, <a href="http://www.myexperimentswithjava.com/2022/06/oracle-functions-in-private-network.html" target="_blank">details can be found here</a><br /><p></p><p>3. Go lang function which will connect to ADB. </p><p>we will use godror library to connect to ADB. A standalone golang program connecting ADB will be found <a href="http://www.myexperimentswithjava.com/2021/07/how-to-connect-go-program-to-oracle.html" target="_blank">here</a>, the same concept we will use to write our function code. </p><div style="background-color: white; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; line-height: 18px; white-space: pre;"><div><span style="color: green;">/**</span></div><div><span style="color: green;"> * @Author Pallab (pallab.rath@gmail.com)</span></div><div><span style="color: green;"> */</span></div><br /><div><span style="color: blue;">package</span> main</div><br /><div><span style="color: blue;">import</span> (</div><div> <span style="color: #a31515;">"context"</span></div><div> <span style="color: #a31515;">"database/sql"</span></div><div> <span style="color: #a31515;">"encoding/json"</span></div><div> <span style="color: #a31515;">"fmt"</span></div><div> <span style="color: #a31515;">"io"</span></div><br /><div> fdk <span style="color: #a31515;">"github.com/fnproject/fdk-go"</span></div><div> _ <span style="color: #a31515;">"github.com/godror/godror"</span></div><div>)</div><br /><div><span style="color: blue;">func</span> main() {</div><div> fdk.Handle(fdk.HandlerFunc(myHandler))</div><div>}</div><br /><div><span style="color: blue;">func</span> myHandler(ctx context.Context, in io.Reader, out io.Writer) {</div><div> db_user := <span style="color: #a31515;">"<db-user>"</span></div><div> db_pwd := <span style="color: #a31515;">"<db-pwd>"</span></div><div> db_host := <span style="color: #a31515;">"<db-host>"</span></div><div> db_port := <span style="color: #a31515;">"<db-port>>"</span></div><div> db_srvc := <span style="color: #a31515;">"<db-service>"</span></div><br /><div> db_details := fmt.Sprintf(<span style="color: #a31515;">`user="%s" password="%s" connectString="tcps://%s:%s/%s"`</span>, db_user, db_pwd,</div><div> db_host, db_port, db_srvc)</div><div> db, err := sql.Open(<span style="color: #a31515;">"godror"</span>, db_details)</div><div> <span style="color: blue;">if</span> err != <span style="color: blue;">nil</span> {</div><div> fmt.Println(err)</div><div> <span style="color: blue;">return</span></div><div> }</div><div> <span style="color: blue;">defer</span> db.Close()</div><div> rows, err := db.Query(<span style="color: #a31515;">"select sysdate from dual"</span>)</div><div> <span style="color: blue;">if</span> err != <span style="color: blue;">nil</span> {</div><div> fmt.Println(<span style="color: #a31515;">"Error running query"</span>)</div><div> fmt.Println(err)</div><div> <span style="color: blue;">return</span></div><div> }</div><div> <span style="color: blue;">defer</span> rows.Close()</div><div> <span style="color: blue;">var</span> resData <span style="color: blue;">string</span></div><div> <span style="color: blue;">for</span> rows.Next() {</div><div> rows.Scan(&resData)</div><div> }</div><div> json.NewEncoder(out).Encode(&resData)</div><div>}</div><br /></div><div>In this program, we need to provide db user, pwd, host, port, and service name. we can find the host port and service name from tls connect string. The tls connection string can be found on the Database Connection page.</div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEj5AdgtQ3t5_ZJoJcBHykY97bn_bB23V7eImKq3_ilbguZ0Ql1LGOjOj2GB9mPaJpYQvinvsum9wlfyENGwVCT0Yi5nF7tOLbSKn6bWrdj22HDDDzWI9MaVZK2EppCtYf-DWAVyGsTEHJO5bKeh_qQh340K1whAzyaw6jDiJI3SIq5mxVpTCKIihlGUhQ" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="1062" data-original-width="1792" height="238" src="https://blogger.googleusercontent.com/img/a/AVvXsEj5AdgtQ3t5_ZJoJcBHykY97bn_bB23V7eImKq3_ilbguZ0Ql1LGOjOj2GB9mPaJpYQvinvsum9wlfyENGwVCT0Yi5nF7tOLbSKn6bWrdj22HDDDzWI9MaVZK2EppCtYf-DWAVyGsTEHJO5bKeh_qQh340K1whAzyaw6jDiJI3SIq5mxVpTCKIihlGUhQ=w400-h238" width="400" /></a></div><br />4. Building and publishing the above function.</div><div><br /></div><div>To build this function we need godror dependency, and this requires gcc compile to be present in build time env. So I created my custom build environment where gcc is installed, instead of the default <span style="background-color: white; color: blue; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;">fnproject/go:1.15-dev </span>build image.</div><div><br /></div><div>a) Create the build image docker definition (gofn_build.Dockerfile)</div><div><br /></div><div><div style="background-color: white; line-height: 18px;"><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">FROM</span> oraclelinux:8</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">RUN</span> yum -y install golang && yum -y clean all && rm -rf /var/cache</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><br /></div><div>Then build this image using the below command</div><div><br /></div><div>$ docker build -f gofn_build.Dockerfile -t gofn_build .</div><div><br /></div><div>b) Create the runtime image docker definition (gofn_runtime.Dockerfile), in the runtime container godror need oracle instant client.</div><div><div style="line-height: 18px;"><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;"><br /></span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">FROM</span> oraclelinux:8</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">WORKDIR</span> /function</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">RUN</span> curl https://download.oracle.com/otn_software/linux/instantclient/217000/oracle-instantclient-basiclite-21.7.0.0.0-1.el8.x86_64.rpm --output oracle-instantclient-basiclite-21.7.0.0.0-1.el8.x86_64.rpm</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">RUN</span> yum -y install oracle-instantclient-basiclite-21.7.0.0.0-1.el8.x86_64.rpm && yum -y clean all && rm -rf /var/cache</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">RUN</span> rm -f oracle-instantclient-basiclite-21.7.0.0.0-1.el8.x86_64.rpm</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><br /></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;">Then build this image using the below comman</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;"><br /></span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;">$ </span><span style="font-family: Times; font-size: medium; white-space: normal;">docker build -f gofn_build.Dockerfile -t gofn_runtime</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;"><br /></span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;">c) Create the custom Dockerfile with below content, where we use build go function in the build conatiner we just created in above steps and create a image of our runtime image and the executable function.</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;"><br /></span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><div style="line-height: 18px;"><div><span style="color: blue;">FROM</span> gofn_build <span style="color: blue;">as</span> build-stage</div><div><span style="color: blue;">WORKDIR</span> /function</div><div><span style="color: blue;">WORKDIR</span> /go/src/func/</div><div><span style="color: blue;">ENV</span> GO111MODULE=on</div><div><span style="color: blue;">COPY</span> . .</div><div><span style="color: blue;">RUN</span> cd /go/src/func/ && go build -o func</div><div><span style="color: blue;">FROM</span> gofn_runtime</div><div><span style="color: blue;">WORKDIR</span> /function</div><div><span style="color: blue;">COPY</span> --from=build-stage /go/src/func/func /function/</div><div><span style="color: blue;">ENTRYPOINT</span> [<span style="color: #a31515;">"./func"</span>]</div><div><br /></div></div></div><div>d) Finally change the func.yaml with below content</div><div><br /></div><div><div style="line-height: 18px;"><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: maroon;">schema_version</span>: <span style="color: #098658;">20180708</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: maroon;">name</span>: <span style="color: blue;">function-adb</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: maroon;">version</span>: <span style="color: #098658;">0.0.26</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: maroon;">runtime</span>: <span style="color: blue;">docker</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;"><br /></span></div><div>$ fn deploy -verbose<span style="color: blue; font-family: Menlo, Monaco, Courier New, monospace;"><span style="font-size: 12px; white-space: pre;"> <ap-name></span></span></div><div><span style="color: blue; font-family: Menlo, Monaco, Courier New, monospace;"><span style="font-size: 12px; white-space: pre;"><br /></span></span></div><div>Then fn deploy should do the job for us.</div><div><br /></div><div>$ fn invoke <span style="color: blue; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><ap-name></span></div><div>"2022-09-04T17:56:09Z"</div><div><span style="color: blue; font-family: Menlo, Monaco, Courier New, monospace;"><span style="font-size: 12px; white-space: pre;"><br /></span></span></div></div></div></div></div><div><br /></div><div>All these source code would be found in <a href="https://github.com/pallabrath/myexpjava/tree/master/function-adb">https://github.com/pallabrath/myexpjava/tree/master/function-adb</a></div></div></div>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-17749406113802527462022-08-02T21:50:00.001-07:002022-08-02T21:53:04.971-07:00 How to measure elapsed time of a method in Java<p>There are times we need to compute how much time is elapsed while executing a certain task. To compute elapsed time we can think something like below.</p><div style="background-color: white; line-height: 18px;"><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> ElapsedTimeDemo {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> main(<span style="color: blue;">String</span>[] args) {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">long</span> startTime = System.currentTimeMillis();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> doSomething();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">long</span> endTime = System.currentTimeMillis();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> System.out.println(<span style="color: #a31515;">"Elapsed time = "</span>+ (endTime - startTime));</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> }</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span> doSomething() {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> }</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;">}</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;"><br /></span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;">This code keeps track of start time of function execution and end time once the execution is complete. The difference between start time and end time gives us the elapsed time. This looks good. The only problem is that for start time and end time we are depending upon the system clock. Because of the clock skew system clock tends to drift away from the actual time. To make the system clock sync usual practice is computer connects to a time server and adjusts its own clock. While adjusting the system clock it may be set forward or backward based on the drift. This can cause some issues in the above program. If the clock is getting adjusted while the method execution of doSomething(), we will get the end time which can be either before or after the real end time. This makes the elapsed time calculation faulty.</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;">To make the correct elapsed time calculation consider the below program.</span></div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="font-family: Times; font-size: medium; white-space: normal;"><br /></span></div><div><div style="line-height: 18px;"><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> ElapsedTimeDemo {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> main(<span style="color: blue;">String</span>[] args) {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">long</span> startTime = System.nanoTime();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> doSomething();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">long</span> endTime = System.nanoTime();</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> System.out.println(<span style="color: #a31515;">"Elapsed time = "</span>+ (endTime - startTime));</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> }</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span> doSomething() {</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"> }</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;">}</div><div style="font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; white-space: pre;"><br /></div><div><span style="font-family: Times; font-size: medium; white-space: normal;">Here instead of </span><span style="font-family: Menlo, Monaco, Courier New, monospace;"><span style="font-size: 12px; white-space: pre;">currentTimeMillis() </span></span><span style="font-family: Times; font-size: medium; white-space: normal;">we are using </span><span style="font-family: Menlo, Monaco, Courier New, monospace;"><span style="font-size: 12px; white-space: pre;">nanoTime(), </span></span>Nano time comes from a JVM monotonic clock. A monotonic clock means it starts with a fixed origin, let's assume it's a counter that starts from 0 when JVM starts it keeps on incrementing. At any point in time counter value will not make much sense in absolute terms. In elapsed time calculation this is useful. And this doesn't get adjusted like a system clock. </div><div><br /></div><div><b>References</b></div><div><br /></div><div><a href="https://en.wikipedia.org/wiki/Clock_skew" target="_blank">Clock Skew</a></div><div><a href="https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/System.html#currentTimeMillis()" target="_blank">System.currentTimeMillis()</a></div><div><a href="https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/System.html#nanoTime()" target="_blank">System.nanoTime()</a></div><div><br /></div></div></div></div>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-59255721182125092752022-06-10T00:37:00.001-07:002022-06-10T00:37:17.145-07:00Oracle Functions in private networkOCI Functions is a server-less platform. In this blog post, we will see how to run oracle functions in a private network. While creating the <a href="https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionscreatingapps.htm#Creating_Applications" target="_blank">Application</a> we need to select the desired VCN and private subnet.<div>Then these are the few things we need to configure for the subnet so that function can run.</div><div><br /></div><div>1) Service Gateway to reach out OCI service</div><div>The function application in the private network needs to connect to the container registry and download the required image. To achieve this we need a Service gateway in the VCN. In the console Network -> Virtual Cloud Network page we can edit the VCN to add a service gateway.</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhZ4N2pC3UjU6nvGl20n1L3pOJmwoEDtrc3nufvW82nzhVi9nQ6B0RekwSFbVocUKT2LtZh1DeiXHDqlpTgmoI4WBrDqwfCxhbvgOctd5mbe6Z7E3H2YYGPC__H46tqv0Siypnr7QEriLEIJyuEbFfb7nipmWZuA9yuIvXr8S1XKESKPM80bQuEdEdkkQ" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="478" data-original-width="530" height="289" src="https://blogger.googleusercontent.com/img/a/AVvXsEhZ4N2pC3UjU6nvGl20n1L3pOJmwoEDtrc3nufvW82nzhVi9nQ6B0RekwSFbVocUKT2LtZh1DeiXHDqlpTgmoI4WBrDqwfCxhbvgOctd5mbe6Z7E3H2YYGPC__H46tqv0Siypnr7QEriLEIJyuEbFfb7nipmWZuA9yuIvXr8S1XKESKPM80bQuEdEdkkQ=w320-h289" width="320" /></a></div><br /><br /></div><div><br /></div><div>2) Route Rule for service gateway.</div><div><br /></div><div>In the private subnet where the application is running, there would be an attached route table, and in that table, we need to add a route rule saying the OCI service calls need to be routed through the service gateway we had created in the previous step.</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhUzEuatNNcKpcWrHvgiiNeyNe-EVJZbVsEXBeZvRClgFzxqEQk6V6Gqr32g7F4IfQ8COuN17p2AmlgOi726YBAdB2zW5VJfL0344mJndfqcLWTVW0AEju6LZoYV402-j9geUl1eA8gE-stdR34XX6LtDWUb6etm98TpiRXadsDzgLwuVkxnUu5bjnnPQ" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="404" data-original-width="948" height="170" src="https://blogger.googleusercontent.com/img/a/AVvXsEhUzEuatNNcKpcWrHvgiiNeyNe-EVJZbVsEXBeZvRClgFzxqEQk6V6Gqr32g7F4IfQ8COuN17p2AmlgOi726YBAdB2zW5VJfL0344mJndfqcLWTVW0AEju6LZoYV402-j9geUl1eA8gE-stdR34XX6LtDWUb6etm98TpiRXadsDzgLwuVkxnUu5bjnnPQ=w400-h170" width="400" /></a></div></div><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><blockquote style="border: none; margin: 0 0 0 40px; padding: 0px;"><div class="separator" style="clear: both; text-align: center;"><br /></div></blockquote></blockquote><div>3) Secure Egress Rule</div><div><br /></div><div>In that particular subnet we need to allow traffic from the subnet to the OCI service, to do so we will add a stateful Egress Rule in the security list of the subnet</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEi92hPo2ce5CDpZci6EPkr5rhzGN5LMtfF5Zw_SyvreC5rRH3v54YX35X5mzr-JI1PODvGjiLNBEnvXqLF6bOvvWjZO-Sof8wxXfhYY31T0JHvAnjVayF5wL3nLAV2QPpdJczn0OlCYlJv2orFNsHH1w7-pnfyYFsOwTngLm9tb6tyObNPy7ObfS_Ydqw" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="321" data-original-width="721" height="178" src="https://blogger.googleusercontent.com/img/a/AVvXsEi92hPo2ce5CDpZci6EPkr5rhzGN5LMtfF5Zw_SyvreC5rRH3v54YX35X5mzr-JI1PODvGjiLNBEnvXqLF6bOvvWjZO-Sof8wxXfhYY31T0JHvAnjVayF5wL3nLAV2QPpdJczn0OlCYlJv2orFNsHH1w7-pnfyYFsOwTngLm9tb6tyObNPy7ObfS_Ydqw=w400-h178" width="400" /></a></div><br /><br /><br /></div><div><br /></div><div>After these steps function should be able to reach out desired OCI service and run.</div><div><br /></div><div>References:</div><div><br /></div><div><a href="https://docs.oracle.com/en-us/iaas/Content/Network/Tasks/servicegateway.htm" target="_blank">Access to Oracle Services: Service Gateway</a><br /></div><div><a href="https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsquickstartguidestop.htm" target="_blank">Functions QuickStart Guides</a><br /></div><div><br /></div>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-82352148077956166492022-01-12T09:03:00.008-08:002022-01-12T09:33:55.812-08:00How to connect Java a program to Oracle Autonomous Database over TLS without wallet<p>In this blog post, we will discuss how to connect Java a program using JDBC thin driver to Oracle Autonomous Database over TLS without a wallet.</p><p>With TLS support we can now connect to ADB without the credential wallet.</p><p><b>Part 1: </b>We need to configure ADB for TLS to get the TLS connection string.</p><p>a) For the ADB we want to connect over TLS, In the Autonomous database details page, we need to set Mutual TLS authentication (mTLS) as not required. </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDJ0fZbgGuvjdPlnhlN_wghkLr8ibqTJwpkDHwAGefLYBVUV2Tufs0QVwzsvBQj8pc3O6oDFiHtjqV2ZGAo70-FVVG3jnmk__jmNmbJJ3lfTd5YWy4svMVrCvky30aBxCKdxIMWpcHzlPe/" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="588" data-original-width="1164" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDJ0fZbgGuvjdPlnhlN_wghkLr8ibqTJwpkDHwAGefLYBVUV2Tufs0QVwzsvBQj8pc3O6oDFiHtjqV2ZGAo70-FVVG3jnmk__jmNmbJJ3lfTd5YWy4svMVrCvky30aBxCKdxIMWpcHzlPe/w400-h203/Screenshot+2022-01-12+at+9.03.38+PM.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;">b) Then from the database connection page (we can navigate to it by clicking on DB connection button in the ADB console) chose "TLS" as TLS authentication and copy the connection string for desired TNS name. In this example, I had copied <span face=""Helvetica Neue", Helvetica, Arial, sans-serif" style="background-color: #f5f9fd; caret-color: rgb(34, 34, 34); color: #222222; font-size: 14px;">demodb_medium</span></div><div class="separator" style="clear: both; text-align: left;"><span face=""Helvetica Neue", Helvetica, Arial, sans-serif" style="background-color: #f5f9fd; caret-color: rgb(34, 34, 34); color: #222222; font-size: 14px;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span face=""Helvetica Neue", Helvetica, Arial, sans-serif" style="background-color: #f5f9fd; caret-color: rgb(34, 34, 34); color: #222222; font-size: 14px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwP6bT0oGXiNgmklA-0NzZD2LVzibisaRxF3NuCxRJnddPYTArJNTw2Djv9znC0Is0Fg202OqE4epOQPjDwMkLHaor2wmY3VKCaoDBcKcnBG_cvhxHGRMLnU6d-Qhql25J0E8YsZvV5jh6/" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="964" data-original-width="1824" height="338" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwP6bT0oGXiNgmklA-0NzZD2LVzibisaRxF3NuCxRJnddPYTArJNTw2Djv9znC0Is0Fg202OqE4epOQPjDwMkLHaor2wmY3VKCaoDBcKcnBG_cvhxHGRMLnU6d-Qhql25J0E8YsZvV5jh6/w640-h338/Screenshot+2022-01-12+at+9.16.25+PM.png" width="640" /></a></div><br /><br /></span></div><p></p><p>Part 2: Java program which uses the above connect string in jdbc to execute sql statements.</p><p>Prereq : <code style="background-attachment: inherit; background-clip: inherit; background-color: #fcfbfa; background-image: inherit; background-origin: inherit; background-position: inherit; background-repeat: inherit; background-size: inherit; border-radius: 4px; box-sizing: border-box; color: #1a1816; font-family: "Courier New", Courier, monospace; font-size: 16px; padding: 0px;">ojdbc8.jar</code><span face=""Oracle Sans", -apple-system, "system-ui", "Segoe UI", "Helvetica Neue", Arial, sans-serif" style="background-color: #fcfbfa; color: #1a1816; font-size: 16px;"> and </span><code style="background-attachment: inherit; background-clip: inherit; background-color: #fcfbfa; background-image: inherit; background-origin: inherit; background-position: inherit; background-repeat: inherit; background-size: inherit; border-radius: 4px; box-sizing: border-box; color: #1a1816; font-family: "Courier New", Courier, monospace; font-size: 16px; padding: 0px;">ucp.jar</code></p><p><code style="background-attachment: inherit; background-clip: inherit; background-color: #fcfbfa; background-image: inherit; background-origin: inherit; background-position: inherit; background-repeat: inherit; background-size: inherit; border-radius: 4px; box-sizing: border-box; color: #1a1816; font-family: "Courier New", Courier, monospace; font-size: 16px; padding: 0px;"><span style="color: black; font-family: Times; font-size: medium;">I have used JDK 11 in this eample</span></code></p><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">package </span>demo<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">import </span>java.sql.*<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;">import </span>java.util.Properties<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">public class </span>ADBSharedTLSConnect {<br /> <span style="color: #cc7832;">private static </span>String <span style="color: #9876aa; font-style: italic;">atps_tls </span>= <span style="color: #6a8759;">"(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.ap-mumbai-1.oraclecloud.com))(connect_data=(service_name=rks9000p5ge4_demodb_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)(ssl_server_cert_dn=</span><span style="color: #cc7832;">\"</span><span style="color: #6a8759;">CN=adb.ap-mumbai-1.oraclecloud.com, OU=Oracle ADB INDIA, O=Oracle Corporation, L=Redwood City, ST=California, C=US</span><span style="color: #cc7832;">\"</span><span style="color: #6a8759;">)))"</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> private static </span>String <span style="color: #9876aa; font-style: italic;">db_url </span>= <span style="color: #6a8759;">"jdbc:oracle:thin:@" </span>+ <span style="color: #9876aa; font-style: italic;">atps_tls</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> private static </span>String <span style="color: #9876aa; font-style: italic;">dbUser </span>= <span style="color: #6a8759;">"admin"</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> private static </span>String <span style="color: #9876aa; font-style: italic;">dbPwd </span>= <span style="color: #6a8759;">"test@ATP122245"</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> public static void </span><span style="color: #ffc66d;">main</span>(String[] args) {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Connecting to ATPS over TLS..."</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>ResultSet rs = <span style="color: #cc7832;">null;<br /></span><span style="color: #cc7832;"> </span>Statement stmt = <span style="color: #cc7832;">null;<br /></span><span style="color: #cc7832;"> </span>Connection con = <span style="color: #cc7832;">null;<br /></span><span style="color: #cc7832;"> try </span>{<br /> Class.<span style="font-style: italic;">forName</span>(<span style="color: #6a8759;">"oracle.jdbc.driver.OracleDriver"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>Properties props = <span style="color: #cc7832;">new </span>Properties()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>props.setProperty(<span style="color: #6a8759;">"user"</span><span style="color: #cc7832;">, </span><span style="color: #9876aa; font-style: italic;">dbUser</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>props.setProperty(<span style="color: #6a8759;">"password"</span><span style="color: #cc7832;">, </span><span style="color: #9876aa; font-style: italic;">dbPwd</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>props.setProperty(<span style="color: #6a8759;">"oracle.jdbc.fanEnabled"</span><span style="color: #cc7832;">, </span><span style="color: #6a8759;">"false"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>con = DriverManager.<span style="font-style: italic;">getConnection</span>(<span style="color: #9876aa; font-style: italic;">db_url</span><span style="color: #cc7832;">, </span>props)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>stmt = con.createStatement()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>rs = stmt.executeQuery(<span style="color: #6a8759;">"select sysdate from dual"</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> while </span>(rs.next()) {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(rs.getString(<span style="color: #6897bb;">1</span>))<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Demo Over..."</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> </span>} <span style="color: #cc7832;">catch </span>(Exception e) {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(e)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>} <span style="color: #cc7832;">finally </span>{<br /> <span style="color: #cc7832;">try </span>{<br /> <span style="color: #cc7832;">if </span>(rs != <span style="color: #cc7832;">null</span>) {<br /> rs.close()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> <span style="color: #cc7832;">if </span>(stmt != <span style="color: #cc7832;">null</span>) {<br /> stmt.close()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> <span style="color: #cc7832;">if </span>(con != <span style="color: #cc7832;">null</span>) {<br /> con.close()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> } <span style="color: #cc7832;">catch </span>(SQLException e) {<br /> e.printStackTrace()<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /> }<br /> }<br />}</pre><p>atps_tls is the one that I had copied from part 1 step b.</p><p>I have used oracle.jdbc.fanEnabled property as false, without this configuration there was an error </p><p><span color="rgba(0, 0, 0, 0.847)" face="-apple-system-font" style="caret-color: rgba(0, 0, 0, 0.847); font-size: 12px; text-size-adjust: auto;">SEVERE: attempt to configure ONS in FanManager failed with oracle.ons.NoServersAvailable: Subscription time out</span></p><p>Although query was getting executed.</p><p>Some useful links</p><p>1. <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-tls.html#GUID-364DB7F0-6F4F-4C42-9395-4BA4D09F0483" target="_blank">JDBC connection without wallet</a></p><p>2. <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/support-tls-mtls-authentication.html#GUID-6A34B30A-3692-4D1F-8458-FD8F32736199" target="_blank">Update your Autonomous Database Instance to Allow both TLS and mTLS Authentication</a></p><p>3. <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-download-wallet.html#GUID-BE884A1B-034D-4CD6-9B71-83A4CCFDE9FB" target="_blank">View TNS Names and Connection Strings for an Autonomous Database Instance</a></p><p>4. <a href="https://github.com/pallabrath/myexpjava/tree/master/ADBTLSConnet" target="_blank">Source Code in git</a></p>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com2tag:blogger.com,1999:blog-8776378117389609026.post-8691708286070863532021-07-19T00:32:00.008-07:002021-07-19T00:53:41.916-07:00How to Connect a Go program to Oracle Autonomous Database<p>In this blog post, we will see how to connect <a href="https://docs.oracle.com/en/database/autonomous-database-cloud-services.html" target="_blank">Oracle Autonomous Database</a> in Go programming language. This can be divided into three parts.</p><p><b>Part 1)</b> We need to download Autonomous Database client credentials. <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/adbdu/download-client-credentials.html#GUID-9F5AD1CB-5DAD-44C4-8978-C485575E23C2">More details</a></p><p>In the Autonomous database OCI console, open to the service console of the DB which we need to connect. There in the Administration tab, we will find the option to download the client credentials.</p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9WLqCBVSKnC2hgQg69rUeGTMvEXYmjEuKifDR2VceLqGk8FWY6Ea4eKGyC0f3_Gay72oVovpo1HCeTSJ3-6nSN0njpnUY1Ck3rSmvor8ERc0wNBYC8KwlTO1nzJld08pmWxhOgZm3e6VH/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="280" data-original-width="827" height="135" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9WLqCBVSKnC2hgQg69rUeGTMvEXYmjEuKifDR2VceLqGk8FWY6Ea4eKGyC0f3_Gay72oVovpo1HCeTSJ3-6nSN0njpnUY1Ck3rSmvor8ERc0wNBYC8KwlTO1nzJld08pmWxhOgZm3e6VH/w400-h135/Screenshot+2021-07-19+at+12.23.43+PM.png" width="400" /></a></div><br /><p></p><p> Once It is downloaded, unzip the same to a directory of choice.</p><p><b>Part 2)</b> To connect to the database we need the database user, password, and the connect string.</p><p>We can use the admin user, which was provided as part of the database provision, or any other database user. Here we need to form the connect string.</p><p>example: <span style="background-color: whitesmoke; white-space: pre-wrap;"><span style="color: #2b00fe; font-family: inherit;">protocol://host:port/service_name?wallet_location=/my/dir&retry_count=3&retry_delay=20</span></span></p><p>We need to form our connect string for the autonomous database we want to connect. We will get details in the <b>tnsnames.ora</b> file which is part of the zip we had downloaded in above Part 1.</p><p>There we will find many connection services (<a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/adbdu/predefined-database-service-names.html#GUID-07401C82-6A01-4CDF-AAE5-54B78B50CA0A">more details about them</a>), We can pick the one we want to use for our connection.</p><p>example: </p><p><span style="color: #2b00fe;">db202107181649_medium = (description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-mumbai-1.oraclecloud.com))(connect_data=(service_name=iro8q5fzknp5ge4_db202107181649_medium.adb.oraclecloud.com))(security=(ssl_server_cert_dn="CN=adb.ap-mumbai-1.oraclecloud.com, OU=Oracle ADB INDIA, O=Oracle Corporation, L=Redwood City, ST=California, C=US")))</span></p><p>Now by referring to this we can create our connection string like below.</p><p><span style="color: #2b00fe;">tcps://adb.ap-mumbai-1.oraclecloud.com:1522/iro8q5fzknp5ge4_db202107181649_medium.adb.oraclecloud.com?wallet_location=/Users/pallab/tool/wallet_DB/</span></p><p>The wallet_location is the path where we had unzipped our client credentials in above Part 1.</p><p><b>Part 3)</b> Now let's write our go code.</p><p>We will use <a href="https://github.com/godror/godror">godror</a> package to connect. </p><p>Install godor # <span face="SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace" style="color: #24292e;">go get github.com/godror/godror</span></p><p>We need runtime dependency for Oracle Client libraries, We can download the free basic or free light version from <a href="https://www.oracle.com/database/technologies/instant-client/downloads.html" style="background-color: white; box-sizing: border-box; color: #0366d6; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; text-decoration-line: none;">https://www.oracle.com/database/technologies/instant-client/downloads.html</a><span face="-apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="background-color: white; color: #24292e; font-size: 16px;">.</span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; line-height: 18px; white-space: pre;"><div><span style="color: #569cd6;">package</span> main</div><br /><div><span style="color: #569cd6;">import</span> (</div><div> <span style="color: #ce9178;">"database/sql"</span></div><div> <span style="color: #ce9178;">"fmt"</span></div><br /><div> _ <span style="color: #ce9178;">"github.com/godror/godror"</span></div><div>)</div><br /><div><span style="color: #569cd6;">func</span> <span style="color: #dcdcaa;">main</span>() {</div><div> <span style="color: #dcdcaa;">connectToADb</span>()</div><div>}</div><br /><div><span style="color: #569cd6;">func</span> <span style="color: #dcdcaa;">connectToADb</span>() {</div><div> fmt.<span style="color: #dcdcaa;">Println</span>(<span style="color: #ce9178;">"Connecting to Oracle Autonoumus database !!!"</span>)</div><div> <span style="color: #9cdcfe;">db</span>, <span style="color: #9cdcfe;">err</span> := sql.<span style="color: #dcdcaa;">Open</span>(<span style="color: #ce9178;">"godror"</span>, <span style="color: #ce9178;">`user="admin" password="testPwd" </span></div><div><span style="color: #ce9178;"> connectString="tcps://adb.ap-mumbai-1.oraclecloud.com:1522/iro8q5fzknp5ge4_db202107181649_medium.adb.oraclecloud.com?</span><span style="color: #ce9178;">wallet_location=/Users/pallab/tool/wallet_DB/"</span></div><div><span style="color: #ce9178;"> libDir="/Users/pallab/tool/instantclient_19_8/"`</span>)</div><div> <span style="color: #c586c0;">if</span> err != <span style="color: #569cd6;">nil</span> {</div><div> fmt.<span style="color: #dcdcaa;">Println</span>(err)</div><div> <span style="color: #c586c0;">return</span></div><div> }</div><div> <span style="color: #c586c0;">defer</span> db.<span style="color: #dcdcaa;">Close</span>()</div><br /><div> <span style="color: #9cdcfe;">rows</span>, <span style="color: #9cdcfe;">err</span> := db.<span style="color: #dcdcaa;">Query</span>(<span style="color: #ce9178;">"select 'hello' from dual"</span>)</div><div> <span style="color: #c586c0;">if</span> err != <span style="color: #569cd6;">nil</span> {</div><div> fmt.<span style="color: #dcdcaa;">Println</span>(<span style="color: #ce9178;">"Error running query"</span>)</div><div> fmt.<span style="color: #dcdcaa;">Println</span>(err)</div><div> <span style="color: #c586c0;">return</span></div><div> }</div><div> <span style="color: #c586c0;">defer</span> rows.<span style="color: #dcdcaa;">Close</span>()</div><br /><div> <span style="color: #569cd6;">var</span> <span style="color: #9cdcfe;">resData</span> <span style="color: #4ec9b0;">string</span></div><div> <span style="color: #c586c0;">for</span> rows.<span style="color: #dcdcaa;">Next</span>() {</div><br /><div> rows.<span style="color: #dcdcaa;">Scan</span>(&resData)</div><div> }</div><div> fmt.<span style="color: #dcdcaa;">Printf</span>(<span style="color: #ce9178;">"The response is: %s</span><span style="color: #d7ba7d;">\n</span><span style="color: #ce9178;">"</span>, resData)</div><div>}</div></div><p>Note: In sql.open() we have passed our oracle client libraries which we downloaded and unzipped as <b>libDir</b> parameter.</p><p><b>Useful Links</b></p><p>1. <a href="https://blogs.oracle.com/opal/how-connect-to-oracle-autonomous-cloud-databases" target="_blank">https://blogs.oracle.com/opal/how-connect-to-oracle-autonomous-cloud-databases</a></p><p>2. <a href="https://blogs.oracle.com/developers/how-to-connect-a-go-program-to-oracle-database-using-goracle">https://blogs.oracle.com/developers/how-to-connect-a-go-program-to-oracle-database-using-goracle</a></p><p>3. <a href="https://godror.github.io/godror/doc/installation.html">https://godror.github.io/godror/doc/installation.html</a></p><p><br /></p><p><br /></p><p><br /></p>Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com1tag:blogger.com,1999:blog-8776378117389609026.post-80815908096870375302020-08-10T08:30:00.002-07:002020-08-10T21:42:17.634-07:00Hosting a static website on Oracle Object Storage<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">
We can use <a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Object/Concepts/objectstorageoverview.htm" target="_blank">Oracle cloud Object Storage</a> to host a static website. Static web pages can contain client-side technologies such as HTML, CSS, and JavaScript. They cannot contain dynamic content such as server-side scripts like PHP, JSP or ASP.NET.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">Step 1:</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">We need to create a public bucket and </span><span style="font-family: arial;">upload the static website contents. Once the bucket is created edit the visibility to give public access. </span></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><img border="0" data-original-height="273" data-original-width="723" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6zXuQL6LB82-Zt2meYjkJOL03KsbGkFA83PZyEo19ZoGZSMHgqzVU4g9RWzZgcN6-IFUQOgQTmNMYndcQNa04p2Efw_Xnk5OX8Fz5gTFEsbSYX8Hka0rK22_GwUsn7wMP8KL03QIkE75z/s640/Screenshot+2020-08-10+at+8.27.36+PM.png" width="640" /></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">Step 2:</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">Upload the files, here is the trick. Object storage doesn't support the hierarchy of folder structure. So we have to name each file based on its path.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">For example, we have a index.html that has a link to the file page-1.html inside the pages folder.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">We have to upload index.html as index.html and page-1.html as "pages/page-1.html". So that links will work.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"> </span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">I know this can be tedious since a website contains hundreds of html files, images, css and javascript. So I have automated this process. </span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><b>Upload Tool</b></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><b><br /></b></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">The scripts are available</span><span style="font-family: arial;"> at </span><a href="https://github.com/pallabrath/myexpjava/tree/master/oci-os-static-web-upload-util">https://github.com/pallabrath/myexpjava/tree/master/oci-os-static-web-upload-util</a></div><div dir="ltr" style="text-align: left;" trbidi="on">It can be cloned by git or download as a zip (<a href="https://github.com/pallabrath/myexpjava">https://github.com/pallabrath/myexpjava</a>) and then unzip</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">1. Move to the oci-os-static-web-upload-util directory.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">2. To run the script we need node and npm to be installed in our environment.</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><span> </span><span style="background-color: white; color: #24292e; font-size: 16px;">Node -</span><span style="background-color: white; color: #24292e; font-size: 16px;"> </span><a href="https://nodejs.org/en/" rel="nofollow" style="box-sizing: border-box; color: #0366d6; font-size: 16px; text-decoration-line: none;">https://nodejs.org/en/</a><span><br /></span><span style="background-color: white; color: #24292e; font-size: 16px;"><span> </span>Npm -</span><span style="background-color: white; color: #24292e; font-size: 16px;"> </span><a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm" rel="nofollow" style="box-sizing: border-box; color: #0366d6; font-size: 16px; text-decoration-line: none;">https://docs.npmjs.com/downloading-and-installing-node-js-and-npm</a></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><span>3. </span><span style="background-color: white; color: #24292e; font-size: 16px;">Required inputs for this tool need to be configured in</span><span style="background-color: white; color: #24292e; font-size: 16px;"> </span><span style="box-sizing: border-box; color: #24292e; font-size: 16px; font-weight: 600;"><em style="box-sizing: border-box;">upload-config.json</em></span><span style="background-color: white; color: #24292e; font-size: 16px;">.</span></span></div><div class="highlight highlight-source-js" style="background-color: white; box-sizing: border-box; color: #24292e; font-size: 16px; margin-bottom: 16px;"><pre style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: normal;"><span class="pl-kos" style="box-sizing: border-box;">{</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"webdir"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"/Users/pallab/mylab/oci-os-static-web"</span><span class="pl-kos" style="box-sizing: border-box;">,</span> # <span class="pl-v" style="box-sizing: border-box; color: #e36209;">This</span> <span class="pl-s1" style="box-sizing: border-box;">is</span> <span class="pl-s1" style="box-sizing: border-box;">the</span> <span class="pl-s1" style="box-sizing: border-box;">path</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">of</span> <span class="pl-s1" style="box-sizing: border-box;">the</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">static</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">web</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">need</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">to</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">be</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">uploaded</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"index"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"index.html"</span><span class="pl-kos" style="box-sizing: border-box;">,</span> # <span class="pl-v" style="box-sizing: border-box; color: #e36209;">Its</span> <span class="pl-s1" style="box-sizing: border-box;">the</span> <span class="pl-s1" style="box-sizing: border-box;">index</span>/<span class="pl-s1" style="box-sizing: border-box;">home</span> <span class="pl-s1" style="box-sizing: border-box;">page</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">of</span> <span class="pl-s1" style="box-sizing: border-box;">your</span> <span class="pl-s1" style="box-sizing: border-box;">website</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"configurationFilePath"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"~/.oci/config"</span><span class="pl-kos" style="box-sizing: border-box;">,</span> # <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">OCI</span> <span class="pl-s1" style="box-sizing: border-box;">credential</span> <span class="pl-s1" style="box-sizing: border-box;">configuration</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"configProfile"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"DEFAULT"</span><span class="pl-kos" style="box-sizing: border-box;">,</span> # <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">OCI</span> <span class="pl-s1" style="box-sizing: border-box;">credentail</span> <span class="pl-s1" style="box-sizing: border-box;">config</span> <span class="pl-s1" style="box-sizing: border-box;">profile</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"comaprtmentOCId"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"ocid1.compartment.oc1......"</span><span class="pl-kos" style="box-sizing: border-box;">,</span> # <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">OCI</span> <span class="pl-s1" style="box-sizing: border-box;">compartment</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">OCID</span> <span class="pl-s1" style="box-sizing: border-box;">where</span> <span class="pl-s1" style="box-sizing: border-box;">we</span> <span class="pl-s1" style="box-sizing: border-box;">want</span> <span class="pl-s1" style="box-sizing: border-box;">to</span> <span class="pl-s1" style="box-sizing: border-box;">upload</span>
<span class="pl-s" style="box-sizing: border-box; color: #032f62;">"bucketName"</span> : <span class="pl-s" style="box-sizing: border-box; color: #032f62;">"myexpdemo"</span> # <span class="pl-s1" style="box-sizing: border-box;">bucket</span> <span class="pl-s1" style="box-sizing: border-box;">name</span> <span class="pl-s1" style="box-sizing: border-box;">to</span> <span class="pl-s1" style="box-sizing: border-box;">be</span> <span class="pl-s1" style="box-sizing: border-box;">created</span>
}<span style="background-color: transparent;"> </span></pre></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><span style="background-color: white; color: #24292e; font-size: 16px;">Details how to create OCI Credential configuration refer # </span><a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/typescriptsdkgettingstarted.htm#Getting_Started" rel="nofollow" style="background-color: white; box-sizing: border-box; color: #0366d6; font-size: 16px; text-decoration-line: none;">https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/typescriptsdkgettingstarted.htm#Getting_Started</a></span><span><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">Example :</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><pre class="pre codeblock scrollcopy" style="background: rgb(255, 255, 255); border-radius: 3px; border: 1px solid rgb(196, 206, 215); color: #666666; font-size: 0.875rem; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: 1.5; margin-bottom: 20px; margin-top: 0px; overflow: auto; padding: 38px 10px 10px; tab-size: 4;"><code style="word-break: break-word;">[DEFAULT]
user=ocid1.user.oc1..<your_unique_id>
fingerprint=<your_fingerprint>
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..<your_unique_id>
customCompartmentId=ocid1.compartment.oc1..<your_unique_id></code></pre></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;">How to create OCI API signing key</span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Functions/Tasks/functionssetupapikey.htm">https://docs.cloud.oracle.com/en-us/iaas/Content/Functions/Tasks/functionssetupapikey.htm</a></span></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">4. Once the credentials are set and inputs are provided in upload-config.json. We are all set to run the util.</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhY7uid7GDhos3hOYff_Q23dHizPLrm1MCzZ53eGA3vL7MJN2qf-perHBDBbKPPLFmMZ3i89JV5-MH51oEv_-oqSRPldHrXSRWIcLaXO9fa7CvH-ExkUiJpgsSVo_vdMMG2toLxD2ZTmAV/s1752/upload-util-screen.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="652" data-original-width="1752" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhY7uid7GDhos3hOYff_Q23dHizPLrm1MCzZ53eGA3vL7MJN2qf-perHBDBbKPPLFmMZ3i89JV5-MH51oEv_-oqSRPldHrXSRWIcLaXO9fa7CvH-ExkUiJpgsSVo_vdMMG2toLxD2ZTmAV/s640/upload-util-screen.png" width="640" /></a></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">This will create a bucket with visibility public and<span style="font-family: arial;"> <span style="background-color: white; color: #24292e; font-size: 16px;">publicAccessType = ObjectReadWithoutList.</span></span></div><div dir="ltr" style="text-align: left;" trbidi="on">It will upload all the files in the user-selected folder (mentioned as webdir in upload-config.json) to the bucket. </div><div dir="ltr" style="text-align: left;" trbidi="on">The util will print the url of the index file. This will be the website homepage url.</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><b>Custom Domain</b></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">We can configure this as a http redirect or url forwarding to use the custom domain. </div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjb6C6EzASgqvgfQzF5oZD0jVNhjAdLW0sH5SENLdpePDSlmUbYLb8SAZDdhuQfkJXiZzbFuPc67tOjMxOPb5iVDyLGggQkSk0g2jKClqk1-pRTaF6NgVeYDSHTA8EW6-UEBNezgqT1S4Gv/s723/Screenshot+2020-08-11+at+10.02.45+AM.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="723" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjb6C6EzASgqvgfQzF5oZD0jVNhjAdLW0sH5SENLdpePDSlmUbYLb8SAZDdhuQfkJXiZzbFuPc67tOjMxOPb5iVDyLGggQkSk0g2jKClqk1-pRTaF6NgVeYDSHTA8EW6-UEBNezgqT1S4Gv/s640/Screenshot+2020-08-11+at+10.02.45+AM.png" width="640" /></a></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: arial;"><br /></span></div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-58183729505975570652020-05-10T09:42:00.001-07:002020-05-12T06:11:52.981-07:00Run a Spring Boot application on OCI Compute Instance<div dir="ltr" style="text-align: left;" trbidi="on">
In this article, we will see how to deploy a spring boot application in Oracle cloud compute instance.<br />
<br />
<h3 style="text-align: left;">
<span style="background-color: white; color: #212121; font-family: "google sans" , "noto sans" , "noto sans jp" , "noto sans kr" , "noto naskh arabic" , "noto sans thai" , "noto sans hebrew" , "noto sans bengali" , sans-serif; font-size: small;">Creating a new Spring Boot app and running it locally</span></h3>
<div>
<span style="background-color: white; color: #212121; font-family: "google sans" , "noto sans" , "noto sans jp" , "noto sans kr" , "noto naskh arabic" , "noto sans thai" , "noto sans hebrew" , "noto sans bengali" , sans-serif; font-size: small;"><br /></span></div>
<div>
<span style="font-family: inherit;">We will create a spring boot app and test locally. </span></div>
<div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><a href="https://start.spring.io/" style="background-color: white; box-sizing: inherit; color: #1a73e8; outline: 0px;">start.spring.io</a><span style="background-color: white; color: #212121;"> can be used to start. Or there is a sample Hello World project I have created can be referred </span></span><a href="https://github.com/pallabrath/myexpjava/tree/master/spring-boot/spring-web-rest-demo">https://github.com/pallabrath/myexpjava/tree/master/spring-boot/spring-web-rest-demo</a></span></div>
<div>
<span style="background-color: white; color: #212121; font-family: inherit; font-size: 16px;"><br /></span></div>
<div>
<table class="highlight tab-size js-file-line-container" data-paste-markdown-skip="" data-tab-size="8" style="background-color: white; border-collapse: collapse; border-spacing: 0px; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 14px; tab-size: 8;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><td class="blob-code blob-code-inner js-file-line" id="LC1" style="box-sizing: border-box; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top;"><span style="font-family: "courier new" , "courier" , monospace; font-size: small;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">package</span><span style="white-space: pre;"> </span><span class="pl-smi" style="box-sizing: border-box; white-space: pre;">myexpjava.demo</span><span style="white-space: pre;">;
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">import</span><span style="white-space: pre;"> </span><span class="pl-smi" style="box-sizing: border-box; white-space: pre;">org.springframework.stereotype.Controller</span><span style="white-space: pre;">;
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">import</span><span style="white-space: pre;"> </span><span class="pl-smi" style="box-sizing: border-box; white-space: pre;">org.springframework.web.bind.annotation.GetMapping</span><span style="white-space: pre;">;
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">import</span><span style="white-space: pre;"> </span><span class="pl-smi" style="box-sizing: border-box; white-space: pre;">org.springframework.web.bind.annotation.ResponseBody</span><span style="white-space: pre;">;
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">@Controller</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">public</span><span style="white-space: pre;"> </span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;">class</span><span style="white-space: pre;"> </span><span class="pl-en" style="box-sizing: border-box; color: #6f42c1; white-space: pre;">DemoController</span><span style="white-space: pre;"> {
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;"> @GetMapping</span><span style="white-space: pre;">(</span><span class="pl-s" style="box-sizing: border-box; color: #032f62; white-space: pre;"><span class="pl-pds" style="box-sizing: border-box;">"</span>/hello<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span style="white-space: pre;">)
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;"> @ResponseBody</span><span style="white-space: pre;">
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;"> public</span><span style="white-space: pre;"> </span><span class="pl-smi" style="box-sizing: border-box; white-space: pre;">String</span><span style="white-space: pre;"> </span><span class="pl-en" style="box-sizing: border-box; color: #6f42c1; white-space: pre;">sayHello</span><span style="white-space: pre;">() {
</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49; white-space: pre;"> return</span><span style="white-space: pre;"> </span><span class="pl-s" style="box-sizing: border-box; color: #032f62; white-space: pre;"><span class="pl-pds" style="box-sizing: border-box;">"</span>Hello World !!!<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span style="white-space: pre;">;
}
}</span></span><span style="font-family: , "consolas" , "liberation mono" , "menlo" , monospace; font-size: 12px; white-space: pre;">
</span><span style="font-size: small; white-space: pre;"><span style="font-family: "courier new" , "courier" , monospace;">./gradlew bootRun</span><span style="font-family: , "consolas" , "liberation mono" , "menlo" , monospace;">
This command will run the application and we can test the same in any browser in the url
</span><a href="http://localhost:8080/hello/"><span style="font-family: inherit;">http://localhost:8080/hello/</span></a></span><span style="font-family: inherit; font-size: small; white-space: pre;">
It should print Hello World !!! in browser</span><span style="font-family: , "consolas" , "liberation mono" , "menlo" , monospace; font-size: 12px; white-space: pre;">
</span><br />
<h3 style="color: black; font-family: Times; white-space: normal;">
<span style="color: #212121; font-family: "google sans" , "noto sans" , "noto sans jp" , "noto sans kr" , "noto naskh arabic" , "noto sans thai" , "noto sans hebrew" , "noto sans bengali" , sans-serif; font-size: small;">Creating an OCI compute instance and deploy the app.</span></h3>
<div>
<span style="color: #212121; font-family: "google sans" , "noto sans" , "noto sans jp" , "noto sans kr" , "noto naskh arabic" , "noto sans thai" , "noto sans hebrew" , "noto sans bengali" , sans-serif; font-size: small;"><br /></span></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit; font-size: small;">We will try to deploy this app directly to a single OCI compute instance and we will run it. </span></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit; font-size: small;">For this example, we have used the standard </span><span style="font-family: inherit;">Oracle-Linux-7.8 image.</span></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit; font-size: small;">To create a compute instance in OCI console, we need to navigate compute and create instance. To access the instance we need ssh keys, to genarte the same please refer <a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm" target="_blank">https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm </a></span><br />
<br /></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit;"><span style="font-size: small;">Once the compute is ready. We need to install Java, as our Spring Boot App requires a java environment to run.</span></span></div>
<div style="color: black; white-space: normal;">
<span style="font-size: small;">1. ssh to compute instance</span></div>
<div style="color: black; white-space: normal;">
<span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); font-family: "courier new" , "courier" , monospace;"><span style="font-size: small;">ssh -i ~/.ssh/oci_compute opc@<public ip> of the compute instance></span></span></div>
<div style="color: black; white-space: normal;">
<span style="caret-color: rgba(0, 0, 0, 0.847); font-family: inherit; font-size: small;">2. Install java in the compute instance, for this example jre is enough.</span></div>
<div style="color: black; white-space: normal;">
<span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); text-size-adjust: auto;"><span style="font-family: "courier new" , "courier" , monospace; font-size: small;">sudo yum install jre-12.x86_64</span></span></div>
<div style="color: black; white-space: normal;">
<span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); text-size-adjust: auto;"><span style="font-family: inherit; font-size: small;">3. Configure the firewall to open port 8080, as our app will run in 8080</span></span></div>
<div style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0, 0, 0, 0.847); text-size-adjust: auto; white-space: normal;">
<span style="color: #242729; font-style: inherit; white-space: inherit;"><span style="font-family: "courier new" , "courier" , monospace; font-size: small;">sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp</span></span></div>
<div style="white-space: normal;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: small;"><span style="color: #242729; font-style: inherit; white-space: inherit;">sudo firewall-cmd --reload</span></span></div>
<div style="white-space: normal;">
<span style="font-family: inherit; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847); color: black;">Make a directory where we will copy our spring app</span></span></div>
<div style="white-space: normal;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847); color: black;">mkdir </span></span><span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); font-family: "courier new" , "courier" , monospace; font-size: small;">spring-app/</span></div>
<span style="font-family: , "consolas" , "liberation mono" , "menlo" , monospace; font-size: 12px; white-space: pre;"></span><br />
<div style="white-space: normal;">
<br /></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit;">4. Network configuration to allow Internet traffic to our compute instance.</span></div>
<div style="color: black; white-space: normal;">
a) We need an internet gateway configured in the VCN where the compute instance belongs</div>
<div style="color: black; white-space: normal;">
b) Route table entry to allow the traffic through the internet gateway<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPfUBWZd9PLL8yfqppb6VFTNFrKcNHEjLdsIAmgFMSdKqom20HQnpFNVaKjJD74B8DbMTg5_cAigoAgk50lPuzdAMbwLyhjHMZjWeaJ4c9J5-kouNKNQ_HUPKtEQqCHBHxgsmwzumYkkUs/s1600/Screenshot+2020-05-10+at+7.40.50+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center; white-space: pre;"><img border="0" data-original-height="290" data-original-width="1600" height="72" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPfUBWZd9PLL8yfqppb6VFTNFrKcNHEjLdsIAmgFMSdKqom20HQnpFNVaKjJD74B8DbMTg5_cAigoAgk50lPuzdAMbwLyhjHMZjWeaJ4c9J5-kouNKNQ_HUPKtEQqCHBHxgsmwzumYkkUs/s400/Screenshot+2020-05-10+at+7.40.50+PM.png" width="400" /></a></div>
<div style="color: black; white-space: normal;">
<br /></div>
<div style="color: black; white-space: normal;">
c) Security Ingress rule needs to be added to allow traffic. </div>
<div class="separator" style="clear: both; text-align: center; white-space: pre;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqTyK4hrYcqiyIudfEl7dlq0RvBEzpBNUPwtDg2IohkOwgE3Qa-a4lj3x3rF2Xn_KsWQLqNA-J1ZErmiQW8eMJzsIF7K1vGwa2cHMlKJ1WNfaYuGCKI1a3s1iwDGZ9RPnhhA6IaqEmeiOF/s1600/Screenshot+2020-05-10+at+7.48.41+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"> <img border="0" data-original-height="344" data-original-width="1600" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqTyK4hrYcqiyIudfEl7dlq0RvBEzpBNUPwtDg2IohkOwgE3Qa-a4lj3x3rF2Xn_KsWQLqNA-J1ZErmiQW8eMJzsIF7K1vGwa2cHMlKJ1WNfaYuGCKI1a3s1iwDGZ9RPnhhA6IaqEmeiOF/s400/Screenshot+2020-05-10+at+7.48.41+PM.png" width="400" /></a></div>
<div style="color: black; white-space: normal;">
<br /></div>
<div style="color: black; white-space: normal;">
<br /></div>
<div style="color: black; white-space: normal;">
<br /></div>
<div style="color: black; white-space: normal;">
<span style="font-family: inherit;"><br /></span></div>
<div style="white-space: pre;">
<span style="color: #212121; font-family: inherit; font-size: small;"><br /></span></div>
<div style="white-space: pre;">
<span style="color: #212121; font-family: inherit; font-size: small;"><br /></span></div>
<div style="white-space: pre;">
<span style="color: #212121; font-size: small;"> 5. Copy our Spring Boot app from laptop to compute instance and run.</span></div>
<span style="color: #212121; font-size: small;"><span style="font-family: "courier new" , "courier" , monospace; white-space: pre;">./gradlew clean build
</span><span style="white-space: pre;">This will build and create the <span style="font-family: "courier new" , "courier" , monospace;">spring-web-rest-demo-1.0.jar </span><span style="font-family: inherit;">in</span><span style="font-family: "courier new" , "courier" , monospace;"> ./build/libs</span></span></span><br />
<div style="white-space: pre;">
<span style="color: #212121; font-size: small;">To copy the same to our compute instance</span></div>
<div style="white-space: pre;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: small;"><span style="color: rgba(0 , 0 , 0 , 0.847); white-space: normal;">scp -i ~/.ssh/oci_compute build/libs/spring-web-rest-demo-1.0.jar opc@</span><public ip><span style="color: rgba(0 , 0 , 0 , 0.847); white-space: normal;">:spring-app/</span></span></div>
<div style="white-space: pre;">
<span style="font-family: inherit; font-size: small;"><span style="color: rgba(0 , 0 , 0 , 0.847); white-space: normal;">Now ssh to the compute and run the app</span></span></div>
<div style="white-space: pre;">
<span style="color: rgba(0 , 0 , 0 , 0.847); font-family: "courier new" , "courier" , monospace; font-size: small; white-space: normal;">ssh -i ~/.ssh/oci_compute opc@<public ip> of the compute instance></span></div>
<div>
<span style="color: black; font-family: "courier new" , "courier" , monospace; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847);">java -jar spring-app/spring-web-rest-demo-1.0.jar</span></span></div>
<div>
<span style="color: black; font-family: "courier new" , "courier" , monospace; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847);"><br /></span></span></div>
<div>
<span style="color: black; font-family: inherit; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847);">Our spring app should run in port 8080. If all the configs are correct </span></span></div>
<div>
<span style="color: black; font-family: inherit; font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847);">we should able to see Hello World !!! in the browser, by trying compute's </span></span></div>
<div>
<span style="color: black; font-family: inherit;"><span style="font-size: small;"><span style="caret-color: rgba(0, 0, 0, 0.847);">http://</span></span><public ip>:8080/hello</span></div>
<div>
<span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); font-size: small;"><span style="font-family: inherit;">If you don't see please revisit step 3 and 4 </span></span><span style="caret-color: rgba(0, 0, 0, 0.847); color: rgba(0 , 0 , 0 , 0.847); font-family: "courier new" , "courier" , monospace; font-size: small;"> </span></div>
<span style="font-family: , "consolas" , "liberation mono" , "menlo" , monospace; font-size: 12px; white-space: pre;">
</span></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="2" id="L2" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC2" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="3" id="L3" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC3" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
</tbody></table>
</div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-26620638007308825772020-02-12T04:36:00.000-08:002020-02-12T04:36:29.200-08:00Connecting Google cloud SQL from App engine.<div dir="ltr" style="text-align: left;" trbidi="on">
Google cloud platform support PHP application in its app engine. App Engine is an elastic container it automatically scales up and down based on the traffic.<div>
<br /></div>
<div>
Here is the <a href="https://cloud.google.com/appengine/docs/standard/php7/quickstart">https://cloud.google.com/appengine/docs/standard/php7/quickstart</a> explaining how to deploy PHP application into GCP app.<br /><div>
<br /></div>
</div>
<div>
The scope of this article is how to connect to the <a href="https://cloud.google.com/sql/?utm_source=google&utm_medium=cpc&utm_campaign=japac-IN-all-en-dr-bkwsrmkt-all-super-trial-e-dr-1008074&utm_content=text-ad-none-none-DEV_c-CRE_300622148620-ADGP_Hybrid+%7C+AW+SEM+%7C+BKWS+~+T1+%7C+EXA+%7C+Storage+%7C+1:1+%7C+IN+%7C+en+%7C+cloud+storage+%7C+google+cloud+sql-KWID_43700037527390338-kwd-28489936691&userloc_9061996&utm_term=KW_google%20cloud%20sql&gclid=CjwKCAiA4Y7yBRB8EiwADV1haSYE_8bAF5ysNIcfYP9ycr-3xgw6TCr81gg_SxfdOzQRu6d16TJr8RoCSAYQAvD_BwE" target="_blank">cloud sql</a> in your PHP from app engine. To deploy into app engine we need to configure some meta information in app.yaml</div>
<div>
If we are connecting by dsn we don't need any special configuration. we can refer to the database directly like</div>
<div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
mysql:unix_socket=/cloudsql/<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>;dbname=<my_db></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
I am assuming both your app engine and cloud database in the same project. Else we have to explicitly give access.</div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;">$dbdsn = "mysql:unix_socket=/cloudsql/<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>;dbname=<my_db>";</span></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$conn = new PDO($dbdsn, $username, $password);</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: black; font-family: Times; font-size: small;">We can add the dsn in app.yaml read as an environment variable. </span></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
In app.yaml</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
env_variables:</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span class="Apple-converted-space"> </span>DB_DSN: mysql:unix_socket=/cloudsql/<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>;dbname=<my_db></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
DB_USER: my-db-user</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
DB_PASS: my-db-pass</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
DB_NAME: my_db<span class="Apple-converted-space"> </span></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
DB_SERVER: my_server</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
And in the code</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$dbdsn = getenv('DB_DSN');</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$username = getenv('DB_USER');</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$password = getenv('DB_PASS');</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$conn = new PDO($dbdsn, $username, $password);</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
If we want to do tcp connection by providing host and port</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
$conn = new mysqli($servername, $username, $password, $dbname);</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
Then app.yaml we have to add </div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
beta_settings:</div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span class="Apple-converted-space"> </span>cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<PORT></div>
<div class="p1" style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<br /></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;">And in the code $servername= </span><span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;">172.17.0.1:<PORT>;</span></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;"><br /></span></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;">For more details # </span><a href="https://cloud.google.com/sql/docs/mysql/connect-app-engine">https://cloud.google.com/sql/docs/mysql/connect-app-engine</a></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;">My example code # </span><a href="https://github.com/pallabrath/myexpjava/tree/master/vlog/gcp/php-mysql-appEngine-clouddb%20/src">https://github.com/pallabrath/myexpjava/tree/master/vlog/gcp/php-mysql-appEngine-clouddb%20/src</a></div>
<div class="p1" style="font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
<span style="color: rgba(0, 0, 0, 0.85); font-family: "Helvetica Neue"; font-size: 13px;"><br /></span></div>
</div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-60010905685716823232019-08-06T06:59:00.000-07:002019-08-06T07:06:30.660-07:00Rest apis powered by GraphQL <div dir="ltr" style="text-align: left;" trbidi="on">
We can understand "Rest Api" means any system expose its functionality over http. So the consumer of api can make a http request to avail the functionalities. JSON is the data format in which client and server communicate over http.<br />
<div>
<br /></div>
<div>
GraphQL is a query language for api which enable the client of rest apis to ask what they need exactly. The same time server side its super easy to expose more functionaries in a generic way and cater lots of specialized request. </div>
<div>
<br /></div>
<div>
more details # <a href="https://graphql.org/">https://graphql.org/</a></div>
<div>
<br /></div>
<div>
GarphQL in Java # <a href="https://www.graphql-java.com/">https://www.graphql-java.com/</a></div>
<div>
<br /></div>
<div>
In this article we will see an example where GraphQL is used with JAX-RS (Jersey) based rest implementation. And we will use Hibernate as Data Fetcher for GraphQL.</div>
<div>
<br /></div>
<div>
Its a standard gradle project, created with eclipse. Here is the link to my github.<br />
<br />
<a href="https://github.com/pallabrath/myexpjava/tree/master/GqlDemo">https://github.com/pallabrath/myexpjava/tree/master/GqlDemo</a></div>
<div>
<br />
1. Lets start with build.gradle<br />
<br />
We have the dependencies for j<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.4px;">ersey</span>, graphql, hibernate, ojdbc driver and some helpers like gson and gauva.<br />
<br />
2. In web.xml we have initialised the j<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.4px;">ersey</span> servlet, there we have specified init parameters to look for com.rest package.<br />
<br />
3. Inside source we have com.rest.Employee.java which expose the rest end point GqlDemo/rest/employee<br />
<br />
We have the Query() method to handle the incoming http post request for graph ql queries.<br />
<br />
4. We have created GraphQl provider which load the schema definitions from resources/schema.graphqls<br />
we have defined the wiring for query employeeById.<br />
<br />
5. In GraphQLDataFetcher we have the implementation for employeeById. where we get the request parameter value and process. Here I have used hibernate to query the database.<br />
<br />
Here is a screen shot of postman request/response.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgisf-Owjq74nYd5boPktsXVLDUkNyMte1SKeQSObNBYkUsjCaZdKVqvg0KfE4Bi_kA9qKTNj1oWCGXgmJB6hyphenhyphenNRjoY9vV7XrHO62QdeFfeioTUxgRhL2QRFRfr6yYjzQHsqe2hzQJRKChR/s1600/Screenshot+2019-08-06+at+7.22.03+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="912" data-original-width="1600" height="363" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgisf-Owjq74nYd5boPktsXVLDUkNyMte1SKeQSObNBYkUsjCaZdKVqvg0KfE4Bi_kA9qKTNj1oWCGXgmJB6hyphenhyphenNRjoY9vV7XrHO62QdeFfeioTUxgRhL2QRFRfr6yYjzQHsqe2hzQJRKChR/s640/Screenshot+2019-08-06+at+7.22.03+PM.png" width="640" /></a></div>
I will try to answer if any question on this. Please let me know your feedback.<br />
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #2a00ff}
</style>
<br />
<br />
<br /></div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-24934154359263200382019-02-09T04:43:00.002-08:002019-08-12T23:51:54.064-07:00Read ZipEntry form ZipInputStream as InputStream<div dir="ltr" style="text-align: left;" trbidi="on">
To read ZipEntry from ZipInputStream<br />
<br />
<table class="highlight tab-size js-file-line-container" data-tab-size="8" style="background-color: white; border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; tab-size: 8;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><td class="blob-code blob-code-inner js-file-line" id="LC9" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">public</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="pl-en" style="box-sizing: border-box; color: #6f42c1;">ZipEntityDemo</span> {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="10" id="L10" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC10" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">public</span> <span class="pl-smi" style="box-sizing: border-box;">InputStream</span> <span class="pl-en" style="box-sizing: border-box; color: #6f42c1;">extract</span>(<span class="pl-smi" style="box-sizing: border-box;">ZipInputStream</span> <span class="pl-v" style="box-sizing: border-box; color: #e36209;">zis</span>, <span class="pl-smi" style="box-sizing: border-box;">String</span> <span class="pl-v" style="box-sizing: border-box; color: #e36209;">entityName</span>) <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">throws</span> <span class="pl-smi" style="box-sizing: border-box;">Exception</span> {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="11" id="L11" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC11" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">ByteArrayOutputStream</span> out <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">ByteArrayOutputStream</span>();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="12" id="L12" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC12" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span> ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="13" id="L13" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC13" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">do</span> {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="14" id="L14" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC14" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zis<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getNextEntry();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="15" id="L15" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC15" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">if</span> (ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">!=</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">&&</span> ze<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getName()<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>equalsIgnoreCase(entityName)) {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="16" id="L16" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC16" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="17" id="L17" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC17" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">byte</span>[] buffer <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">byte</span>[<span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">1</span>];</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="18" id="L18" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC18" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span> (zis<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>available() <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">==</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">1</span>) {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="19" id="L19" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC19" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">zis<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>read(buffer);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="20" id="L20" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC20" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>write(buffer, <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">0</span>, <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">1</span>);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="21" id="L21" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC21" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="22" id="L22" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC22" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>close();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="23" id="L23" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC23" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">ByteArrayInputStream</span>(out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>toByteArray());</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="24" id="L24" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC24" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="25" id="L25" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC25" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">} <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span> (ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">!=</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span>);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="26" id="L26" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC26" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="27" id="L27" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC27" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">return</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="28" id="L28" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC28" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="29" id="L29" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC29" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
</tbody></table>
Here zis.getNextEntry() will return us the beginning of an entry. If the name of the entry match to our desired name we can start reading. Here we are reading byte by byte in a loop till last byte. We could have improved by using ze.<strong style="background-color: white; color: #353833; font-family: monospace; font-size: 11.856px;"><a href="https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipEntry.html#getSize()" style="color: #4c6b87; text-decoration-line: none;">getSize</a></strong><span style="background-color: white; color: #353833; font-family: monospace; font-size: 11.856px;">(), </span>and we could read multiple bytes till the size. But the getSize() methods some time returns -1 for some zipfiles. So it wouldn't be reliable. With out knowing the entry size only way is to read byte by byte till last.</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-73793047375508505152019-01-13T06:22:00.000-08:002019-01-13T06:22:48.776-08:00How to Unzip in Java<div dir="ltr" style="text-align: left;" trbidi="on">
Here is a quick java program to unzip or explode a zip file. We will read the zip file with the help of java.util.ZipFile, then we will process each entries inside this. We will read the entries as stream and write to a buffer and eventually to a file output stream. So at the end we will extract all the elements in the zip, and maintain the same hierarchical structure of directories .Please refer the below example.<br />
<table class="highlight tab-size js-file-line-container" data-tab-size="8" style="background-color: white; border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; tab-size: 8;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><td class="blob-code blob-code-inner js-file-line" id="LC1" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><br /></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="2" id="L2" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC2" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="3" id="L3" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC3" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.io.File</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="4" id="L4" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC4" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.io.FileOutputStream</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="5" id="L5" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC5" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.io.IOException</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="6" id="L6" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC6" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.io.InputStream</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="7" id="L7" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC7" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.util.Enumeration</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="8" id="L8" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC8" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.util.zip.ZipEntry</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="9" id="L9" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC9" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.util.zip.ZipFile</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="10" id="L10" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC10" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="11" id="L11" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC11" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">public</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="pl-en" style="box-sizing: border-box; color: #6f42c1;">UnZipDemo</span> {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="12" id="L12" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC12" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">public</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">static</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">void</span> <span class="pl-en" style="box-sizing: border-box; color: #6f42c1;">main</span>(<span class="pl-smi" style="box-sizing: border-box;">String</span> <span class="pl-v" style="box-sizing: border-box; color: #e36209;">args</span>[]) <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">throws</span> <span class="pl-smi" style="box-sizing: border-box;">IOException</span></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="13" id="L13" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC13" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">{</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="14" id="L14" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC14" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">String</span> zipFileName <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>D:<span class="pl-cce" style="box-sizing: border-box;">\\</span>demo.zip<span class="pl-pds" style="box-sizing: border-box;">"</span></span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="15" id="L15" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC15" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">ZipFile</span> zipFile <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">ZipFile</span>(zipFileName);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="16" id="L16" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC16" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">String</span> extractedPath <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>D:<span class="pl-cce" style="box-sizing: border-box;">\\</span>demo<span class="pl-pds" style="box-sizing: border-box;">"</span></span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="17" id="L17" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC17" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">byte</span>[] buffer <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">byte</span>[<span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">1024</span>];</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="18" id="L18" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC18" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">Enumeration</span> enm <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zipFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>entries();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="19" id="L19" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC19" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span> ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="20" id="L20" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC20" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">InputStream</span> fis <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">null</span>;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="21" id="L21" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC21" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span> (enm<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>hasMoreElements()) {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="22" id="L22" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC22" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> (<span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span>) enm<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>nextElement();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="23" id="L23" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC23" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">System</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>println(<span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>processing = <span class="pl-pds" style="box-sizing: border-box;">"</span></span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">+</span>ze<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getName());</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="24" id="L24" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC24" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">File</span> newFile <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">File</span>(extractedPath <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">+</span> <span class="pl-smi" style="box-sizing: border-box;">File</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>separator <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">+</span> ze<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getName());</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="25" id="L25" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC25" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-c" style="box-sizing: border-box; color: #6a737d;"><span class="pl-c" style="box-sizing: border-box;">//</span> create all non exists folders</span></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="26" id="L26" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC26" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">File</span>(newFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getParent())<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>mkdirs();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="27" id="L27" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC27" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-smi" style="box-sizing: border-box;">FileOutputStream</span> fos <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">FileOutputStream</span>(newFile);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="28" id="L28" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC28" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">int</span> len;</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="29" id="L29" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC29" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">fis <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zipFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getInputStream(ze);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="30" id="L30" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC30" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span> ((len <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> fis<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>read(buffer)) <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">></span> <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">0</span>) {</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="31" id="L31" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC31" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">fos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>write(buffer, <span class="pl-c1" style="box-sizing: border-box; color: #005cc5;">0</span>, len);</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="32" id="L32" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC32" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="33" id="L33" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC33" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">fos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>close();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="34" id="L34" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC34" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">fis<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>close();</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="35" id="L35" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC35" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="36" id="L36" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC36" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="37" id="L37" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC37" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;"></td></tr>
<tr style="box-sizing: border-box;"><td class="blob-num js-line-number" data-line-number="38" id="L38" style="box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 0px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;"></td><td class="blob-code blob-code-inner js-file-line" id="LC38" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 0px 10px; position: relative; vertical-align: top; white-space: pre;">}</td></tr>
</tbody></table>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com1tag:blogger.com,1999:blog-8776378117389609026.post-81490839772806917172019-01-03T08:41:00.001-08:002019-01-04T01:07:05.601-08:00Zip files in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
To create or read zip files in Java, we can use <a href="http://java.util.zipfile/">java.util.ZipFile</a>. This allow to read an zip file and read the entries inside it. We can create a zip file by using <a href="https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipOutputStream.html" target="_blank">ZipOutputStream</a>.</div>
<div>
Please refer the below example program<br />
<br />
<a href="https://github.com/pallabrath/myexpjava/blob/master/myexpjava/src/demo/zipfile/ZipFileDemo.java">https://github.com/pallabrath/myexpjava/blob/master/myexpjava/src/demo/zipfile/ZipFileDemo.java</a></div>
<div>
<br /></div>
<div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC1" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">package</span> <span class="pl-smi" style="box-sizing: border-box;">demo.zipfile</span>;</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L2" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.io.FileOutputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC4" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">import</span> <span class="pl-smi" style="box-sizing: border-box;">java.io.IOException</span>;</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L5" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.io.InputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L6" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.util.Enumeration</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L7" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.util.Scanner</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L8" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.util.zip.ZipEntry</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L9" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.util.zip.ZipFile</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L10" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">import</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">java.util.zip.ZipOutputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">;</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC13" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">public</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">class</span> <span class="pl-en" style="box-sizing: border-box; color: #6f42c1;">ZipFileDemo</span> {</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L16" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">public</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">static</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">void</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-en" style="background-color: white; box-sizing: border-box; color: #6f42c1; text-align: left; white-space: pre;">main</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">(</span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">String</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-v" style="background-color: white; box-sizing: border-box; color: #e36209; text-align: left; white-space: pre;">args</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">[]) </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">throws</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">IOException</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> { </span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L17" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-c" style="background-color: white; box-sizing: border-box; color: #6a737d; text-align: left; white-space: pre;"><span class="pl-c" style="box-sizing: border-box;"> //</span> create a zip file</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC18" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">String</span> zipFileName <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>D:<span class="pl-cce" style="box-sizing: border-box;">\\</span>demo.zip<span class="pl-pds" style="box-sizing: border-box;">"</span></span>;</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; box-sizing: border-box; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L19" style="box-sizing: border-box; cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; width: 50px;">
<span class="pl-smi" style="box-sizing: border-box; text-align: left;"></span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;"> ZipOutputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> zipos </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">=</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">new</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">ZipOutputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">(</span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">new</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">FileOutputStream</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">(zipFileName));</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L20" style="background-color: rgb(250, 251, 252) !important; box-sizing: border-box; color: rgba(27, 31, 35, 0.3); cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; white-space: nowrap; width: 50px;">
<span class="pl-c" style="background-color: white; box-sizing: border-box; color: #6a737d; text-align: left; white-space: pre;"> //</span><span style="background-color: white; color: #6a737d; text-align: left; white-space: pre;"> In Zip Output Stream we can create file.</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; box-sizing: border-box; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-num blame-blob-num bg-gray-light js-line-number" id="L21" style="box-sizing: border-box; cursor: pointer; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; min-width: 50px; padding: 3px 10px; text-align: right; user-select: none; vertical-align: top; width: 50px;">
<span class="pl-smi" style="box-sizing: border-box; text-align: left;"></span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;"> ZipEntry</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> ze1 </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">=</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-k" style="background-color: white; box-sizing: border-box; color: #d73a49; text-align: left; white-space: pre;">new</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;"> </span><span class="pl-smi" style="background-color: white; box-sizing: border-box; color: #24292e; text-align: left; white-space: pre;">ZipEntry</span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">(</span><span class="pl-s" style="background-color: white; box-sizing: border-box; color: #032f62; text-align: left; white-space: pre;"><span class="pl-pds" style="box-sizing: border-box;">"</span>file1.txt<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span style="background-color: white; color: #24292e; text-align: left; white-space: pre;">);</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC22" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>putNextEntry(ze1);</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC23" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>write(<span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>Hello World<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getBytes());</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC24" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>closeEntry();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC25" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-c" style="box-sizing: border-box; color: #6a737d;"><span class="pl-c" style="box-sizing: border-box;">//</span> Now lets create another file in a directory</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC26" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>putNextEntry(<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span>(<span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>etc<span class="pl-cce" style="box-sizing: border-box;">\\</span>file2.txt<span class="pl-pds" style="box-sizing: border-box;">"</span></span>));</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC27" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>write(<span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>Hello Again<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getBytes());</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC28" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>closeEntry();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC29" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
zipos<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>close();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC31" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-c" style="box-sizing: border-box; color: #6a737d;"><span class="pl-c" style="box-sizing: border-box;">//</span> To Read Zip file</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC32" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">ZipFile</span> zipFile <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">ZipFile</span>(zipFileName);</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC33" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-c" style="box-sizing: border-box; color: #6a737d;"><span class="pl-c" style="box-sizing: border-box;">//</span> for specific entry</span></div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC34" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span> ze2 <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zipFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getEntry(<span class="pl-s" style="box-sizing: border-box; color: #032f62;"><span class="pl-pds" style="box-sizing: border-box;">"</span>file1.txt<span class="pl-pds" style="box-sizing: border-box;">"</span></span>);</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC35" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">InputStream</span> is <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zipFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getInputStream(ze2);</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC36" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">Scanner</span> scan <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">new</span> <span class="pl-smi" style="box-sizing: border-box;">Scanner</span>(is);</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC37" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span> (scan<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>hasNextLine()){</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC39" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">System</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>println(scan<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>nextLine());</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC40" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
}</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC41" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
scan<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>close();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC42" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">Enumeration</span> enm <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> zipFile<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>entries();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC43" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">while</span>(enm<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>hasMoreElements()) {</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC45" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span> ze <span class="pl-k" style="box-sizing: border-box; color: #d73a49;">=</span> (<span class="pl-smi" style="box-sizing: border-box;">ZipEntry</span>) enm<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>nextElement();</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC46" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
<span class="pl-smi" style="box-sizing: border-box;">System</span><span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>out<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>println(ze<span class="pl-k" style="box-sizing: border-box; color: #d73a49;">.</span>getName());</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC47" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
} </div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC48" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
}</div>
</div>
<div class="d-flex flex-justify-start flex-items-start" style="align-items: flex-start !important; background-color: white; box-sizing: border-box; color: #24292e; display: flex !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; justify-content: flex-start !important;">
<div class="blob-code blob-code-inner js-file-line" id="LC50" style="box-sizing: border-box; flex-grow: 1; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; line-height: 20px; overflow-wrap: normal; overflow: visible; padding: 3px 10px; position: relative; vertical-align: top; white-space: pre;">
}</div>
</div>
</div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-1035107118949195652018-12-30T04:28:00.002-08:002018-12-30T04:28:59.745-08:00Java 8 at a glance <div dir="ltr" style="text-align: left;" trbidi="on">
In Java 8 following are the major enhancement.<br />
<br />
- <a href="http://www.myexperimentswithjava.com/2014/05/do-it-my-way.html" target="_blank">Lambda Expression</a><br />
- <a href="https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx" target="_blank">Method Reference for Lambda Expression</a><br />
- <a href="http://www.myexperimentswithjava.com/2018/12/java-streams.html" target="_blank">Java Streams</a><br />
- Improved Interface<br />
<br />
a) Now we can have concrete methods in an interface. It can be either static or default. Default methods can be overridden in implemented class. If a class implement two or more interface and there is a collision of default method then it is mandatory to override.<br />
b) Functional interface. For lambda expression we can define an interface as function it will have only one abstract method.<br />
Here is an good article explaining in details #<a href="https://www.blogger.com/%C2%A0https://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method" target="_blank"> https://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method</a><br />
<br />
- Enhancement to Annotation<br />
More details # <a href="https://www.javatpoint.com/java-8-type-annotations-and-repeating-annotations">https://www.javatpoint.com/java-8-type-annotations-and-repeating-annotations</a><br />
<br />
- Parallel Array Sorting<br />
<a href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#parallelSort-int:A-" target="_blank"> java.util.Arrays</a> support sorting in parallel which will be faster.<br />
- Hash Map performance improvement in case of Key collision<br />
- Removed JDBC-ODBC bridge<br />
<br />
<br />
More details on Java 8 features<br />
<a href="http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html">http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html</a><br />
<br /></div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-58144027109901228152018-12-28T06:28:00.000-08:002018-12-28T08:52:20.819-08:00Java Streams<div dir="ltr" style="text-align: left;" trbidi="on">
Streams are very popular feature in Java 8. Its another step in the direction of supporting functional programming in Java.<br />
<div>
Streams are a way of accessing data from various data source. Data source can be an array, any java collection objects, I/O channel (like files, socket etc) or a generator function. One important thing is Streams are not data structure it doesn't store data, it just convey the data from data source.</div>
<div>
<br /></div>
<div>
Streams facilitate various computational operations like fetch, filter, sort, aggregate, search etc. in a efficient way. But all these operations produces a result while not changing its source.</div>
<div>
Stream operations are of two types, intermediate or terminal .Filter and map are examples of intermediate operation For each and reduce are terminal operation. Intermediate operation doesn't get executed instantly ,it creates a new stream. Only after terminal operation it get evaluated.</div>
<div>
<br /></div>
<div>
Intermediate operations are further divided into stateless or stateful. In case of stateless like filter or map no state of previous element is required. But in case of sateful eg: sort or distinct to process new element previous element details are necessary.<br />
<br />
By default streams are executed sequential, Streams support parallelism also. So that the elements can be processed in parallel manner. </div>
<div>
<br /></div>
<div>
Lets see some code.<br />
<br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">package demo.streams;</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">import java.util.Arrays;</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">import java.util.List;</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">import java.util.stream.Stream;</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">public class StreamDemo {</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>public static void main (String args[])</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>String[] arr = {"python", "perl", "java", "java script", "c", "c++", "html", "sql"};</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>List<String> strList = Arrays.asList(arr);</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// steam creation</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>Stream<String> stream = strList.stream();</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// sort the elements</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>stream.sorted().forEach(System.out::println);</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// find first element starts with j</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>Stream<String> filteredStream = strList.stream().filter(string-> string.startsWith("j"));</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>System.out.println(filteredStream.findFirst().get());</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// in first 4 element how many elements start with p</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>System.out.println(strList.stream().limit(4).filter(str->str.startsWith("p")).count());<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// map</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// prepend hello begin of each string</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>strList.stream().map(str->"Hello "+str).forEach(System.out::println);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// close</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>filteredStream.close();</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>stream.close();</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>// parallel Stream, count the number of letters</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>strList.parallelStream().map(str->str + "="+ str.length()).forEach(System.out::println);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<span style="color: #999999; font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
More details<a href="https://www.blogger.com/More%20details%20https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" target="_blank"> https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></div>
<div>
<br /></div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-19577600418374718702018-12-22T06:56:00.000-08:002018-12-22T06:56:35.709-08:00tnsnames.ora parsing in java<div dir="ltr" style="text-align: left;" trbidi="on">
tnsnames.ora is a configuration file which store the information about network service names and connect descriptor. These information are vital to connect the database. More details about tnsora can be found here. <a href="https://docs.oracle.com/database/121/NETRF/tnsnames.htm#NETRF259">https://docs.oracle.com/database/121/NETRF/tnsnames.htm#NETRF259</a><br />
<div>
<br /></div>
<div>
The configurations are defined key value manner. value can be literal or one or multiple key values again. Syntax rule can be found here <a href="https://docs.oracle.com/cd/A57673_01/DOC/net/doc/NWUS233/apb.htm" target="_blank">https://docs.oracle.com/cd/A57673_01/DOC/net/doc/NWUS233/apb.htm </a> </div>
<div>
To parse the same in Java please refer below code</div>
<div>
<br />
<br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.io.FileInputStream;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.io.InputStream;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.ArrayList;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.List;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.Scanner;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.Stack;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;">public class TnsOraParser {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private static final String COMMENT = "#";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private Stack<Parameter> stack = new Stack<Parameter>();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private List<Parameter> netServiceNames = new ArrayList<Parameter>();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private boolean bracketOpen;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public TnsNamesOra parse(InputStream is) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>Scanner scan = new Scanner(is);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>StringBuilder buffer = new StringBuilder();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>boolean quote = false;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>while (scan.hasNextLine()) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>String aLine = scan.nextLine();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>aLine = aLine.trim();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (aLine.startsWith(COMMENT)) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>continue;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>for (int i = 0; i < aLine.length(); i++) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>char ch = aLine.charAt(i);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (ch == '"')</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>quote = !quote;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else if (quote)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>buffer.append(ch);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>continue;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else if (ch == ' ' || ch == '\n' || ch == '\r') {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>continue;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (ch == '=')</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>gotNewParam(buffer);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else if (ch == '(')</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>bracketOpen = true;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else if (ch == ')')</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>bracketOpen = false;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>Parameter cp = stack.pop();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (cp != null)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (buffer.length() > 0)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>cp.setValue(buffer.toString());</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>buffer.delete(0, buffer.length());</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (!stack.isEmpty())</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>stack.peek().addParm(cp);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>buffer.append(ch);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>scan.close();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return new TnsNamesOra(netServiceNames);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private void gotNewParam(StringBuilder buffer) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>Parameter param = new Parameter(buffer.toString());</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (!bracketOpen && stack.size() == netServiceNames.size())</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>netServiceNames.add(param);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>buffer.delete(0, buffer.length());</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>stack.push(param);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public static void main(String args[]) throws Exception {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>FileInputStream testFile = new FileInputStream("D:\\Work\\tnsnames.ora");</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>TnsOraParser tnsOraParser = new TnsOraParser();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>TnsNamesOra tnsOra = tnsOraParser.parse(testFile);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>System.out.println(tnsOra.getServiceNames());<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: "Courier New", Courier, monospace;">Parameter .java</span><br />
<span style="color: #999999; font-family: "Courier New", Courier, monospace;">==============</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.ArrayList;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.List;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;">public class Parameter {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private String name;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private String value;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private List<Parameter> parameters;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public Parameter(String name) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>this.name = name;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public String getName() {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return name;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public void setName(String name) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>this.name = name;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public String getValue() {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return value;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public void setValue(String value) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>this.value = value;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public List<Parameter> getParameters() {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return parameters;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public void setParameters(List<Parameter> parameters) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>this.parameters = parameters;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public void addParm(Parameter cp) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (parameters == null) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>parameters = new ArrayList<Parameter>();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>parameters.add(cp);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="white-space: pre;"><span style="color: #999999; font-family: Courier New, Courier, monospace;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public String toString()</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>StringBuilder sb = new StringBuilder();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>sb.append(name + "=(" );</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (value != null)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>sb.append(value);<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>else if (parameters != null)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>for (Parameter param : parameters)</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>{</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>sb.append(param);</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>sb.append(")");</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return sb.toString();<span style="white-space: pre;"> </span></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"></span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: "Courier New", Courier, monospace;">TnsNamesOra.java</span><br />
<span style="color: #999999; font-family: "Courier New", Courier, monospace;">==================</span><br />
<span style="color: #999999; font-family: "Courier New", Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: "Courier New", Courier, monospace;">import java.util.ArrayList;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.HashSet;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.List;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">import java.util.Set;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;">public class TnsNamesOra {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private List<Parameter> netServiceName;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private final String DESCRIPTION_LIST = "DESCRIPTION_LIST";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private final String DESCRIPTION = "DESCRIPTION";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private final String ADDRESS_LIST = "ADDRESS_LIST";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private final String ADDRESS = "ADDRESS";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>private final String PROTOCOL = "PROTOCOL";</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public TnsNamesOra(List<Parameter> netServiceNames) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>this.netServiceName = netServiceNames;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>public List<String> getServiceNames() {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>List<String> serviceNames = null;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>if (netServiceName != null) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>serviceNames = new ArrayList<String>();</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>for (Parameter param : netServiceName) {</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>serviceNames.add(param.getName());</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>return serviceNames;</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><span style="white-space: pre;"> </span>}</span><br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;"><br /></span>
<br />
<span style="color: #999999; font-family: Courier New, Courier, monospace;">}</span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-78343681792458975672018-12-16T03:51:00.000-08:002018-12-16T03:51:55.147-08:00Java Interview Preparation article : OOPS concept<div dir="ltr" style="text-align: left;" trbidi="on">
This is continuation of <a href="http://www.myexperimentswithjava.com/2018/12/java-interview-preparation-article-1.html" target="_blank">Java Interview Preparation </a>. Here we will see OOPS concept. Encapsulation, Abstraction, Inheritance and Polymorphism are the basic pillars of object oriented programming language. And how Java adhere to these principle is crucial. These topics are covered in almost all the java book or can be found over internet also. Here is one <a href="https://www.journaldev.com/12496/oops-concepts-java-example" target="_blank">link</a>.<br />
<br />
While answering abstraction and encapsulation it may sound overlapping. My explanation for encapsulation is, inside class all the members would be private to prohibit direct access of them from out side of the class. The public methods are for carry out various operation. And the caller of those methods need not know the internal implementation, only the meaning of required arguments to invoke the method is sufficient to achieve the goal. This is called abstraction.<br />
<br />
In Java inheritance is achieved by "extends" and "implements" key word. It means either by extending another class or implementing a interface.<br />
Child class is always a super set of parent class. Its also known as "is a" relation ship. There cloud be many questions under this topic. How the objects get created, the order of invocation of constructors. How a parent class reference can hold a child class object etc. Why java doesn't allow multiple inheritance. What are marker interface ? Several questions fall under this.<br />
<br />
<a href="https://www.w3schools.com/java/java_polymorphism.asp" target="_blank">This is an nice article to understand polymorphism.</a> There are two aspects of this compile time polymorphism and run time polymorphism.<br />
<br />
https://www.geeksforgeeks.org/dynamic-method-dispatch-runtime-polymorphism-java/<br />
https://www.dineshonjava.com/compile-time-polymorphism-in-java/<br />
<br />
There could be other question like difference between aggregation vs composition.<br />
"is a" vs "has a"<br />
How can you achieve multiple inheritance in java ? ( By interface)<br />
<br />
But I feel the major three concepts will put you in good spot to attempt the question around it.<br />
<br /></div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-20165265646957072582018-12-09T21:36:00.000-08:002018-12-16T03:54:19.615-08:00Java Interview Preparation article :1 <div dir="ltr" style="text-align: left;" trbidi="on">
Time and again I get requests to write on preparation for interviews in Java. So I am planning to write series of post targeting interview preparation.<br />
To start if the interview is for service based company the key is breadth. And for product based company its the depth. The exception for (breadth/depth) is directly proportional to years of experience.<br />
<div>
Another point I want to make entry level roles (0 years to 5 years) you need to do well in java interview rounds to get selected. For more experience this round is kind of elimination round.<br />
<br />
Topics in core Java<br />
<a href="http://www.myexperimentswithjava.com/2018/12/java-interview-preparation-article-oops.html" target="_blank"><br /></a>
<ol style="text-align: left;">
<li><a href="http://www.myexperimentswithjava.com/2018/12/java-interview-preparation-article-oops.html" target="_blank">OOPs concept</a></li>
<li><a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html" target="_blank">Java key word, more priority to the latest released key words.</a></li>
<li>Inheritance, interface, marker interface, abstract class and is a has a relationship </li>
<li><a href="https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" target="_blank">Enums</a></li>
<li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html" target="_blank">The details about java.lang.Object </a></li>
<li><a href="https://www.myexperimentswithjava.com/2014/10/string-stringbuffer-and-stringbuilder.html" target="_blank">Strings </a></li>
<li>Exception handling</li>
<li>Collections </li>
<li>Threads and Concurrent Package</li>
<li>Design Pattern</li>
</ol>
All these topics will cover good amount of ground. There might be some here and there question about garbage collection, jvm architecture, java memory management etc. Those questions were more relevant when java was dominant. Now we see the increase popularity of python, Scala so its kind of ok to focus energy in understanding other topics.<br />
<br />
Once you get thorough these topic next stage is Java in web development, which we will see in another post. </div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-8701414032344765532018-08-07T22:38:00.001-07:002018-08-08T10:47:26.384-07:00BlockingQueue<div dir="ltr" style="text-align: left;" trbidi="on">
Producer - consumer problem arises in multi thread environment. This can be defined when there are one or more threads generate data independently and same time one or many threads read those data to process independently.<br />
<div>
<br /></div>
<div>
To solve this we can have an array and there would be synchronized method to access. So when one thread only can either read/write at same time. If the array is full, producer threads need to wait. And if the array is empty consumer threads need to wait.</div>
<div>
<br /></div>
<div>
<a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html">java.util.concurrent</a> package comes with <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html" target="_blank">Blocking queue</a> which is very handy to solve this problem.</div>
<div>
Blocking queue is an interface. <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html" target="_blank">ArrayBlockingQueue </a> is an concrete implementation of interface. It allow both wait or fail for producer if the queue is full, same way for consumer it fails or waits if the queue is empty.</div>
<div>
<br /></div>
<div>
Some sample codes can be foundin <a href="https://github.com/pallabrath/myexpjava/tree/master/myexpjava/src/demo/blockingQueue">https://github.com/pallabrath/myexpjava/tree/master/myexpjava/src/demo/blockingQueue</a><br />
<br />
What we discussed here is in the scope of single instance. If we want scale then there would be more than one instance. In that case of distributed environment we need different kind of solution. Java message queue fit into this situation.<br />
<br />
Again if we scale further and we want to process data in large volume, Kafka goes well in the distributed environment. Kafka itself run on one or many node to scale.<br />
<br />
We will discuss Kafka in a separate thread.</div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-37051179531855937682017-06-10T02:54:00.000-07:002017-06-10T09:04:25.809-07:00I too want to be cloud developer<div dir="ltr" style="text-align: left;" trbidi="on">
In last few years cloud was one of those word which we developer hardly spend a day with out listening. In fact its becoming quite louder and in future we will see more software run in cloud. As a developer I always wonder cloud means what change in my design and coding. Any way my java program need a JVM whether its on premise or in cloud how does it change. Users will see my web app through a browser what changes if it served from a on premise web server or a cloud web server at the end we need a Java servlet container to run our server side code. So how will I adapt to this cloud computing ?<br />
<div>
<br /></div>
<div>
As I started seeing cloud from more close quarter, I can say with some minor tweaks we can leverage the power of cloud computing. While talking about the cloud the next word come to my mind is "elasticity". It means our IT resources like computing storage etc can seamlessly expand and contract based on the need. With out upfront H/W cost we will pay as we use and we will use as we need. This particular things bring down the cost of IT. So for a developer like us increase the scope of further imagination. We can have more storage and compute as we need.<br />
<br />
To appreciate the elasticity nature of the cloud our software design should be truly distributed and loosely coupled. If the software is segregated in different independent component then its very easy for a particular component can scale to meet demand, which will be the most efficient use of resource. All these things are also popularly known as micro service architecture.<br />
<br />
Another good thing is cloud vendors provide a lots of standard software building blocks and technique. So we can quickly turn around new features and software.<br />
<br />
I believe that cloud will make software more affordable so that more people can use the power computing.<br />
<br /></div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com8tag:blogger.com,1999:blog-8776378117389609026.post-15059733140313551072017-04-09T00:38:00.001-07:002017-04-09T00:38:53.721-07:00Automation is boon or bane<div dir="ltr" style="text-align: left;" trbidi="on">
"Automation" dictionary meaning is "the use or introduction of automatic equipment in a manufacturing or other process or facility. And the next line is ‘unemployment due to the spread of automation’. This word is quite old probably its in practice from 1940. So when we read lots of stories like impact of automation and job loss because of it, I wonder how this problem is not solved yet.<br />
If we look back Human evolution, apart from the biological changes we are continuously improving tools and adapting to use them. So we invent and discover something lets say motor car. then we try to produce them in scale by setting up factory and establishing process. And slowly we delicate these task to different tools which are machine, robots and computers. In this process we force our fellow human from one form of job to another. So we always move to more productive things.<br />
<br />
I belong to digital industry which thrives on the possibility of automation. Computers and software are catalyst for automation. After automating different industry now we are looking inside. It means IT will be more efficient in term of economy more people can afford and use.<br />
<br />
For a developer perspective if we look beyond job risk, there are lots of possibilities are in horizon. Now all the development platform or systems are automatically provisioned. So we can have more servers, compute more storage to write and test our code. With test automation we will be more confident on our changes and quality. With automatic monitoring the health of our production system and software our systems will be more reliable. We are having smart editor which generate some part of code to assist developers. So in less time we can code more features.<br />
<br />
My point is automation is a contentious process and an ultimate truth. The one who adapts it open to learn new productive way of doing always thrive.<br />
<br />
<br /></div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-28351301706328374502016-06-16T07:00:00.002-07:002016-06-16T22:59:37.239-07:00Magic with Computers<div dir="ltr" style="text-align: left;" trbidi="on">
The expectation from a computer is always to do something which surprise us, which complement human brain in terms of memory, processing, decision making, thinking and imagining. When I say computer, it includes smart phone, tablets or wearable devices. When we compare it with washing machine, refrigerator or lift, we always expect more out of a computer.<br />
First time when ATM machines were introduced, people were amazed how banking can be experienced seamlessly. You can deposit money in one location and withdraw from a different location instantly. When we first time sent an email, or did video chat all were magical experience.<br />
<br />
But Now a days all these look normal and nothing great about it. So for a programmer whats next ?<br />
what are the things or feature will get that "wow" from the user, keeping in mind now a days almost every one has been introduced to Facebook and google.<br />
<br />
I feel below points will help to do the magic.<br />
<br />
<b>Simple UI</b><br />
<b><br /></b>
Its very easy to say simple ui, all of us know and its a quite old rule. But still it holds good and the people consciously or sub consciously decide whether to use or not based on user experience.<br />
<br />
There is no rule or some way you can measure whether the one design is better than other. But certainly there are lots of good practices. I believe deeper understanding of the problem and solution based on user perspective, knowing different attribute of your target audience like region, language, age group, popular culture of target audience will certainly help enhance for better user experience.<br />
<br />
User experience is so important that, if you skip reading the below points from now, still you can get a wow from the user. <br />
<br />
<b>Input / Command</b><br />
<br />
We appreciate some one obey the commands and do our work, But we love and addict to those who do the required task with out the command.<br />
The less input,intervention is required for system to work enhance user experience . While designing system we can follow some philosophy like don't ask for explicit input, rather than observe it from the environment or historical data.<br />
<br />
<b>Prediction</b><br />
<br />
Dare to predict and suggest and improve your prediction based on time. Always try to add some more value to the user.<br />
<br />
Its a continuous process, reiterate the above point time to time.<br />
<br />
Let me know your thoughts how to get a wow ....<br />
<br />
<br />
<br />
<br /></div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com0tag:blogger.com,1999:blog-8776378117389609026.post-31031397872109153912015-06-29T10:04:00.001-07:002015-06-29T10:04:14.516-07:00Java Immutable Object<div dir="ltr" style="text-align: left;" trbidi="on">
The simple definition of immutable object is "An object called immutable whose states can't be changed after it constructed". It means every time we need to alter the state we actually end up having a new object. The best example is "java.lang.String".<div>
<br /></div>
<div>
Some body might be thinking its actually overhead, this practice ends up creating too many objects. Actually over the time with optimization of garbage collection and the cost of creating new objects decreases. So in a situation where you want to define constant, or say a multi threading case immutable objects work well. As the value doesn't change at any stage, we are assured of dirty or inconsistent value.</div>
<div>
<br /></div>
<div>
Lets understand how to create our own immutable object.</div>
<div>
<br /></div>
<div>
1) Make the class final, to stop any other class to extend.</div>
<div>
2) All fields in the class should be private and final.</div>
<div>
3) Since we don't want the state of the object to change, there should not be any setter method.</div>
<div>
4) Any method which require to change the state, must create a new object with updated state.</div>
<div>
<br /></div>
<div>
For Ex:</div>
<div>
<br /></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">final public class MyCar</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">{</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> private final String registrationNumber;</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> public MyCar(String registrationNumber)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> {</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> this.registrationNumber = registrationNumber;</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> }</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> public String getRegistrationNumber()</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> {</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> return registrationNumber;</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> }</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> public MyCar upgradeCar(String newCarRegNo)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> {</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> return new MyCar(newCarRegNo);</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> }</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">}</span></div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com1tag:blogger.com,1999:blog-8776378117389609026.post-69943057732071075142014-10-26T23:24:00.001-07:002014-10-26T23:24:24.727-07:00String, StringBuffer and StringBuilder<div dir="ltr" style="text-align: left;" trbidi="on">
With respect to C or C++ in java String handling is different. Java provides String class to represent Strings. For better memory utilization java treats String as constant and their value can't be changed.<div>
This behavior is popularly known as immutable.The idea is if some one write</div>
<div>
<i>String hello = "Hello"; </i></div>
<div>
<i>String greetings = "Hello";</i></div>
<div>
Internally both refer to same object. In JVM memory area there is a dedicated memory for these String literals, known as String pool.</div>
<div>
So in our example <i>hello == greetings </i>will return true.</div>
<div>
But if someone write</div>
<div>
<i>String str = new String("Hello");</i></div>
<div>
It will create a new object, it won't point to same literal available in string pool.</div>
<div>
So hello == str will return false. </div>
<div>
Another advantage of java String is we can use + for concatenation.</div>
<div>
<br /></div>
<div>
<i>String greetMe = hello + "Java";</i></div>
<div>
<br /></div>
<div>
We can concatenate diffrent type of object other than String with String too. The typecast is automatic.String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.</div>
<div>
As we learned String objects are immutable means the values cannot be altered. So strings are always thread safe. Any operation such as <i>hello.toLowerCase()</i> will result another object. For extensive String manipulation java provides two other classes StringBuffer and StringBuilder. These classes gives us a way to manipulate String efficiently. The only difference is StringBuffer is thread safe while StringBuilder is not. So by design StringBuilder is faster than StringBuffer.</div>
<div>
<br /></div>
<div>
So when to use what</div>
<div>
<br /></div>
<div>
String is used to handle multiple literal, it gives you optimum way to handle duplicate literal for better memory utilization.</div>
<div>
<br /></div>
<div>
StringBuffer is natural choice if we need to do a lots of String manipulation in a thread safe way. If synchronization is not the issue StringBuilder should be used since its faster than StringBuffer.</div>
<div>
<br /></div>
<div>
Have fun with your Strings ;)</div>
</div>
Pallab Rathhttp://www.blogger.com/profile/15600801455756472141noreply@blogger.com9