1 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.surefire.booter.Command;
23 import org.apache.maven.surefire.booter.MasterProcessCommand;
24
25 import java.io.IOException;
26
27 /**
28 * Reader stream sends commands to forked jvm std-{@link java.io.InputStream input-stream}.
29 *
30 * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
31 * @since 2.19
32 * @see org.apache.maven.surefire.booter.Command
33 */
34 public abstract class AbstractCommandStream
35 extends AbstractForkInputStream
36 {
37 private byte[] currentBuffer;
38 private int currentPos;
39
40 protected abstract boolean isClosed();
41
42 /**
43 * Opposite to {@link #isClosed()}.
44 * @return {@code true} if not closed
45 */
46 protected boolean canContinue()
47 {
48 return !isClosed();
49 }
50
51 /**
52 * Possibly waiting for next command (see {@link #nextCommand()}) unless the stream is atomically
53 * closed (see {@link #isClosed()} returns {@code true}) before this method has returned.
54 *
55 * @throws IOException stream error while waiting for notification regarding next test required by forked jvm
56 */
57 protected void beforeNextCommand()
58 throws IOException
59 {
60 }
61
62 protected abstract Command nextCommand();
63
64 /**
65 * Returns quietly and immediately.
66 */
67 protected final void invalidateInternalBuffer()
68 {
69 currentBuffer = null;
70 currentPos = 0;
71 }
72
73 /**
74 * Used by single thread in StreamFeeder class.
75 *
76 * @return {@inheritDoc}
77 * @throws IOException {@inheritDoc}
78 */
79 @SuppressWarnings( "checkstyle:magicnumber" )
80 @Override
81 public int read()
82 throws IOException
83 {
84 if ( isClosed() )
85 {
86 tryFlush();
87 return -1;
88 }
89
90 if ( currentBuffer == null )
91 {
92 tryFlush();
93
94 if ( !canContinue() )
95 {
96 close();
97 return -1;
98 }
99
100 beforeNextCommand();
101
102 if ( isClosed() )
103 {
104 return -1;
105 }
106
107 Command cmd = nextCommand();
108 MasterProcessCommand cmdType = cmd.getCommandType();
109 currentBuffer = cmdType.hasDataType() ? cmdType.encode( cmd.getData() ) : cmdType.encode();
110 }
111
112 int b = currentBuffer[currentPos++] & 0xff;
113 if ( currentPos == currentBuffer.length )
114 {
115 currentBuffer = null;
116 currentPos = 0;
117 }
118 return b;
119 }
120 }